hbase-java的demo

Posted fxw-learning

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hbase-java的demo相关的知识,希望对你有一定的参考价值。

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 }

 

以上是关于hbase-java的demo的主要内容,如果未能解决你的问题,请参考以下文章

12mmaction2 行为识别商用级别X3D复现 demo实现 检测自己的视频 Expanding Architecturesfor Efficient Video Recognition(代码片段

面试常用的代码片段

[异常解决] Keil安装好nRF51822开发环境,运行DEMO报错:Error:“GPIOTE_CONFIG_NUM_OF_LOW_POWER_ENVENTS” is undefined(代码片段

VS code自定义用户代码片段snippet

jquery 对象的 heightinnerHeightouterHeight 的区别以及DOM 元素的 clientHeightoffsetHeightscrollHeightoffset(代码片段

视频直接变漫画!GAN又有了新玩法|Demo+代码+论文