Hbase Java API程序设计步骤
http://www.it165.net/admin/html/201407/3390.html 步骤1:创建一个Configuration对象
包含了客户端链接Hbase服务所需的全部信息:
zookeeper位置(我们只有链接到zookeeper才能与hbase通信,master仅负责负载均衡等) ,
zookeeper链接超时时间 包含各种配置信息,hbase server zookeeper 访问地址和端口号等。
Configutation conf = HbaseConfiguration.create();
create()函数的内部逻辑:
从classpath中加载hbase-default.xml和hbase-site.xml两个文件
hbase-defaut.xml已经打包到Hbase jar包中
hbase-site.xml需要添加到class path中
hbase-site.xml将覆盖hbase-default.xml中的同名属性。 hbase如何找到并从classpath中获取hbase-site.xml信息 ====》如何检查hbase-site.xml已经在hadoop classpath中了呢? 运行 hadoop classpath | grep hbase
1 修改hadoop脚本,将Hbase classpath加入
2 在<hadoop_install>/conf/hadoop-env.sh中设置:
export HADOOP_CLASSPATH=$HBASE_HOME/*:$HBASE_HOME/conf:$HADOOP_CLASSPATH 两个目录放到hadoop的conf的hadoop.env.sh中 如果已经有了一个Configuration文件,可进行如下操作:
Configuration newconf = Configuration.create(existingConf);
用户自定义的配置文件将在已有配置文件之后加载,将覆盖hbase-default.xml hbase-site.xml中的配置
create
public static org.apache.hadoop.conf.Configuration create()
Creates a Configuration with HBase resources
Returns:
a Configuration with HBase resources
create
public static org.apache.hadoop.conf.Configuration create(org.apache.hadoop.conf.Configuration that)
Parameters:
that - Configuration to clone.
Returns:
a Configuration created with the hbase-*.xml files plus the given configuration. 没有直接从路径添加的构造函数,但是可以构造一个 org.apache.hadoop.conf.Configuration
/**
* Add a configuration resource.
*
* The properties of this resource will override properties of previously
* added resources, unless they were marked <a href="#Final">final</a>.
*
* @param file file-path of resource to be added, the local filesystem is
* examined directly to find the resource, without referring to
* the classpath.
*/
public void addResource(Path file) {
addResourceObject(new Resource(file));
}
可单独覆盖某一个或多个参数值
Configuration conf = HbaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "node1, node2"); 但通常不推荐这么做。 因为需要重新打包,不方便,不如放到配置文件中。
步骤2:创建一个HTable句柄
提供Configuration对象
提供待访问Table名称
HTable table = new HTable(conf, tabName); 》一个table对应一个Htable句柄 这个句柄在org.apache.hadoop.hbase.client.HTable
》提供了CRUD操作 create read update del
》提供行级事务,
不支持多行事务或者表级别事务
严格的行一致性
并发读,顺序写。 创建HTable句柄代价很大
1 扫描.META.表
2 创建一次,以后尽可能复用
3 如果需要创建多个HTable句柄,使用HTableTool HTable并非线程安全的,一个线程创建一个即可。
Htable支持CRUD批处理,非线程安全,仅是为了提高性能。 步骤3:执行相应的操作
put,get,delete,scan等
table.getTableName();
步骤4:关闭Htable句柄 【句柄不关,会发生内存泄露】
将内存数据刷新到磁盘上。
释放各种资源。
table.close()
package com.jlc.hadoop.hbase.example; import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes; public class PutTest { public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
HTable hTable = new HTable(conf, "tab1");
System.out.println(Bytes.toString(hTable.getTableName())); //步骤一,创建一个put对象
Put put = new Put(Bytes.toBytes("row1")); //设置cell值
//Put.add(family, column, value)
//Put.add(family, column, timestamp, value)
//Put.add(KeyValue kv) KeyValue 事实上就是封装了 上面四个,看起来简单 byte[] family = Bytes.toBytes("fam1");
byte[] qualifier = Bytes.toBytes("col1");
byte[] value = Bytes.toBytes("val2");;
put.add(family, qualifier, value); hTable.put(put); hTable.close(); // hbase(main):003:0> scan 'tab1'
// ROW COLUMN+CELL
// row1 column=fam1:col1, timestamp=1395796780021, value=val1
// row2 column=fam1:col1, timestamp=1398305517331, value=val2
// 2 row(s) in 0.0090 seconds
// 若row1 fam1 col1 已经存在,则 scan的时候 显示最新时间戳的那个值,其他的如何获取呢???
// hbase(main):004:0> scan 'tab1'
// ROW COLUMN+CELL
// row1 column=fam1:col1, timestamp=1398305623182, value=val2
// row2 column=fam1:col1, timestamp=1398305517331, value=val2 } }
package com.jlc.hadoop.hbase.example; import java.io.IOException; import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.conf.Configuration; public class ConstructHTable { public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
HTable hTable = new HTable(conf, "tab1");
System.out.println(Bytes.toString(hTable.getTableName()));
hTable.close();
} }
package com.jlc.hadoop.hbase.example; import java.io.IOException; import org.apache.hadoop.conf.Configuration;
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.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes; public class ReadTest { public static void print(Result result){
System.out.println("-----------------------------");
System.out.println("RowId:" + Bytes.toString(result.getRow()));
byte[] val1 = result.getValue(Bytes.toBytes("fam1"), Bytes.toBytes("col1"));
System.out.println("fam1:col1=" + Bytes.toString(val1));
byte[] val2 = result.getValue(Bytes.toBytes("fam1"), Bytes.toBytes("col1"));
System.out.println("fam1:col1=" + Bytes.toString(val2));
} public static void scan(HTable hTable, String startrow,String stoprow) throws IOException{
System.out.println("scaning from " + startrow + " to " + stoprow); Scan scan = new Scan(Bytes.toBytes(startrow), Bytes.toBytes(stoprow)); // 这个限定了row 但怎么限定列呢
//scan.addColumn(family, qualifier)
scan.addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("col1")); // 这个就指明我们仅仅取某个列就行了
ResultScanner scanner = hTable.getScanner(scan); //获取scanner句柄
for (Result result : scanner) {
// byte[] value = result.getValue(family, qualifier)
byte[] value = result.getValue(Bytes.toBytes("fam1"), Bytes.toBytes("col1"));
System.out.println(Bytes.toString(result.getRow()) + "=>" + Bytes.toString(value));
}
scanner.close();// 这个不要忘记哦
} public static void main(String[] args) throws IOException { /*
* 支持的API类型 通过rowkey获取一行数据 通过rowkey集合获取多条记录 扫描整个表或者表的一部分
*
* 扫描表:可指定扫描的范围,[startkey, endkey] 表中数据是按照rowkey排序的
*
* 读取时的注意事项: 1 只读取需要的数据
* [比方我只读取某个columnfamliy里某一个column的数据,指定这一列就行了,不要都读取出来,浪费io] 2
* 尽可能的增加数据约束条件 3 可增加family, column(s) time range 和 max
* versions【我要返回多少个版本号】等约束条件
*
* 接口实例:get.setTimeRange(minStamp, maxStamp) 这个就是增加时间范围, time range
* get.setMaxVersions(maxVersions)get.addFamily(family) 只后去众多familys
* 中的一个familyget.addColumn(family, column)
* 只获取某个family的众多columen中的一个column的数据
*/ Configuration conf = HBaseConfiguration.create();
HTable hTable = new HTable(conf, "tab1");
System.out.println(Bytes.toString(hTable.getTableName())); // byte[] family = Bytes.toBytes("fam1");
// byte[] qualifier = Bytes.toBytes("col1");
// byte[] value = Bytes.toBytes("val2");; // get example
// Get get = new Get(Bytes.toBytes("row1"));
// Result result = hTable.get(get);
// print(result); //有时候我们不需要整个row里的所有数据,我们可以加以限制 [推荐使用这种限制的]
// get.addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("col1"));
// result = hTable.get(get);
// print(result); // keyvalues={row1/fam1:col1/1398305623182/Put/vlen=4/mvcc=0}
// keyvalues={row1/fam1:col1/1398305623182/Put/vlen=4/mvcc=0} //delete example
//Delete delete = new Delete(Bytes.toBytes("rowtodel"));
//hTable.delete(delete); //Delete delete1 = new Delete(Bytes.toBytes("another row"));
//delete1.deleteColumn(family, qualifier)
//hTable.delete(delete1); //scan example
scan(hTable, "row1", "row3"); //前闭 后开的 要得道前两行,需要指定到第三行
hTable.close(); } }
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使用(一)
前言 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 ...
随机推荐
- Nothing
破烂的文具盒里,一张十年的纸条子和一袋存了十年的德芙巧克力 浅绿色的纸条子上写是当时你给我抄的作业题目,蓝色清秀的字体 但是十年后,你却已嫁他人 将身后的风雪.夕阳,空气埋葬.窑藏,待非常多年以后酿成 ...
- [Webpack 2] Chunking common modules from multiple apps with the Webpack CommonsChunkPlugin
If you have a multi-page application (as opposed to a single page app), you’re likely sharing module ...
- linux进程间通讯-System V IPC 信号量
进程间通信的机制--信号量.注意请不要把它与之前所说的信号混淆起来,信号与信号量是不同的两种事物.有关信号的很多其它内容,能够阅读我的还有一篇文章:Linux进程间通信--使用信号.以下就进入信号量的 ...
- android 74 下载文本
页面: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tool ...
- Chapter 4 - How to Fire some Bullets
Now, we want to let the hero fire some bullets to kill the enemies, add the codes below to set the l ...
- VC++ 响应回车键的2种方法
众所周知,VC++响应回车键经常用的方法是利用 BOOL PreTranslateMessage(MSG* pMsg) 截取回车键消息,如: if (pMsg->message == WM_KE ...
- Java基础知识强化之集合框架笔记57:Map集合之HashMap集合(HashMap<Student,String>)的案例
1. HashMap集合(HashMap<Student,String>)的案例 HashMap<Student,String>键:Student 要求:如果两个对象 ...
- 如何让android sdk manager飞奔安装sdk
由于国内的gwf的原因,凡是google的服务都是无法正常访问,这两天从android官网上下载sdk不是一般的慢.不过现在有一个好的方法.sdk都是从https://dl-ssl.google.co ...
- skynet网络库socket-server
最近在读大神云风的开源服务器架构skynet,其中的网络库,云风已经单独开来,可以独立使用. 开源地址: https://github.com/cloudwu/socket-server 网络库已经封 ...
- Android 读取txt文件并以utf-8格式转换成字符串
博客: 安卓之家 微博: 追风917 CSDN: 蒋朋的家 简书: 追风917 博客园: 追风917 # 使用EncodingUtils 今天用到了城市选择三级联动的库,用的这个:https://gi ...