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

1.Configuration

在使用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?

  1. HBaseAdmin admin=new HBaseAdmin(config);
  2. If(!admin.tableExists(“test”)){
  3. HTableDescriptor tableDesc=new HTableDescriptor(“test”);
  4. HColumnDescriptor cf=new HColumnDescriptor(“cf”);
  5. tableDesc.addFamily(cf);
  6. admin.createTable(tableDesc);
  7. }

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?

  1. Get get=new Get(rowKey);
  2. Result res=table.get(get);

Get get=new Get(rowKey); Result res=table.get(get);示例2:Put操作

[java]view plaincopyprint?

  1. Put put=new Put(rowKey);
  2. put.add(columnFamily,column,value);
  3. table.put(put);

Put put=new Put(rowKey); put.add(columnFamily,column,value);table.put(put); 注:在HBase中,实体的新增和更新都是通过Put操作来实现。

示例3:Delete操作

[java]view plaincopyprint?

  1. Delete delete=new Delete();
  2. table.delete(delete);

Delete delete=new Delete(); table.delete(delete);示例4:Scan操作

[java]view plaincopyprint?

  1. Scan scan=new Scan( );
  2. scan.addColumn(columnFamily,column);//指定查询要返回的column
  3. SingleColumnValueFilter filter=new SingleColumnValueFilter(
  4. columnFamily,column,//指定要过滤的column
  5. CompareOp.EQUAL,value//指定过滤条件
  6. );
  7. //更多的过滤器信息请查看org.apache.hadoop.hbase.filter包
  8. scan.setFilter(filter);//为查询指定过滤器
  9. ResultScanner scanner=table.getScanner(scan);//执行扫描查找
  10. Iterator<Result> res=scanner.iterator( );//返回查询遍历器

HBase应用笔记:通过Java Api与HBase交互(转自 Taobao QA Team)相关推荐

  1. 6 HBase java API访问HBase数据库

    HBase java API访问HBase数据库 package com.hunan.hbase_options;import org.apache.hadoop.conf.Configuration ...

  2. 使用 Java API 操作 HBase

    使用 Java API 操作 HBase 数据库,就类似HBase Shell,本质上一个是Java 代码,一个是Shell 命令.(hadoop 的文件系统莫不如此,可用Java API 的方式操作 ...

  3. HBase总结(十二)Java API 与HBase交互实例

    HBase提供了Java Api的访问接口,掌握这个就跟Java应用使用RDBMS时需要JDBC一样重要 [html] view plaincopy import java.io.IOExceptio ...

  4. 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 ...

  5. 通过Java Api与HBase交互(转)

    HBase提供了Java Api的访问接口,掌握这个就跟Java应用使用RDBMS时需要JDBC一样重要,本文将继续前两篇文章中blog表的示例,介绍常用的Api. import java.io.IO ...

  6. hbase开发,hbase表操作及其java api实现

    ] 开发环境 hadoop: hadoop-1.1.2 hbase: hbase-0.94.11-security eclipse:Juno Service Release 2 配置Eclipse 通 ...

  7. hbase应用场景 java_Hbase Java API 使用

    1.配置环境 1)eclipse和jdk的下载安装配置 2)加载相关xml文件 pom.xml添加maven依赖 添加配置文件至新创建的resources中   找到hbase  zookeeper  ...

  8. Java Api 操作 Hbase

    文章目录 1.需求 2.思路 2.1 建立远程Hbase集群连接 -- HbaseInit.java 2.2 创建表结构--CreateTable.java 2.3 插入数据 -- PutRow.ja ...

  9. Hase Java API 和 Hbase Scala API

    Table of Contents Java 版的 Hbase 工具类,配置 zookeeper 的地址 建表操作 修改表结构 列出所有表 删表 写表 根据 rowKey 删数据 查表,所有数据 根据 ...

最新文章

  1. C++实现九九乘法表
  2. Docker映像和容器之间有什么区别?
  3. ADN新开了云计算Cloud和移动计算Mobile相关技术的博客
  4. 地线与接地螺丝_电气接地规范与接地的各项参数
  5. Prototype(原型)--对象创建模式
  6. python-字母与ascii码的转换-利用数字转字母-利用字母转数字
  7. java swing还有人用吗_Java不是自动管理内存吗,怎么还有内存泄漏?
  8. 生日在java中怎么写_如何用java写代码:输入自己的生日,距离今天已过多少天,或者还有...
  9. vue -- router路由跳转错误 , NavigationDuplicated
  10. gVerify:前端验证码插件
  11. 小石坝第一次月赛:A
  12. 2014年百度之星程序设计大赛 - 资格赛
  13. 拼多多进军社区团购 店宝宝:巨头竞争加剧
  14. Audio:Android-TinyAlsa架构 Mixer API
  15. bismark判断甲基化的比对原理
  16. 苦难是人生最大的财富
  17. debian linux 关闭防火墙,debian怎么样关闭防火墙
  18. 【转载】Cygwin安装
  19. 【php】PHP数据库访问
  20. matlab fsolve函数 误差,matlab关于fsolve函数的运用出现的问题

热门文章

  1. iOS之深入解析单例的实现和销毁的底层原理
  2. 中国大学MOOC 编译原理 第6讲测验
  3. 《算法竞赛入门经典》习题4-2 正方形 (Squares,ACM,ICPC World Finals 1990,UVa201)——仅提供大体方法
  4. 【工业控制】激光跟踪仪概述
  5. 【Qt】数据库实战之QSqlQueryModel
  6. 【Tools】GitBook入门教程
  7. 【Ubuntu】 Ubuntu18.04修改主机名
  8. 【Centos】Centos7.5取消自动锁屏功能
  9. 【Libevent】Libevent学习笔记(二):创建event_base
  10. qt messagebox退出程序_[Qt] Mac平台安装最新版qt的方法(源码编译) - 刘傲天