• 2020101802


    如每周总结所说,HBase无法解析的问题是本机的主机名未采用纯英文导致。改用英文后解决。

    HBase连接工具类

      1 package konoha.util;
      2 
      3 import java.io.IOException;
      4 
      5 import org.apache.hadoop.conf.Configuration;
      6 import org.apache.hadoop.hbase.HBaseConfiguration;
      7 import org.apache.hadoop.hbase.HColumnDescriptor;
      8 import org.apache.hadoop.hbase.HTableDescriptor;
      9 import org.apache.hadoop.hbase.TableName;
     10 import org.apache.hadoop.hbase.client.Admin;
     11 import org.apache.hadoop.hbase.client.Connection;
     12 import org.apache.hadoop.hbase.client.ConnectionFactory;
     13 import org.apache.hadoop.hbase.client.Delete;
     14 import org.apache.hadoop.hbase.client.Get;
     15 import org.apache.hadoop.hbase.client.Put;
     16 import org.apache.hadoop.hbase.client.Result;
     17 import org.apache.hadoop.hbase.client.ResultScanner;
     18 import org.apache.hadoop.hbase.client.Scan;
     19 import org.apache.hadoop.hbase.client.Table;
     20 import org.apache.hadoop.hbase.util.Bytes;
     21 
     22 public class HBaseTestUtil {
     23     public static Configuration configuration;
     24     public static Connection connection;
     25     public static Admin admin;
     26 
     27     public static void init() {
     28         configuration = HBaseConfiguration.create();
     29         configuration.set("hbase.zookeeper.quorum", "localhost");
     30         configuration.set("hbase.zookeeper.property.clientPort", "2181");
     31         try {
     32             connection = ConnectionFactory.createConnection(configuration);
     33             admin = connection.getAdmin();
     34         } catch (IOException e) {
     35             e.printStackTrace();
     36         }
     37     }
     38 
     39     public static void close() {
     40         try {
     41             if (admin != null) {
     42                 admin.close();
     43             }
     44             if (null != connection) {
     45                 connection.close();
     46             }
     47         } catch (IOException e) {
     48             e.printStackTrace();
     49         }
     50     }
     51 
     52     public static void getDataTest() throws Exception {
     53         init();
     54         Table table = connection.getTable(TableName.valueOf("Student"));
     55         Get get = new Get(Bytes.toBytes("scofield"));
     56         get.addColumn(Bytes.toBytes("English"), Bytes.toBytes(""));
     57         Result result = table.get(get);
     58         System.out.println(new String(result.getValue("English".getBytes(), null)));
     59         table.close();
     60     }
     61 
     62     public static void createTable(String myTableName, String[] colFamily) throws IOException {
     63         init();
     64         TableName tableName = TableName.valueOf(myTableName);
     65         if (admin.tableExists(tableName)) {
     66             System.out.println("talbe is exists!");
     67             admin.disableTable(tableName);
     68             admin.deleteTable(tableName);
     69         }
     70         HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
     71         for (String str : colFamily) {
     72             HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
     73             hTableDescriptor.addFamily(hColumnDescriptor);
     74             admin.createTable(hTableDescriptor);
     75             System.out.println("create table success");
     76         }
     77         close();
     78     }
     79 
     80     public static void addRecord(String tableName, String row, String[] fields, String[] values) throws IOException {
     81         init();
     82         Table table = connection.getTable(TableName.valueOf(tableName));
     83         for (int i = 0; i < fields.length; i++) {
     84             Put put = new Put(row.getBytes());
     85             String[] cols = fields[i].split(":");
     86             put.addColumn(cols[0].getBytes(), cols[1].getBytes(), values[i].getBytes());
     87             table.put(put);
     88         }
     89         table.close();
     90         close();
     91     }
     92 
     93     public static void scanColumn(String tableName, String column) throws IOException {
     94         init();
     95         Table table = connection.getTable(TableName.valueOf(tableName));
     96         Scan scan = new Scan();
     97         scan.addFamily(Bytes.toBytes(column));
     98         ResultScanner scanner = table.getScanner(scan);
     99         for (Result result = scanner.next(); result != null; result = scanner.next()) {
    100             System.out.println(result);
    101         }
    102         table.close();
    103         close();
    104     }
    105 
    106     public static void modiyData(String tableName, String row, String column, String val) throws IOException {
    107         init();
    108         Table table = connection.getTable(TableName.valueOf(tableName));
    109         Put put = new Put(row.getBytes());
    110         String[] cols = column.split(":");
    111         put.addColumn(cols[0].getBytes(), cols[1].getBytes(), val.getBytes());
    112         table.put(put);
    113         table.close();
    114         close();
    115     }
    116 
    117     public static void deleteRow(String tableName, String row) throws IOException {
    118         init();
    119         Table table = connection.getTable(TableName.valueOf(tableName));
    120         Delete delete = new Delete(row.getBytes());
    121         table.delete(delete);
    122         table.close();
    123         close();
    124     }
    125 
    126     public static void getData(String tableName, String rowKey, String colFamily, String col) throws IOException {
    127         Table table = connection.getTable(TableName.valueOf(tableName));
    128         Get get = new Get(Bytes.toBytes(rowKey));
    129         get.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col));
    130         Result result = table.get(get);
    131         System.out.println(new String(result.getValue(colFamily.getBytes(), col == null ? null : col.getBytes())));
    132         table.close();
    133     }
    134 
    135     public static void insertData(String tableName, String rowKey, String colFamily, String col, String val)
    136             throws IOException {
    137         Table table = connection.getTable(TableName.valueOf(tableName));
    138         Put put = new Put(Bytes.toBytes(rowKey));
    139         put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col), Bytes.toBytes(val));
    140         table.put(put);
    141         table.close();
    142     }
    143 
    144     public static void main(String[] args) {
    145         try {
    146             scanColumn("Student", "S_Name");
    147         } catch (Exception e) {
    148             // TODO Auto-generated catch block
    149             e.printStackTrace();
    150         }
    151     }
    152 }
    HBaseTestUtil

    修改后的录入处理JSP

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <%@page import="java.sql.*" %>  
    <%@page import="java.io.PrintWriter" %>
    <%@page import="java.io.IOException" %>  
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>提交中....</title>
    </head>
    <body>
    
    
      <jsp:useBean id="db" class="bean.DBean" scope="page" />
      <%@ page import = "Serial.SerialID"%> 
    <%
        request.setCharacterEncoding("UTF-8");
        SerialID serial=new SerialID();
            String demandID="";
            demandID=serial.get(demandID);
            //System.out.println(demandID);
            String unitID="";
            String unitName="";
            String unitDepartment="";
            String unitAddress="";
            String unitCity="";
            String unitURL="";
            String unitEmail="";
            String unitLPeople="";
            String unitZipCode="";
            String unitCPeople="";
            String unitPhone="";
            String unitMPhone="";
            String unitFax="";
            String unitAttribute="";
            String unitIntroduce="";
            
            String userID=(String)session.getAttribute("LoginID");
            String sql="select * from unit where userID="+"'"+userID+"'";
            ResultSet rss=db.executeQuery(sql);
            while(rss.next())
            {
                unitID=rss.getString("unitID");
                unitName=rss.getString("unitName");
                unitDepartment=rss.getString("unitDepartment");
                unitAddress=rss.getString("unitAddress");
                unitCity=rss.getString("unitCity");
                unitURL=rss.getString("unitURL");
                unitEmail=rss.getString("unitEmail");
                unitLPeople=rss.getString("unitLPeople");
                unitZipCode=rss.getString("unitZipCode");
                unitCPeople=rss.getString("unitCPeople");
                unitPhone=rss.getString("unitPhone");
                unitMPhone=rss.getString("unitMPhone");
                unitFax=rss.getString("unitFax");
                unitAttribute=rss.getString("unitAttribute");
                unitIntroduce=rss.getString("unitIntroduce");
            }
            
            
            String demandName=(String)request.getParameter("demandName");
            String demandStart=(String)request.getParameter("demandStart");
            String demandEnd=(String)request.getParameter("demandEnd");
            String demandMain=(String)request.getParameter("demandMain");
            String demandKey=(String)request.getParameter("demandKey");
            String demandTarget=(String)request.getParameter("demandTarget");
            String demandKeyWord1=(String)request.getParameter("demandKeyWord1");
            String demandKeyWord2=(String)request.getParameter("demandKeyWord2");
            String demandKeyWord3=(String)request.getParameter("demandKeyWord3");
            String demandKeyWord4=(String)request.getParameter("demandKeyWord4");
            String demandKeyWord5=(String)request.getParameter("demandKeyWord5");
            String demandLoan=(String)request.getParameter("demandLoan");
            String demandResearchMode=(String)request.getParameter("demandResearchMode");
            String demandCooperationUnit=(String)request.getParameter("demandCooperationUnit");
            String demandTechnology=(String)request.getParameter("demandTechnology");
            String demandClass3=(String)request.getParameter("Classification3");
            String demandDomain=(request.getParameter("Filed1")+request.getParameter("Filed2")+
                    request.getParameter("Filed3")+request.getParameter("Filed4")+
                    request.getParameter("Filed5")+request.getParameter("Filed6")
                    +request.getParameter("Filed7")+request.getParameter("Filed8")
                    +request.getParameter("Filed9")+request.getParameter("Filed10")
                    +request.getParameter("Filed11")+request.getParameter("Filed12")).replaceAll("null","");
            String demandEconomicClass3=(String)request.getParameter("Industry3");
            String submitType=(String)request.getParameter("submitType");
            String s ="Select * From Requirement";
            ResultSet rs = db.executeQuery(s);
            String demandKeyWord = demandKeyWord1;
            if(!demandKeyWord2.equals("")){
                demandKeyWord = demandKeyWord+" "+demandKeyWord2;
            }
            if(!demandKeyWord3.equals("")){
                demandKeyWord = demandKeyWord+" "+demandKeyWord3;
            }
            if(!demandKeyWord4.equals("")){
                demandKeyWord = demandKeyWord+" "+demandKeyWord4;
            }
            if(!demandKeyWord5.equals("")){
                demandKeyWord = demandKeyWord+" "+demandKeyWord5;
            }
            
            System.out.println("[INFO]Intalizing...");
            HBaseTestUtil.init();
            try {
                System.out.println("[INFO]Start INSERT into table@HBase");
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "unitID", unitID);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "unitName", unitName);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "unitDepartment", unitDepartment);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "unitAddress", unitAddress);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "unitCity", unitCity);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "unitURL", unitURL);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "unitMail", unitEmail);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "unitLPeople", unitLPeople);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "unitZipCode", unitZipCode);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "unitCPeople", unitCPeople);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "unitPhone", unitPhone);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "unitMPhone", unitMPhone);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "unitFax", unitFax);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "unitAttribute", unitAttribute);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "unitIntroduce", unitIntroduce);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "demandName", demandName);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "demandStart", demandStart);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "demandEnd", demandEnd);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "demandMain", demandMain);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "demandKey", demandKey);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "demandTarget", demandTarget);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "demandName", demandName);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "demandKeyWord1", demandKeyWord1);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "demandKeyWord2", demandKeyWord2);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "demandKeyWord3", demandKeyWord3);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "demandKeyWord4", demandKeyWord4);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "demandKeyWord5", demandKeyWord5);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "demandLoan", demandLoan);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "demandResearchMode", demandResearchMode);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "demandCooperationUnit", demandCooperationUnit);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "demandTechnology", demandTechnology);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "demandClass3", demandClass3);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "demandDomain", demandDomain);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "demandEconomicClass3", demandEconomicClass3);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "unitstaffID1", "");
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "unitstaffID2", "");
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "demandState1", submitType);
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "demandState2", "未审核");
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "Auditopinion", "");
                HBaseTestUtil.insertData("Requirement", demandID, "demandInfo", "Auditdepartment", "");
                System.out.println("[INFO]Insert into table@HBase Success");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            HBaseTestUtil.close();
            db.close(); 
            System.out.println("[INFO]Process Done");
            
        %>    
            
    </body>
    </html>
    JSP文件代码
  • 相关阅读:
    刚听完CSDN总裁蒋涛先生的学术报告
    WinForm下屏幕截图程序的实现
    .NET4.5 Async 与 Async Targeting Pack区别
    WP8中的Tiles
    WP8中调用APP的方式
    安装Win8后必做的优化
    如何将项目从WP7升级到WP8
    ActiveWriter集成到VS.NET的NHibernate(ActiveRecord)对象可视化设计工具
    概述CSLA.NET 3.6 (Overview of CSLA .NET 3.6 for Windows and Silverlight)
    SQL Server BI Step by Step 1 准备
  • 原文地址:https://www.cnblogs.com/minadukirinno/p/14199328.html
Copyright © 2020-2023  润新知