java调用hbase的开发

引言
HBase提供了Java Api的访问接口,掌握这个就跟Java应用使用RDBMS时需要JDBC一样重要,本文将继续前两篇文章中blog表的示例,介绍常用的Api。
练习前的准备工作

  • 创建一个Maven工程,加入以下依赖:

<br /><dependency><br /><groupId>org.apache.hbase</groupId><br /><artifactId>hbase</artifactId><br /><version>0.90.2</version><br /></dependency>
如果你的Maven库里还没有hbase,还需要配置下repository
<repositories><br /><repository><br /><id>cloudera</id><br /><url>https://repository.cloudera.com/content/groups/public</url><br /></repository><br /></repositories>

  • 确保HBase环境已启动且能连接到,将HBase环境的hbase-site.xml文件拷贝到上述工程的src/test/resources目录

加载配置
Configuration conf = new Configuration();<br />// conf.addResource("hbase-site-cluster.xml");//可以指定文件加载<br />conf = HBaseConfiguration.create(conf);
创建表
/**========创建表=========*/<br />HTableDescriptor desc = new HTableDescriptor("blog");<br />desc.addFamily(new HColumnDescriptor("article"));<br />desc.addFamily(new HColumnDescriptor("author"));<br />admin.createTable(desc );
增加记录
/**=========插入数据=========*/<br />Put put = new Put(Bytes.toBytes("1"));<br />put.add(Bytes.toBytes("article"), Bytes.toBytes("title"), Bytes.toBytes("Head First HBase"));<br />put.add(Bytes.toBytes("article"), Bytes.toBytes("content"), Bytes.toBytes("HBase is the Hadoop database. Use it when you need random, realtime read/write access to your Big Data."));<br />put.add(Bytes.toBytes("article"), Bytes.toBytes("tags"), Bytes.toBytes("Hadoop,HBase,NoSQL"));<br />put.add(Bytes.toBytes("author"), Bytes.toBytes("name"), Bytes.toBytes("hujinjun"));<br />put.add(Bytes.toBytes("author"), Bytes.toBytes("nickname"), Bytes.toBytes("一叶渡江"));<br />table.put(put);
知识点回顾:RowKey 和 ColumnName 是二进制值(Java 类型 byte[]),value 是一个字节数组(Java类型 byte[])
根据RowKey查询
/**=========根据rowkey<br />Get get = new Get(Bytes.toBytes("1")); 查询数据=========*/<br />Result result = table.get(get);<br />for(KeyValue kv :result.list()){<br />  System.out.println("family:" +Bytes.toString(kv.getFamily()));<br />  System.out.println("qualifier:" +Bytes.toString(kv.getQualifier()));<br />  System.out.println("value:" +Bytes.toString(kv.getValue()));<br />  System.out.println("Timestamp:" +kv.getTimestamp());<br />}
遍历查询与迭代
/**=========遍历查询=========*/<br />Scan scan = new Scan();<br />ResultScanner rs =null;<br />try {<br />  rs = table.getScanner(scan);<br />  for (Result r : rs) {<br />    for(KeyValue kv :r.list()){<br />      System.out.println("family:" +Bytes.toString(kv.getFamily()));<br />      System.out.println("qualifier:" +Bytes.toString(kv.getQualifier()));<br />    System.out.println("value:" +Bytes.toString(kv.getValue()));<br />    }<br />  }<br />} finally {<br />  rs.close();<br />}
知识点回顾:HTable的存储结构

可以看到上面代码我们用了两次for循环来遍历迭代。


