HBase提供了Java Api的访问接口
来源:互联网 发布:mybatis 打印完整sql 编辑:程序博客网 时间:2024/05/18 15:25
引言
HBase提供了Java Api的访问接口,掌握这个就跟Java应用使用RDBMS时需要JDBC一样重要,本文将继续前两篇文章中blog表的示例,介绍常用的Api。
练习前的准备工作
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase</artifactId>
<version>0.90.2</version>
</dependency>
如果你的Maven库里还没有hbase,还需要配置下repository<repositories>
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/content/groups/public</url>
</repository>
</repositories>
加载配置Configuration conf = new Configuration();
// conf.addResource("hbase-site-cluster.xml");//可以指定文件加载
conf = HBaseConfiguration.create(conf);
创建表/**========创建表=========*/
HTableDescriptor desc = new HTableDescriptor("blog");
desc.addFamily(new HColumnDescriptor("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查询/**=========根据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();
}
知识点回顾:HTable的存储结构
可以看到上面代码我们用了两次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 = new HTable(conf, Bytes.toBytes("blog"));//HTabel负责跟记录相关的操作如增删改查等
/**========创建表=========*/
HTableDescriptor desc = new HTableDescriptor("blog");
desc.addFamily(new HColumnDescriptor("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
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();
}
/**=========更新=========*/
//查询更新前的值
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));
}
}
小结
本文介绍了Java api创建、删除表,及记录的增删改查,还是以练习为主,也可作为速查手册(比如看如何迭代查询结果),对HBase的基本概念及操作就介绍到这里,后面将介绍如何使用MapReduce对HBase数据进行分布式计算。
- HBase提供了Java Api的访问接口
- Java 访问 Hbase 的API
- Base提供了Java Api的访问接口,掌握这个就跟Java应用使用RDBMS时需要JDBC一样重要[
- java api 访问 Hbase
- JAVA API接口提供注意事项
- jdbc----java提供的访问数据库的接口【三】
- 解决Java API不能远程访问HBase的问题
- Hbase学习笔记:使用hbase提供的API
- HBase 的Java API
- HBase的Java API
- Hbase的java接口
- Hbase框架原理及相关的知识点理解、Hbase访问MapReduce、Hbase访问Java API、Hbase shell及Hbase性能优化总结
- Hbase访问方式之Java API
- Hbase访问方式之Java API
- java api访问 hbase——demo
- hadoop学习笔记--5.HDFS的java api接口访问
- JAVA提供的SOCKET API
- hbase-shell + hbase的java api
- 2013 ACM/ICPC Asia Regional Nanjing Online( hdu 4749)
- PHP写入txt文件换行
- JBOSS中配置JNDI数据源
- php数据库操作类
- YII 框架 第八天(4) 显示sql语句执行时间
- HBase提供了Java Api的访问接口
- View和其subview的半透明问题
- centos 6.2 编译cowpatty-4.6遇到的问题
- ISF 视频下载地址(持续更新)
- 滚动到页顶
- ECMALL get_options 生成下拉菜单
- 遍历(广度优先)windows系统某一目录
- 将Excel导入到虚拟DataTable中
- 归并排序求逆序数java