标签:can scan pre ack create class val after value
demo
1 package com.bjsxt.hbase; 2 3 import java.io.IOException; 4 5 import org.apache.hadoop.conf.Configuration; 6 import org.apache.hadoop.hbase.Cell; 7 import org.apache.hadoop.hbase.CellUtil; 8 import org.apache.hadoop.hbase.HColumnDescriptor; 9 import org.apache.hadoop.hbase.HTableDescriptor; 10 import org.apache.hadoop.hbase.TableName; 11 import org.apache.hadoop.hbase.client.Get; 12 import org.apache.hadoop.hbase.client.HBaseAdmin; 13 import org.apache.hadoop.hbase.client.HTable; 14 import org.apache.hadoop.hbase.client.Put; 15 import org.apache.hadoop.hbase.client.Result; 16 import org.apache.hadoop.hbase.client.ResultScanner; 17 import org.apache.hadoop.hbase.client.Scan; 18 import org.apache.hadoop.hbase.util.Bytes; 19 import org.junit.After; 20 import org.junit.Before; 21 import org.junit.Test; 22 23 public class HBaseDemo { 24 25 //表的管理类 26 HBaseAdmin admin = null; 27 //数据的管理类 28 HTable table = null; 29 //表名 30 String tm = "phone"; 31 32 /** 33 * 完成初始化功能 34 * @throws Exception 35 */ 36 @Before 37 public void init() throws Exception{ 38 Configuration conf = new Configuration(); 39 conf.set("hbase.zookeeper.quorum", "node1,node2,node3"); 40 admin = new HBaseAdmin(conf); 41 table = new HTable(conf,tm.getBytes()); 42 } 43 44 /** 45 * 创建表 46 * @throws Exception 47 */ 48 @Test 49 public void createTable() throws Exception{ 50 //表的描述类 51 HTableDescriptor desc = new HTableDescriptor(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 @Test 63 public void insert() throws Exception{ 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 @Test 71 public void get() throws Exception{ 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 @Test 87 public void scan() throws Exception{ 88 Scan scan = new Scan(); 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 @After 103 public void destory() throws Exception{ 104 if(admin!=null){ 105 admin.close(); 106 } 107 } 108 }
标签:can scan pre ack create class val after value
原文地址:https://www.cnblogs.com/fxw-learning/p/12458083.html