HBase Java API 使用示例

在使用HBase Java API 之前,大家首先要了解HBase Java API类,可参考博客:http://www.cnblogs.com/ggjucheng/p/3380267.html

几个相关类与HBase数据模型之间的对应关系

java类 HBase数据模型
HBaseAdmin 数据库(DataBase)
HBaseConfiguration
HTable 表(Table)
HTableDescriptor 列族(Column Family)
Put 列修饰符(Column Qualifier)
Get
Scanner

HBaseConfiguration:对HBase进行配置。

HBaseAdmin:提供了一个接口来管理HBase数据库的表信息。它提供的方法包括:创建表,删除表,列出表项,使表有效或无效,以及添加或删除表列族成员等。

HTableDescriptor:包含了表的名字极其对应表的列族

HColumnDescriptor:维护着关于列族的信息,例如版本号,压缩设置等。它通常在创建表或者为表添加列族的时候使用。列族被创建后不能直接修改,只能通过删除然后重新创建的方式。列族被删除的时候,列族里面的数据也会同时被删除。

HTable:可以用来和HBase表直接通信。此方法对于更新操作来说是非线程安全的。

Put:用来对单个行执行添加操作。

Get:用来获取单个行的相关信息。

Result:存储Get或者Scan操作后获取表的单行值。使用此类提供的方法可以直接获取值或者各种Map结构(key-value对)。

ResultScanner:客户端获取值的接口。

代码示例:







  1. public class HBaseDemo {  
  2.   
  3.     private Configuration conf = null;  
  4.       
  5.     @Before  
  6.     public void init(){  
  7.         conf = HBaseConfiguration.create();  
  8.         //客户端连接zookeeper  
  9.         conf.set(”hbase.zookeeper.quorum”“hadoop01,hadoop02,hadoop03”);  
  10.     }  
  11.       
  12.     @Test  
  13.     public void testDrop() throws Exception{  
  14.           
  15.         HBaseAdmin admin = new HBaseAdmin(conf);  
  16.         admin.disableTable(”account”);  
  17.         admin.deleteTable(”account”);  
  18.         admin.close();  
  19.     }  
  20.     /** 
  21.     *插入数据 
  22.     */  
  23.     @Test  
  24.     public void testPut() throws Exception{  
  25.         HTable table = new HTable(conf, “user”);  
  26.         //提供row key  
  27.         Put put = new Put(Bytes.toBytes(“rk0003”));  
  28.         //列族、列的标示符、值,参数都是字节数组  
  29.         put.add(Bytes.toBytes(”info”), Bytes.toBytes(“name”), Bytes.toBytes(“liuyan”));  
  30.         table.put(put);  
  31.         table.close();  
  32.     }  
  33.     /** 
  34.     *获取数据 
  35.     */  
  36.     @Test  
  37.     public void testGet() throws Exception{  
  38.         //HTablePool pool = new HTablePool(conf, 10);  
  39.         //HTable table = (HTable) pool.getTable(“user”);  
  40.         HTable table = new HTable(conf, “user”);  
  41.         Get get = new Get(Bytes.toBytes(“rk0001”));  
  42.         //get.addColumn(Bytes.toBytes(“info”), Bytes.toBytes(“name”));  
  43.         get.setMaxVersions(5);  
  44.         Result result = table.get(get);  
  45.           
  46.       //String r = Bytes.toString(result.getValue(family, qualifier) );这个获取方法不常用  
  47.           
  48.         for(KeyValue kv : result.list()){  
  49.             String family = new String(kv.getFamily());  
  50.             System.out.println(family);  
  51.             String qualifier = new String(kv.getQualifier());  
  52.             System.out.println(qualifier);  
  53.             System.out.println(new String(kv.getValue()));  
  54.         }  
  55.         table.close();  
  56.     }  
  57.       
  58.     @Test  
  59.     public void testScan() throws Exception{  
  60.         HTablePool pool = new HTablePool(conf, 10);  
  61.         HTableInterface table = pool.getTable(”user”);  
  62.         Scan scan = new Scan(Bytes.toBytes(“rk0001”), Bytes.toBytes(“rk0002”));  
  63.         scan.addFamily(Bytes.toBytes(”info”));  
  64.         //结果集   
  65.         ResultScanner scanner = table.getScanner(scan);  
  66.         for(Result r : scanner){  
  67.             /** 
  68.             for(KeyValue kv : r.list()){ 
  69.                 String family = new String(kv.getFamily()); 
  70.                 System.out.println(family); 
  71.                 String qualifier = new String(kv.getQualifier()); 
  72.                 System.out.println(qualifier); 
  73.                 System.out.println(new String(kv.getValue())); 
  74.             } 
  75.             */  
  76.             byte[] value = r.getValue(Bytes.toBytes(“info”), Bytes.toBytes(“name”));  
  77.             System.out.println(new String(value));  
  78.         }  
  79.         pool.close();  
  80.     }  
  81.       
  82.       
  83.     @Test  
  84.     public void testDel() throws Exception{  
  85.         HTable table = new HTable(conf, “user”);  
  86.         Delete del = new Delete(Bytes.toBytes(“rk0001”));  
  87.         del.deleteColumn(Bytes.toBytes(”data”), Bytes.toBytes(“pic”));  
  88.         table.delete(del);  
  89.         table.close();  
  90.     }  
  91.       
  92.       
  93.       
  94.       
  95.     public static void main(String[] args) throws Exception {  
  96.         Configuration conf = HBaseConfiguration.create();  
  97.         conf.set(”hbase.zookeeper.quorum”“hadoop01,hadoop02,hadoop03”);  
  98.         //ddl操作,传入conf,可知操作哪个hbase集群  
  99.         HBaseAdmin admin = new HBaseAdmin(conf);  
  100.         HTableDescriptor td = new HTableDescriptor(“account”);  
  101.         HColumnDescriptor cd = new HColumnDescriptor(“info”);  
  102.         cd.setMaxVersions(10);  
  103.         td.addFamily(cd);  
  104.         admin.createTable(td);  
  105.         admin.close();  
  106.   
  107.     }  
  108.       
  109.     public void createTable(String tableName, int maxVersion, String… cf){  
  110.           
  111.     }  
  112.   
  113. }  
