HBase应用笔记:通过Java Api与HBase交互(转自 Taobao QA Team)
http://blog.sina.com.cn/s/blog_66474b1601017hvx.html
http://www.cnblogs.com/eprsoft/archive/2012/10/22/2734133.html
引言
HBase提供了Java Api的访问接口,掌握这个就跟Java应用使用RDBMS时需要JDBC一样重要,本文将继续前两篇文章中blog表的示例,介绍常用的Api。
练习前的准备工作
创建一个Maven工程,加入以下依赖:
org.apache.hbase
hbase
0.90.2
如果你的Maven库里还没有hbase,还需要配置下repository
cloudera
https://repository.cloudera.com/content/groups/public
确保HBase环境已启动且能连接到,将HBase环境的hbase-site.xml文件拷贝到上述工程的src/test/resources目录
加载配置
Configuration conf = new Configuration(); // conf.addResource("hbase-site-cluster.xml");//可以指定文件加载 conf = HBaseConfiguration.create(conf);
创建表
HTableDescriptor desc = new HTableDescriptor("blog"); desc.addFamily(newHColumnDescriptor("article")); desc.addFamily(new HColumnDescriptor("author")); admin.createTable(desc );
增加记录
Put put = new Put(Bytes.toBytes("1")); put.add(Bytes.toBytes("article"), Bytes.toBytes("title"), Bytes.toBytes("Head First HBase")); 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.")); put.add(Bytes.toBytes("article"), Bytes.toBytes("tags"), Bytes.toBytes("Hadoop,HBase,NoSQL")); put.add(Bytes.toBytes("author"), Bytes.toBytes("name"), Bytes.toBytes("hujinjun")); put.add(Bytes.toBytes("author"), Bytes.toBytes("nickname"), Bytes.toBytes("一叶渡江")); table.put(put);
知识点回顾:RowKey 和 ColumnName 是二进制值(Java 类型 byte[]),value 是一个字节数组(Java类型 byte[])
根据RowKey查询
Get get = new Get(Bytes.toBytes("1")); Result result = table.get(get); for(KeyValue kv :result.list()){ System.out.println("family:" +Bytes.toString(kv.getFamily())); System.out.println("qualifier:" +Bytes.toString(kv.getQualifier())); System.out.println("value:" +Bytes.toString(kv.getValue())); System.out.println("Timestamp:" +kv.getTimestamp()); }
遍历查询与迭代
Scan scan = new Scan(); ResultScanner rs =null; try { rs = table.getScanner(scan); for(Result r : rs) { for(KeyValue kv :r.list()){ System.out.println("family:"+Bytes.toString(kv.getFamily())); System.out.println("qualifier:"+Bytes.toString(kv.getQualifier())); System.out.println("value:"+Bytes.toString(kv.getValue())); } } } finally { rs.close(); }
可以看到上面代码我们用了两次for循环来遍历迭代。
更新练习
//查询更新前的值
Get get2 = new Get(Bytes.toBytes("1")); get2.addColumn(Bytes.toBytes("author"), Bytes.toBytes("nickname")); assertThat(Bytes.toString(table.get(get2).list().get(0).getValue()),is("一叶渡江"));
//更新nickname为yedu
Put put2 = new Put(Bytes.toBytes("1")); : put2.add(Bytes.toBytes("author"), Bytes.toBytes("nickname"), Bytes.toBytes("yedu")); table.put(put2);
//查询更新结果
Get get3 = new Get(Bytes.toBytes("1")); get3.addColumn(Bytes.toBytes("author"), Bytes.toBytes("nickname")); assertThat(Bytes.toString(table.get(get3).list().get(0).getValue()),is("yedu"));
//查询nickname的多个(本示例为2个)版本值
Get get4 = new Get(Bytes.toBytes("1")); get4.addColumn(Bytes.toBytes("author"), Bytes.toBytes("nickname")); get4.setMaxVersions(2); List results = table.get(get4).list(); assertThat(results.size(),is(2)); assertThat(Bytes.toString(results.get(0).getValue()),is("yedu")); assertThat(Bytes.toString(results.get(1).getValue()),is("一叶渡江"));
删除记录
//删除指定column
Delete deleteColumn = new Delete(Bytes.toBytes("1")); deleteColumn.deleteColumns(Bytes.toBytes("author"),Bytes.toBytes("nickname")); table.delete(deleteColumn); assertThat( table.get(get4).list(),nullValue());
//删除所有column
Delete deleteAll = new Delete(Bytes.toBytes("1")); table.delete(deleteAll); assertThat(table.getScanner(scan).next(),nullValue());
删除表
admin.disableTable("blog"); admin.deleteTable("blog"); assertThat(admin.tableExists("blog"),is(false));
完整代码示例
public class HBase { public static void main(String[] args) throws IOException { Configuration conf = new Configuration(); // conf.addResource("hbase-site-cluster.xml");//指定文件加载 conf = HBaseConfiguration.create(conf); HBaseAdmin admin =new HBaseAdmin(conf);//HBaseAdmin负责跟表相关的操作如create,drop等 HTable table = newHTable(conf, Bytes.toBytes("blog"));//HTabel负责跟记录相关的操作如增删改查等HTableDescriptor desc = new HTableDescriptor("blog"); desc.addFamily(newHColumnDescriptor("article")); desc.addFamily(new HColumnDescriptor("author")); admin.createTable(desc ); Put put = new Put(Bytes.toBytes("1")); put.add(Bytes.toBytes("article"), Bytes.toBytes("title"), Bytes.toBytes("Head First HBase")); 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.")); put.add(Bytes.toBytes("article"), Bytes.toBytes("tags"), Bytes.toBytes("Hadoop,HBase,NoSQL")); put.add(Bytes.toBytes("author"), Bytes.toBytes("name"), Bytes.toBytes("hujinjun")); put.add(Bytes.toBytes("author"), Bytes.toBytes("nickname"), Bytes.toBytes("一叶渡江")); table.put(put); Result result = table.get(get); for(KeyValue kv :result.list()){ System.out.println("family:" +Bytes.toString(kv.getFamily())); System.out.println("qualifier:" +Bytes.toString(kv.getQualifier())); System.out.println("value:" +Bytes.toString(kv.getValue())); System.out.println("Timestamp:" +kv.getTimestamp()); } Scan scan = new Scan(); ResultScanner rs =null; try { rs = table.getScanner(scan); for (Result r : rs) {for(KeyValue kv :r.list()){ System.out.println("family:"+Bytes.toString(kv.getFamily())); System.out.println("qualifier:"+Bytes.toString(kv.getQualifier())); System.out.println("value:"+Bytes.toString(kv.getValue())); } } } finally { rs.close(); } //查询更新前的值 Get get2 = new Get(Bytes.toBytes("1")); get2.addColumn(Bytes.toBytes("author"), Bytes.toBytes("nickname")); assertThat(Bytes.toString(table.get(get2).list().get(0).getValue()),is("一叶渡江")); //更新nickname为yedu Put put2 = new Put(Bytes.toBytes("1")); : put2.add(Bytes.toBytes("author"), Bytes.toBytes("nickname"), Bytes.toBytes("yedu")); table.put(put2); //查询更新结果 Get get3 = new Get(Bytes.toBytes("1")); get3.addColumn(Bytes.toBytes("author"), Bytes.toBytes("nickname")); assertThat(Bytes.toString(table.get(get3).list().get(0).getValue()),is("yedu")); //查询nickname的多个(本示例为2个)版本值 Get get4 = new Get(Bytes.toBytes("1")); get4.addColumn(Bytes.toBytes("author"), Bytes.toBytes("nickname")); get4.setMaxVersions(2); List results = table.get(get4).list(); assertThat(results.size(),is(2)); assertThat(Bytes.toString(results.get(0).getValue()),is("yedu")); assertThat(Bytes.toString(results.get(1).getValue()),is("一叶渡江")); //删除指定columnDelete deleteColumn = new Delete(Bytes.toBytes("1")); deleteColumn.deleteColumns(Bytes.toBytes("author"),Bytes.toBytes("nickname")); table.delete(deleteColumn); assertThat( table.get(get4).list(),nullValue()); //删除所有column Delete deleteAll = new Delete(Bytes.toBytes("1")); table.delete(deleteAll); assertThat(table.getScanner(scan).next(),nullValue()); admin.disableTable("blog"); admin.deleteTable("blog"); assertThat(admin.tableExists("blog"),is(false)); } }
http://hi.baidu.com/cpuramdisk/item/007bb0d35bc7d9322b35c723
HBase之Java API
在使用Java API时,Client端需要知道HBase的配置环境,如存储地址,zookeeper等信息。这些信息通过Configuration对象来封装,可通过如下代码构建该对象
Configuration config=HBaseConfiguration.create();
在调用HBaseConfiguration.create()方法时,HBase首先会在classpath下查找hbase-site.xml文件,将里面的信息解析出来封装到Configuration对象中,如果hbase-site.xml文件不存在,则使用默认的hbase-core.xml文件。
除了将hbase-site.xml放到classpath下,开发人员还可通过config.set(name, value)方法来手工构建Configuration对象。
Configuration.set(String name, String value)
2.HBaseAdmin
HBaseAdmin用于创建数据库表格,并管理表格的元数据信息,通过如下方法构建
HBaseAdmin admin=new HBaseAdmin(config);
常用方法:
addColumn(tableName,column):为表格添加栏位
deleteColumn(tableName,column):删除指定栏位
balanceSwitch(boolean):是否启用负载均衡
createTable(HTableDescriptor desc):创建表格
deleteTable(tableName):删除表格
tableExists(tableName):判断表格是否存在
示例:创建test表格,并为其指定columnFamily为cf
[java]view plaincopyprint?
- HBaseAdmin admin=new HBaseAdmin(config);
- If(!admin.tableExists(“test”)){
- HTableDescriptor tableDesc=new HTableDescriptor(“test”);
- HColumnDescriptor cf=new HColumnDescriptor(“cf”);
- tableDesc.addFamily(cf);
- admin.createTable(tableDesc);
- }
HBaseAdmin admin=new HBaseAdmin(config); If(!admin.tableExists(“test”)){ HTableDescriptor tableDesc=new HTableDescriptor(“test”); HColumnDescriptor cf=new HColumnDescriptor(“cf”); tableDesc.addFamily(cf); admin.createTable(tableDesc);}
3.HTable
在HBase中,HTable封装表格对象,对表格的增删改查操作主要通过它来完成,构造方法如下:
HTable table=new HTable(config,tableName);
在构建多个HTable对象时,HBase推荐所有的HTable使用同一个Configuration。这样,HTable之间便可共享HConnection对象、zookeeper信息以及Region地址的缓存信息。
示例1:Get操作
[java]view plaincopyprint?
- Get get=new Get(rowKey);
- Result res=table.get(get);
Get get=new Get(rowKey); Result res=table.get(get);示例2:Put操作
[java]view plaincopyprint?
- Put put=new Put(rowKey);
- put.add(columnFamily,column,value);
- table.put(put);
Put put=new Put(rowKey); put.add(columnFamily,column,value);table.put(put); 注:在HBase中,实体的新增和更新都是通过Put操作来实现。
示例3:Delete操作
[java]view plaincopyprint?
- Delete delete=new Delete();
- table.delete(delete);
Delete delete=new Delete(); table.delete(delete);示例4:Scan操作
[java]view plaincopyprint?
- Scan scan=new Scan( );
- scan.addColumn(columnFamily,column);//指定查询要返回的column
- SingleColumnValueFilter filter=new SingleColumnValueFilter(
- columnFamily,column,//指定要过滤的column
- CompareOp.EQUAL,value//指定过滤条件
- );
- //更多的过滤器信息请查看org.apache.hadoop.hbase.filter包
- scan.setFilter(filter);//为查询指定过滤器
- ResultScanner scanner=table.getScanner(scan);//执行扫描查找
- Iterator<Result> res=scanner.iterator( );//返回查询遍历器
HBase应用笔记:通过Java Api与HBase交互(转自 Taobao QA Team)相关推荐
- 6 HBase java API访问HBase数据库
HBase java API访问HBase数据库 package com.hunan.hbase_options;import org.apache.hadoop.conf.Configuration ...
- 使用 Java API 操作 HBase
使用 Java API 操作 HBase 数据库,就类似HBase Shell,本质上一个是Java 代码,一个是Shell 命令.(hadoop 的文件系统莫不如此,可用Java API 的方式操作 ...
- HBase总结(十二)Java API 与HBase交互实例
HBase提供了Java Api的访问接口,掌握这个就跟Java应用使用RDBMS时需要JDBC一样重要 [html] view plaincopy import java.io.IOExceptio ...
- HBase 6、用Phoenix Java api操作HBase
开发环境准备:eclipse3.5.jdk1.7.window8.hadoop2.2.0.hbase0.98.0.2.phoenix4.3.0 1.从集群拷贝以下文件:core-site.xml.hb ...
- 通过Java Api与HBase交互(转)
HBase提供了Java Api的访问接口,掌握这个就跟Java应用使用RDBMS时需要JDBC一样重要,本文将继续前两篇文章中blog表的示例,介绍常用的Api. import java.io.IO ...
- hbase开发,hbase表操作及其java api实现
] 开发环境 hadoop: hadoop-1.1.2 hbase: hbase-0.94.11-security eclipse:Juno Service Release 2 配置Eclipse 通 ...
- hbase应用场景 java_Hbase Java API 使用
1.配置环境 1)eclipse和jdk的下载安装配置 2)加载相关xml文件 pom.xml添加maven依赖 添加配置文件至新创建的resources中 找到hbase zookeeper ...
- Java Api 操作 Hbase
文章目录 1.需求 2.思路 2.1 建立远程Hbase集群连接 -- HbaseInit.java 2.2 创建表结构--CreateTable.java 2.3 插入数据 -- PutRow.ja ...
- Hase Java API 和 Hbase Scala API
Table of Contents Java 版的 Hbase 工具类,配置 zookeeper 的地址 建表操作 修改表结构 列出所有表 删表 写表 根据 rowKey 删数据 查表,所有数据 根据 ...
最新文章
- C++实现九九乘法表
- Docker映像和容器之间有什么区别?
- ADN新开了云计算Cloud和移动计算Mobile相关技术的博客
- 地线与接地螺丝_电气接地规范与接地的各项参数
- Prototype(原型)--对象创建模式
- python-字母与ascii码的转换-利用数字转字母-利用字母转数字
- java swing还有人用吗_Java不是自动管理内存吗,怎么还有内存泄漏?
- 生日在java中怎么写_如何用java写代码:输入自己的生日,距离今天已过多少天,或者还有...
- vue -- router路由跳转错误 , NavigationDuplicated
- gVerify:前端验证码插件
- 小石坝第一次月赛:A
- 2014年百度之星程序设计大赛 - 资格赛
- 拼多多进军社区团购 店宝宝:巨头竞争加剧
- Audio:Android-TinyAlsa架构 Mixer API
- bismark判断甲基化的比对原理
- 苦难是人生最大的财富
- debian linux 关闭防火墙,debian怎么样关闭防火墙
- 【转载】Cygwin安装
- 【php】PHP数据库访问
- matlab fsolve函数 误差,matlab关于fsolve函数的运用出现的问题
热门文章
- iOS之深入解析单例的实现和销毁的底层原理
- 中国大学MOOC 编译原理 第6讲测验
- 《算法竞赛入门经典》习题4-2 正方形 (Squares,ACM,ICPC World Finals 1990,UVa201)——仅提供大体方法
- 【工业控制】激光跟踪仪概述
- 【Qt】数据库实战之QSqlQueryModel
- 【Tools】GitBook入门教程
- 【Ubuntu】 Ubuntu18.04修改主机名
- 【Centos】Centos7.5取消自动锁屏功能
- 【Libevent】Libevent学习笔记(二):创建event_base
- qt messagebox退出程序_[Qt] Mac平台安装最新版qt的方法(源码编译) - 刘傲天