HBase Java API 使用示例

来源:互联网 发布:淘宝联盟分享赚互刷 编辑:程序博客网 时间:2024/05/21 09:18

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

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

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

HBaseConfiguration:对HBase进行配置。

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

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

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

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

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

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

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

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

代码示例:
public class HBaseDemo {private Configuration conf = null;@Beforepublic void init(){conf = HBaseConfiguration.create();//客户端连接zookeeperconf.set("hbase.zookeeper.quorum", "hadoop01,hadoop02,hadoop03");}@Testpublic void testDrop() throws Exception{HBaseAdmin admin = new HBaseAdmin(conf);admin.disableTable("account");admin.deleteTable("account");admin.close();}/***插入数据*/@Testpublic void testPut() throws Exception{HTable table = new HTable(conf, "user");//提供row keyPut put = new Put(Bytes.toBytes("rk0003"));//列族、列的标示符、值,参数都是字节数组put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("liuyan"));table.put(put);table.close();}/***获取数据*/@Testpublic 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();}@Testpublic 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();}@Testpublic 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){}}


0 0
原创粉丝点击