public class HBaseDemo {

    private Configuration conf = null;

    @Before
    public void init(){
        conf = HBaseConfiguration.create();
        //客户端连接zookeeper
        conf.set("hbase.zookeeper.quorum", "hadoop01,hadoop02,hadoop03");
    }

    @Test
    public void testDrop() throws Exception{

        HBaseAdmin admin = new HBaseAdmin(conf);
        admin.disableTable("account");
        admin.deleteTable("account");
        admin.close();
    }
    /**
    *插入数据
    */
    @Test
    public void testPut() throws Exception{
        HTable table = new HTable(conf, "user");
        //提供row key
        Put put = new Put(Bytes.toBytes("rk0003"));
        //列族、列的标示符、值,参数都是字节数组
        put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("liuyan"));
        table.put(put);
        table.close();
    }
    /**
    *获取数据
    */
    @Test
    public void testGet() throws Exception{
        //HTablePool pool = new HTablePool(conf, 10);
        //HTable table = (HTable) pool.getTable("user");
        HTable table = new HTable(conf, "user");
        Get get = new Get(Bytes.toBytes("rk0001"));
        //get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
        get.setMaxVersions(5);
        Result result = table.get(get);

      //String r = Bytes.toString(result.getValue(family, qualifier) );这个获取方法不常用

        for(KeyValue kv : result.list()){
            String family = new String(kv.getFamily());
            System.out.println(family);
            String qualifier = new String(kv.getQualifier());
            System.out.println(qualifier);
            System.out.println(new String(kv.getValue()));
        }
        table.close();
    }

    @Test
    public void testScan() throws Exception{
        HTablePool pool = new HTablePool(conf, 10);
        HTableInterface table = pool.getTable("user");
        Scan scan = new Scan(Bytes.toBytes("rk0001"), Bytes.toBytes("rk0002"));
        scan.addFamily(Bytes.toBytes("info"));
        //结果集 
        ResultScanner scanner = table.getScanner(scan);
        for(Result r : scanner){
            /**
            for(KeyValue kv : r.list()){
                String family = new String(kv.getFamily());
                System.out.println(family);
                String qualifier = new String(kv.getQualifier());
                System.out.println(qualifier);
                System.out.println(new String(kv.getValue()));
            }
            */
            byte[] value = r.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"));
            System.out.println(new String(value));
        }
        pool.close();
    }


    @Test
    public void testDel() throws Exception{
        HTable table = new HTable(conf, "user");
        Delete del = new Delete(Bytes.toBytes("rk0001"));
        del.deleteColumn(Bytes.toBytes("data"), Bytes.toBytes("pic"));
        table.delete(del);
        table.close();
    }




    public static void main(String[] args) throws Exception {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "hadoop01,hadoop02,hadoop03");
        //ddl操作,传入conf,可知操作哪个hbase集群
        HBaseAdmin admin = new HBaseAdmin(conf);
        HTableDescriptor td = new HTableDescriptor("account");
        HColumnDescriptor cd = new HColumnDescriptor("info");
        cd.setMaxVersions(10);
        td.addFamily(cd);
        admin.createTable(td);
        admin.close();

    }

    public void createTable(String tableName, int maxVersion, String... cf){

    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值