HBase基本操作-Java实现

创建Table

 1 public static void createTable(String tableName){
 2         try {
 3             HBaseAdmin hbaseAdmin = new HBaseAdmin(HBaseConfiguration.create());
 4             
 5             if(hbaseAdmin.tableExists(tableName)){
 6                 hbaseAdmin.disableTable(tableName);
 7                 hbaseAdmin.deleteTable(tableName);
 8                 System.out.println("Table "+ tableName +" is already exist");
 9             }
10             
11 //            HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
12             //新版 API
13             HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
14             hTableDescriptor.addFamily(new HColumnDescriptor("name"));
15             hTableDescriptor.addFamily(new HColumnDescriptor("course"));
16             hTableDescriptor.addFamily(new HColumnDescriptor("address"));
17             hbaseAdmin.createTable(hTableDescriptor);
18             
19             hbaseAdmin.close();
20             System.out.println("Create table "+ tableName +" finish...");
21             
22         } catch (IOException e) {
23             e.printStackTrace();
24         }
25     }

插入数据

 1 public static void insertDate(String tableName){
 2         try {
 3             HTable hTable = new HTable(HBaseConfiguration.create(),tableName);
 4             Put putRow = null;
 5             for(int i=1;i<=1000;i++){
 6                 putRow = new Put(("rowkey"+i).getBytes());
 7                 putRow.add("name".getBytes(), null, getNameByRandom());
 8                 putRow.add("course".getBytes(), "CHINESE".getBytes(), getScoreByRandom());
 9                 putRow.add("course".getBytes(), "ENGLISH".getBytes(), getScoreByRandom());
10                 putRow.add("course".getBytes(), "MATH".getBytes(), getScoreByRandom());
11                 putRow.add("address".getBytes(), "CITY".getBytes(), getNameByRandom());
12                 putRow.add("address".getBytes(), "STREET".getBytes(), getNameByRandom());
13                 putRow.add("address".getBytes(), "PROVINCE".getBytes(), getNameByRandom());
14                 hTable.put(putRow);
15             }
16             hTable.close();
17             System.out.println("Insert finish...");
18         } catch (IOException e) {
19             e.printStackTrace();
20         }
21     }
22 
23     public static byte[] getScoreByRandom() {
24         return (((int)(Math.random()*60)+40)+"").getBytes();
25     }
26 
27     public static byte[] getNameByRandom() {
28         int length = (int)(Math.random()*5)+3;
29         String nameStr = ""+(char)((int)(Math.random()*26)+'A');
30         for(int i=1;i<length;i++){
31             char c = (char)((int)(Math.random()*26)+'a');
32             nameStr+=c;
33         }
34         return nameStr.getBytes();
35     }

查询数据

 1 public static void queryByRowKeyFamily(String tableName,String rowKey,String family){
 2         try {
 3             HTable table = new HTable(HBaseConfiguration.create(), tableName);
 4             Get getRow = new Get(Bytes.toBytes(rowKey));
 5             getRow.addFamily(family.getBytes());
 6             Result result = table.get(getRow);
 7             
 8             for(KeyValue kv:result.list()){
 9                 System.out.println(new String(kv.getFamily())+"."+new String(kv.getQualifier())+" "+new String(kv.getValue()));
10             }
11             System.out.println(Bytes.toString(result.getRow()));
12             table.close();
13         } catch (IOException e) {
14             e.printStackTrace();
15         }
16     }
原文地址:https://www.cnblogs.com/nashiyue/p/4236916.html