更新练习
/**=========更新=========*/
//查询更新前的值<br />Get get2 = new Get(Bytes.toBytes("1"));<br />get2.addColumn(Bytes.toBytes("author"), Bytes.toBytes("nickname"));<br />assertThat(Bytes.toString(table.get(get2).list().get(0).getValue()),is("一叶渡江"));<br />//更新nickname为yedu<br />Put put2 = new Put(Bytes.toBytes("1")); :<br />put2.add(Bytes.toBytes("author"), Bytes.toBytes("nickname"), Bytes.toBytes("yedu"));<br />table.put(put2);<br />//查询更新结果<br />Get get3 = new Get(Bytes.toBytes("1"));<br />get3.addColumn(Bytes.toBytes("author"), Bytes.toBytes("nickname"));<br />assertThat(Bytes.toString(table.get(get3).list().get(0).getValue()),is("yedu"));<br />//查询nickname的多个(本示例为2个)版本值<br />Get get4 = new Get(Bytes.toBytes("1"));<br />get4.addColumn(Bytes.toBytes("author"), Bytes.toBytes("nickname"));<br />get4.setMaxVersions(2);<br />List results = table.get(get4).list();<br />assertThat(results.size(),is(2));<br />assertThat(Bytes.toString(results.get(0).getValue()),is("yedu"));<br />assertThat(Bytes.toString(results.get(1).getValue()),is("一叶渡江"));
删除记录
/**=========删除记录=========*/<br />//删除指定column<br />Delete deleteColumn = new Delete(Bytes.toBytes("1"));<br />deleteColumn.deleteColumns(Bytes.toBytes("author"),Bytes.toBytes("nickname"));<br />table.delete(deleteColumn);<br />assertThat( table.get(get4).list(),nullValue());<br />//删除所有column<br />Delete deleteAll = new Delete(Bytes.toBytes("1"));<br />table.delete(deleteAll);<br />assertThat(table.getScanner(scan).next(),nullValue());
删除表
/**=========删除表=========*/<br />admin.disableTable("blog");<br />admin.deleteTable("blog");<br />assertThat(admin.tableExists("blog"),is(false));
完整代码示例
public class HBase {<br />public static void main(String[] args) throws IOException {<br />Configuration conf = new Configuration();<br />// conf.addResource("hbase-site-cluster.xml");//指定文件加载<br />conf = HBaseConfiguration.create(conf);<br />HBaseAdmin admin = new HBaseAdmin(conf);//HBaseAdmin负责跟表相关的操作如create,drop等<br />HTable table = new HTable(conf, Bytes.toBytes("blog"));//HTabel负责跟记录相关的操作如增删改查等<br />/**========创建表=========*/<br />HTableDescriptor desc = new HTableDescriptor("blog");<br />desc.addFamily(new HColumnDescriptor("article"));<br />desc.addFamily(new HColumnDescriptor("author"));<br />admin.createTable(desc );<br />/**=========插入数据=========*/<br />Put put = new Put(Bytes.toBytes("1"));<br />put.add(Bytes.toBytes("article"), Bytes.toBytes("title"), Bytes.toBytes("Head First HBase"));<br />put.add(Bytes.toBytes("article"), Bytes.toBytes("content"), Bytes.toBytes("HBase is the Hadoop database. Use it when you need random, realtime read/write access to your Big Data."));<br />put.add(Bytes.toBytes("article"), Bytes.toBytes("tags"), Bytes.toBytes("Hadoop,HBase,NoSQL"));<br />put.add(Bytes.toBytes("author"), Bytes.toBytes("name"), Bytes.toBytes("hujinjun"));<br />put.add(Bytes.toBytes("author"), Bytes.toBytes("nickname"), Bytes.toBytes("一叶渡江"));<br />table.put(put);<br />/**=========根据rowkey<br />Get get = new Get(Bytes.toBytes("1")); 查询数据=========*/<br />Result result = table.get(get);<br />  for(KeyValue kv :result.list()){<br />  System.out.println("family:" +Bytes.toString(kv.getFamily()));<br />  System.out.println("qualifier:" +Bytes.toString(kv.getQualifier()));<br />  System.out.println("value:" +Bytes.toString(kv.getValue()));<br />  System.out.println("Timestamp:" +kv.getTimestamp());<br />}<br />/**=========遍历查询=========*/<br />Scan scan = new Scan();<br />ResultScanner rs =null;<br />try {<br />  rs = table.getScanner(scan);<br />  for (Result r : rs) {<br />    for(KeyValue kv :r.list()){<br />      System.out.println("family:" +Bytes.toString(kv.getFamily()));<br />      System.out.println("qualifier:" +Bytes.toString(kv.getQualifier()));<br />      System.out.println("value:" +Bytes.toString(kv.getValue()));<br />    }<br />  }<br />} finally {<br />  rs.close();<br />}<br />/**=========更新=========*/<br />//查询更新前的值<br />Get get2 = new Get(Bytes.toBytes("1"));<br />get2.addColumn(Bytes.toBytes("author"), Bytes.toBytes("nickname"));<br />assertThat(Bytes.toString(table.get(get2).list().get(0).getValue()),is("一叶渡江"));<br />//更新nickname为yedu<br />Put put2 = new Put(Bytes.toBytes("1")); :<br />put2.add(Bytes.toBytes("author"), Bytes.toBytes("nickname"), Bytes.toBytes("yedu"));<br />table.put(put2);<br />//查询更新结果<br />Get get3 = new Get(Bytes.toBytes("1"));<br />get3.addColumn(Bytes.toBytes("author"), Bytes.toBytes("nickname"));<br />assertThat(Bytes.toString(table.get(get3).list().get(0).getValue()),is("yedu"));<br />//查询nickname的多个(本示例为2个)版本值<br />Get get4 = new Get(Bytes.toBytes("1"));<br />get4.addColumn(Bytes.toBytes("author"), Bytes.toBytes("nickname"));<br />get4.setMaxVersions(2);<br />List results = table.get(get4).list();<br />assertThat(results.size(),is(2));<br />assertThat(Bytes.toString(results.get(0).getValue()),is("yedu"));<br />assertThat(Bytes.toString(results.get(1).getValue()),is("一叶渡江"));<br />/**=========删除记录=========*/<br />//删除指定column<br />Delete deleteColumn = new Delete(Bytes.toBytes("1"));<br />deleteColumn.deleteColumns(Bytes.toBytes("author"),Bytes.toBytes("nickname"));<br />table.delete(deleteColumn);<br />assertThat( table.get(get4).list(),nullValue());<br />//删除所有column<br />Delete deleteAll = new Delete(Bytes.toBytes("1"));<br />table.delete(deleteAll);<br />assertThat(table.getScanner(scan).next(),nullValue());<br />/**=========删除表=========*/<br />admin.disableTable("blog");<br />admin.deleteTable("blog");<br />assertThat(admin.tableExists("blog"),is(false));<br />}<br />}

小结
本文介绍了Java api创建、删除表,及记录的增删改查,还是以练习为主,也可作为速查手册(比如看如何迭代查询结果),对HBase的基本概念及操作就介绍到这里,后面将介绍如何使用MapReduce对HBase数据进行分布式计算。

相关推荐