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 ...
随机推荐
- Node.js知识点学习
Node.js知识点学习 一.基本概念 Node.js,或者 Node,是一个可以让 JavaScript 运行在服务器端的平台.可以说,Node.js开创了javascript模块化开发的先河,早期 ...
- UVA1422-Processor(二分法+优先队列)
option=com_onlinejudge&Itemid=8&category=512&page=show_problem&problem=4168"> ...
- python--多种程序分析(2)
1.文件操作有哪些模式?请简述各模式的作用 r模式只读 w模式只写 a模式只添加 r+可读可写 w+可写可读 a+可读可添加 rb 二进制只读 wb 二进制只写 ab 二进制添加 ...
- Android性能优化之中的一个 布局优化
本文为Android性能优化--布局优化,主要介绍使用抽象布局标签(include, viewstub, merge).去除不必要的嵌套和View节点.降低不必要的infalte及其它Layout方面 ...
- kubernetes之pod中断
系列目录 目标读者: 想要构建高可用应用的应用所有者,因此需要知道pod会发生哪些类型的中断 想要执行自动化(比如升级和自动扩容)的集群管理员. 自愿和非自愿的中断 pod不会自动消息,除非有人(可能 ...
- MyBatis学习(二):与Spring整合(非注解方式配置MyBatis)
搭建SpringMVC的-->传送门<-- 一.环境搭建: 目录结构: 引用的JAR包: 如果是Maven搭建的话,pom.xml的配置如下: <?xml version=" ...
- datatable的使用
学习可参考:http://www.guoxk.com/node/jquery-datatables http://yuemeiqing2008-163-com.iteye.com/blog/20069 ...
- Openlayers中layer介绍
1.base layers & overlay layers base layer:最底层的layer,其它的图层是在他之上,最先增加的图层默认作为base layer. overlay la ...
- 数据结构基础之memset---有memset 抛出的int 和 char 之间的转换和字节对齐
今天晚上,在做滤波算法时,里面用到很多float 和int 以及char 之间的类型强制转换,后面滤波完发现图片有些区域块,有过度曝光的白光,我就跟踪,以为是char 字符数字数据溢出问题,加了0-2 ...
- C# 打开指定的目录 记住路径中 / 与 \ 的使用方法
老生常谈的问题了,C#在指定目录时,路径中要使用 \\.直接看实例 using System; namespace OpenFile{ class OpenFile{ static void Main ...