2.3-2.6 HBase java API
一、get 、put、delete、scan
1、代码
package com.beifeng.senior.hadoop.hbase; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IOUtils; /**
* CRUD operations
*
* @author root
*
*/ public class HBaseOperation { public static HTable getHTableByTableName(String tableName) throws Exception {
// get instance of default configuration
Configuration configuration = HBaseConfiguration.create(); // get table instance
HTable table = new HTable(configuration, tableName); return table;
} public void getData() throws Exception {
String tableName = "user"; // default.user HTable table = getHTableByTableName(tableName); //create get with rowkey
Get get = new Get(Bytes.toBytes("10002")); //add column
get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age")); //get data
Result result = table.get(get); //key: rowkey + cf + c + version
//value: value
for(Cell cell : result.rawCells()){
System.out.println(
Bytes.toString(CellUtil.cloneFamily(cell)) + ":" //
+ Bytes.toString(CellUtil.cloneQualifier(cell)) + "->" //
+ Bytes.toString(CellUtil.cloneValue(cell)));
} //table close
table.close();
} /**
* 建议:
* tablename & column family ->常量, HbaseTableContent
*
* Map<String, Object>
*
* @throws Exception
*/ public void putData() throws Exception {
String tableName = "user"; // default.user HTable table = getHTableByTableName(tableName); Put put = new Put(Bytes.toBytes("10004")); //add a column with value
put.add(Bytes.toBytes("info"),
Bytes.toBytes("name"),
Bytes.toBytes("zhaoliu")); put.add(Bytes.toBytes("info"),
Bytes.toBytes("age"),
Bytes.toBytes("25")); put.add(Bytes.toBytes("info"),
Bytes.toBytes("address"),
Bytes.toBytes("shanghai")); table.put(put); table.close();
} public void deleteData() throws Exception {
String tableName = "user"; // default.user HTable table = getHTableByTableName(tableName); Delete delete = new Delete(Bytes.toBytes("10004")); /*删除单个数据
delete.deleteColumn(Bytes.toBytes("info"),
Bytes.toBytes("address"));
*/ /*删除整个列簇*/
delete.deleteFamily(Bytes.toBytes("info")); table.delete(delete); table.close();
} public static void main(String[] args) throws Exception {
String tableName = "user"; // default.user HTable table = null;
ResultScanner resultScanner = null; try {
table = getHTableByTableName(tableName); /*全表扫描
Scan scan = new Scan();
*/ //指定Rowkey范围
Scan scan = new Scan(); scan.setStartRow(Bytes.toBytes("10002"));
scan.setStopRow(Bytes.toBytes("10004")); resultScanner = table.getScanner(scan); for(Result result : resultScanner) {
System.out.println(Bytes.toString(result.getRow()));
//System.out.println(result); for(Cell cell : result.rawCells()){
System.out.println(
Bytes.toString(CellUtil.cloneFamily(cell)) + ":" //
+ Bytes.toString(CellUtil.cloneQualifier(cell)) + "->" //
+ Bytes.toString(CellUtil.cloneValue(cell)));
} System.out.println("--------------------");
} } catch (Exception e) {
e.printStackTrace();
}finally{
IOUtils.closeStream(resultScanner);
IOUtils.closeStream(table);
} } }
HBase命令:
hbase(main):006:0> flush 'table name' //刷数据 hbase(main):007:0> compact 'table name' //合并数据
2.3-2.6 HBase java API的更多相关文章
- 【Hbase学习之三】Hbase Java API
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-2.6.5 hbase-0.98.12.1-h ...
- hbase java api样例(版本1.3.1,新API)
hbase版本:1.3.1 目的:HBase新API的使用方法. 尝试并验证了如下几种java api的使用方法. 1.创建表 2.创建表(预分区) 3.单条插入 4.批量插入 5.批量插入(客户端缓 ...
- hbase java API跟新数据,创建表
package hbaseCURD; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import o ...
- HBase 学习之路(六)——HBase Java API 的基本使用
一.简述 截至到目前(2019.04),HBase 有两个主要的版本,分别是1.x 和 2.x ,两个版本的Java API有所不同,1.x 中某些方法在2.x中被标识为@deprecated过时.所 ...
- HBase 系列(六)——HBase Java API 的基本使用
一.简述 截至到目前 (2019.04),HBase 有两个主要的版本,分别是 1.x 和 2.x ,两个版本的 Java API 有所不同,1.x 中某些方法在 2.x 中被标识为 @depreca ...
- Hbase Java API详解
HBase是Hadoop的数据库,能够对大数据提供随机.实时读写访问.他是开源的,分布式的,多版本的,面向列的,存储模型. 在讲解的时候我首先给大家讲解一下HBase的整体结构,如下图: HBase ...
- Hbase Java API程序设计步骤
http://www.it165.net/admin/html/201407/3390.html 步骤1:创建一个Configuration对象 包含了客户端链接Hbase服务所需的全部信息: zoo ...
- HBase Java API使用(一)
前言 1. 创建表:(由master完成) 首先需要获取master地址(master启动时会将地址告诉zookeeper)因而客户端首先会访问zookeeper获取master的地址 client和 ...
- Hbase(六) hbase Java API
一. 几个主要 Hbase API 类和数据模型之间的对应关系: 1. HBaseAdmin关系: org.apache.hadoop.hbase.client.HBaseAdmin作用:提供了一个接 ...
- Hbase Java API包括协处理器统计行数
package com.zy; import java.io.IOException; import org.apache.commons.lang.time.StopWatch; import or ...
随机推荐
- iLBC简要介绍
iLBC(internet lowbitrate codec):是全球著名语音引擎提供商Global IP Sound开发,它是低比特率的编码解码器,提供在丢包时具有的强大的健壮性.iLBC 提供的语 ...
- 小贝_mysql主从复制作用以及案例
mysql主从复制 简要: 一.mysql用户授权 二.mysql bin-log日志 三.mysql主从复制 一.mysql用户授权 1.命令 2.作用:进行权限控制 3.样例: (备注: 同意 ...
- 用算法求N(N>=3)之内素数的个数
首先.我们谈一下素数的定义.什么是素数?除了1和它本身外,不能被其它自然数整除(除0以外)的数 称之为素数(质数):否则称为合数. 依据素数的定义,在解决问题上,一開始我想到的方法是从3到N之间每一个 ...
- Apache Server与多个独立Tomcat集成
取经自http://www.ramkitech.com/2012/03/virtual-host-apache-httpd-server-tomcat.html 继续干Tomcat和Apache Se ...
- 安卓版本6.0打开uiautomator报错
可能是appium打开了,被占用:或者是找不到手机
- 兼容最新firefox、chrome和IE的javascript图片预览实现代码
这篇文章主要介绍了兼容最新firefox.chrome和IE的javascript图片预览实现代码,测试了浏览器firefox6.firefox12.chrome 25.0.1364.172 m.IE ...
- 红黑树深入剖析及Java实现(转自知乎美团点评技术团队)
作者:美团点评技术团队 链接:https://zhuanlan.zhihu.com/p/24367771 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 红黑树是平衡 ...
- wepy原理研究
像VUE一样写微信小程序-深入研究wepy框架 https://zhuanlan.zhihu.com/p/28700207 wepy原理研究 虽然wepy提升了小程序开发体验,但毕竟最终要运行在小程序 ...
- PPTP&L2TP&PPPOE client and server configure
一. PPPOE 1. server(参考http://laibulai.iteye.com/blog/1171898) (1)安装rp-pppoe:yum install rp-pppoe (2)配 ...
- HZNU 2154 ldh发奖金【字符串】
题目链接 http://acm.hznu.edu.cn/OJ/problem.php?id=2154 思路 先判断不能拆分的情况 以为需要拆分成两个正整数 所以我们可以知道 只有个位的数字 是不能够拆 ...