hbase java开发 demo_hbase-java的demo

1 packagecom.bjsxt.hbase;2

3 importjava.io.IOException;4

5 importorg.apache.hadoop.conf.Configuration;6 importorg.apache.hadoop.hbase.Cell;7 importorg.apache.hadoop.hbase.CellUtil;8 importorg.apache.hadoop.hbase.HColumnDescriptor;9 importorg.apache.hadoop.hbase.HTableDescriptor;10 importorg.apache.hadoop.hbase.TableName;11 importorg.apache.hadoop.hbase.client.Get;12 importorg.apache.hadoop.hbase.client.HBaseAdmin;13 importorg.apache.hadoop.hbase.client.HTable;14 importorg.apache.hadoop.hbase.client.Put;15 importorg.apache.hadoop.hbase.client.Result;16 importorg.apache.hadoop.hbase.client.ResultScanner;17 importorg.apache.hadoop.hbase.client.Scan;18 importorg.apache.hadoop.hbase.util.Bytes;19 importorg.junit.After;20 importorg.junit.Before;21 importorg.junit.Test;22

23 public classHBaseDemo {24

25 //表的管理类

26 HBaseAdmin admin = null;27 //数据的管理类

28 HTable table = null;29 //表名

30 String tm = "phone";31

32 /**

33 * 完成初始化功能34 *@throwsException35 */

36 @Before37 public void init() throwsException{38 Configuration conf = newConfiguration();39 conf.set("hbase.zookeeper.quorum", "node1,node2,node3");40 admin = newHBaseAdmin(conf);41 table = newHTable(conf,tm.getBytes());42 }43

44 /**

45 * 创建表46 *@throwsException47 */

48 @Test49 public void createTable() throwsException{50 //表的描述类

51 HTableDescriptor desc = newHTableDescriptor(TableName.valueOf(tm));52 //列族的描述类

53 HColumnDescriptor family = new HColumnDescriptor("cf".getBytes());54 desc.addFamily(family);55 if(admin.tableExists(tm)){56 admin.disableTable(tm);57 admin.deleteTable(tm);58 }59 admin.createTable(desc);60 }61

62 @Test63 public void insert() throwsException{64 Put put = new Put("1111".getBytes());65 put.add("cf".getBytes(), "name".getBytes(), "zhangsan".getBytes());66 put.add("cf".getBytes(), "age".getBytes(), "12".getBytes());67 put.add("cf".getBytes(), "sex".getBytes(), "man".getBytes());68 table.put(put);69 }70 @Test71 public void get() throwsException{72 Get get = new Get("1111".getBytes());73 //添加要获取的列和列族,减少网络的io,相当于在服务器端做了过滤

74 get.addColumn("cf".getBytes(), "name".getBytes());75 get.addColumn("cf".getBytes(), "age".getBytes());76 get.addColumn("cf".getBytes(), "sex".getBytes());77 Result result =table.get(get);78 Cell cell1 = result.getColumnLatestCell("cf".getBytes(), "name".getBytes());79 Cell cell2 = result.getColumnLatestCell("cf".getBytes(), "age".getBytes());80 Cell cell3 = result.getColumnLatestCell("cf".getBytes(), "sex".getBytes());81 System.out.println(Bytes.toString(CellUtil.cloneValue(cell1)));82 System.out.println(Bytes.toString(CellUtil.cloneValue(cell2)));83 System.out.println(Bytes.toString(CellUtil.cloneValue(cell3)));84 }85

86 @Test87 public void scan() throwsException{88 Scan scan = newScan();89 //scan.setStartRow(startRow);90 //scan.setStopRow(stopRow);

91 ResultScanner rss =table.getScanner(scan);92 for(Result result : rss) {93 Cell cell1 = result.getColumnLatestCell("cf".getBytes(), "name".getBytes());94 Cell cell2 = result.getColumnLatestCell("cf".getBytes(), "age".getBytes());95 Cell cell3 = result.getColumnLatestCell("cf".getBytes(), "sex".getBytes());96 System.out.println(Bytes.toString(CellUtil.cloneValue(cell1)));97 System.out.println(Bytes.toString(CellUtil.cloneValue(cell2)));98 System.out.println(Bytes.toString(CellUtil.cloneValue(cell3)));99 }100 }101

102 @After103 public void destory() throwsException{104 if(admin!=null){105 admin.close();106 }107 }108 }