HBaseclientAPI基本操作
Java类与HBase数据模型
HBaseConfiguration
包名 : org.apache.hadoop.hbase.HBaseConfiguration
作用:对HBase进行配置。
使用方法演示样例:
HBaseConfiguration hconfig = new HBaseConfiguration();
hconfig.set("hbase.zookeeper.property.clientPort","2181");
HBaseAdmin
包名 : org.apache.hadoop.hbase.client.HBaseAdmin
作用:提供了一个接口来管理HBase数据库的表信息。
它提供的方法包括:创建表。删除表,列出表项。使表有效或无效,以及加入或删除表列族成员等。
使用方法演示样例:
HBaseAdmin admin = new HBaseAdmin(config);
admin.disableTable("tablename")
HTableDescriptor
包名: org.apache.hadoop.hbase.HTableDescriptor
作用:包括了表的名字及其相应表的列族。
使用方法演示样例:
HTableDescriptor htd = new HTableDescriptor(table);
htd.addFamily(new HcolumnDescriptor("family"));
HColumnDescriptor
包名: org.apache.hadoop.hbase.HColumnDescriptor
作用:维护着关于列族的信息,比如版本。压缩设置等。
它通常在创建表或者为表加入列族的时候使用。
列族被创建后不能直接改动。仅仅能通过删除,然后又一次创建的方式。
列族被删除的时候,列族里面的数据也会同一时候被删除。
使用方法演示样例:
HTableDescriptor htd = new HTableDescriptor(tablename);
HColumnDescriptor col = new HColumnDescriptor("content:");
htd.addFamily(col);
HTable
包名: org.apache.hadoop.hbase.client.HTable
作用:能够用来和HBase表直接通信。此方法对于更新操作来说是非线程安全的。
使用方法演示样例:
HTable table = new HTable(conf, Bytes.toBytes(tablename));
ResultScanner scanner = table.getScanner(family);
HTablePool
包名: org.apache.hadoop.hbase.client.HTablePool
作用:能够解决HTable存在的线程不安全问题。同一时候通过维护固定数量的HTable对象,能够在程序执行期间复用这些HTable资源对象。
说明:
1. HTablePool能够自己主动创建HTable对象,并且对客户端来说使用上是全然透明的。能够避免多线程间数据并发改动问题。
2. HTablePool中的HTable对象之间是公用Configuration连接的,能够能够降低网络开销。
HTablePool的使用非常easy:每次进行操作前。通过HTablePool的getTable方法取得一个HTable对象,然后进行put/get/scan/delete等操作,最后通过HTablePool的putTable方法将HTable对象放回到HTablePool中。
/**
* A simple pool of HTable instances.
*
* Each HTablePool acts as a pool for all tables. To use, instantiate an
* HTablePool and use {@link #getTable(String)} to get an HTable from the pool.
*
* This method is not needed anymore, clients should call HTableInterface.close()
* rather than returning the tables to the pool
*
* Once you are done with it, close your instance of {@link HTableInterface}
* by calling {@link HTableInterface#close()} rather than returning the tables
* to the pool with (deprecated) {@link #putTable(HTableInterface)}.
*
* <p>
* A pool can be created with a <i>maxSize</i> which defines the most HTable
* references that will ever be retained for each table. Otherwise the default
* is {@link Integer#MAX_VALUE}.
*
* <p>
* Pool will manage its own connections to the cluster. See
* {@link HConnectionManager}.
* @deprecated as of 0.98.1. See {@link HConnection#getTable(String)}.
*/
@InterfaceAudience.Private
@Deprecated
public class HTablePool implements Closeable {
}
Put
包名: org.apache.hadoop.hbase.client.Put
作用:用来对单个行执行加入操作。
使用方法演示样例:
HTable table = new HTable(conf,Bytes.toBytes(tablename));
Put p = new Put(brow);//为指定行创建一个Put操作
p.add(family,qualifier,value);
table.put(p);
Get
包名: org.apache.hadoop.hbase.client.Get
作用:用来获取单个行的相关信息。
使用方法演示样例:
HTable table = new HTable(conf, Bytes.toBytes(tablename));
Get g = new Get(Bytes.toBytes(row));
table.get(g);
Result
包名: org.apache.hadoop.hbase.client.Result
作用:存储Get或者Scan操作后获取表的单行值。
使用此类提供的方法能够直接获取值或者各种Map结构( key-value对)。
ResultScanner
包名: org.apache.hadoop.hbase.client.ResultScanner
作用:存储Get或者Scan操作后获取表的单行值。
使用此类提供的方法能够直接获取值或者各种Map结构( key-value对)。
例程
package HbaseAPI;
import java.io.IOException;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseConnection {
private String rootDir;
private String zkServer;
private String port;
private Configuration conf;
private HConnection hConn = null;
private HBaseConnection(String rootDir,String zkServer,String port) throws IOException{
this.rootDir = rootDir;
this.zkServer = zkServer;
this.port = port;
conf = HBaseConfiguration.create();
conf.set("hbase.rootdir", rootDir);
conf.set("hbase.zookeeper.quorum", zkServer);
conf.set("hbase.zookeeper.property.clientPort", port);
hConn = HConnectionManager.createConnection(conf);
}
public void creatTable(String tableName,List<String> cols){
try {
//管理数据库的表信息
HBaseAdmin admin = new HBaseAdmin(conf);
if(admin.tableExists(tableName)){
throw new Exception("table exists");
}
else{
//
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
for (String col : cols) {
//提供列族
HColumnDescriptor colDesc = new HColumnDescriptor(col);
colDesc.setCompressionType(Algorithm.GZ);
colDesc.setDataBlockEncoding(DataBlockEncoding.DIFF);
tableDesc.addFamily(colDesc);
}
//创建表
admin.createTable(tableDesc);
}
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
//插入数据
public void putData(String tableName,List<Put> puts) throws IOException{
HTableInterface table = hConn.getTable(tableName);
table.put(puts);
table.setAutoFlush(false);
table.flushCommits();
}
//获取数据
public Result getData(String tableName,String rowkey) throws IOException{
HTableInterface table = hConn.getTable(tableName);
//用来获取单个行的相关信息
Get get = new Get(Bytes.toBytes(rowkey));
return table.get(get);
}
public void format(Result result){
//行键
String rowkey = Bytes.toString(result.getRow());
//Return an cells of a Result as an array of KeyValues
KeyValue[] kvs = result.raw();
for (KeyValue kv : kvs) {
//列族名
String family = Bytes.toString(kv.getFamily());
//列名
String qualifier = Bytes.toString(kv.getQualifier());
String value = Bytes.toString(result.getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier)));
System.out.println("rowkey->"+rowkey+", family->"
+family+", qualifier->"+qualifier);
System.out.println("value->"+value);
}
}
public static void main(String[] args) throws IOException {
String rootDir = "hdfs://hadoop1:8020/hbase";
String zkServer = "hadoop1";
String port = "2181";
//初始化
HBaseConnection conn = new HBaseConnection(rootDir,zkServer,port);
//创建表
List<String> cols = new LinkedList<>();
cols.add("basicInfo");
cols.add("moreInfo");
conn.creatTable("students", cols);
//插入数据
List<Put> puts = new LinkedList<>();
Put put1 = new Put(Bytes.toBytes("Tom"));
//(列族名,列,值)
put1.add(Bytes.toBytes("basicInfo"),Bytes.toBytes("age"),Bytes.toBytes("27"));
put1.add(Bytes.toBytes("basicInfo"),Bytes.toBytes("tel"),Bytes.toBytes("3432"));
Put put2 = new Put(Bytes.toBytes("Joson"));
put2.add(Bytes.toBytes("basicInfo"),Bytes.toBytes("age"),Bytes.toBytes("24"));
put2.add(Bytes.toBytes("basicInfo"),Bytes.toBytes("tel"),Bytes.toBytes("34322"));
puts.add(put1);
puts.add(put2);
conn.putData("students", puts);
//输出结果
Result result = conn.getData("students", "Tom");
conn.format(result);
}
}
HBaseclientAPI基本操作的更多相关文章
- Key/Value之王Memcached初探:二、Memcached在.Net中的基本操作
一.Memcached ClientLib For .Net 首先,不得不说,许多语言都实现了连接Memcached的客户端,其中以Perl.PHP为主. 仅仅memcached网站上列出的语言就有: ...
- Android Notification 详解(一)——基本操作
Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...
- Android Notification 详解——基本操作
Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...
- 三、Redis基本操作——List
小喵的唠叨话:前面我们介绍了Redis的string的数据结构的原理和操作.当时我们提到Redis的键值对不仅仅是字符串.而这次我们就要介绍Redis的第二个数据结构了,List(链表).由于List ...
- 二、Redis基本操作——String(实战篇)
小喵万万没想到,上一篇博客,居然已经被阅读600次了!!!让小喵感觉压力颇大.万一有写错的地方,岂不是会误导很多筒子们.所以,恳请大家,如果看到小喵的博客有什么不对的地方,请尽快指正!谢谢! 小喵的唠 ...
- 一、Redis基本操作——String(原理篇)
小喵的唠叨话:最近京东图书大减价,小喵手痒了就买了本<Redis设计与实现>[1]来看看.这里权当小喵看书的笔记啦.这一系列的模式,主要是先介绍Redis的实现原理(可能很大一部分会直接照 ...
- Linq查询基本操作
摘要:本文介绍Linq查询基本操作(查询关键字) - from 子句 - where 子句 - select子句 - group 子句 - into 子句 - orderby 子句 - join 子句 ...
- C++ map的基本操作和使用
原文地址:http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可 ...
- python之最强王者(10)———文件(File)、输入输出的基本操作
1. Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 2.打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式. ...
随机推荐
- webp转jpg
webp转jpg CreateTime--2017年12月8日09:32:38 Author:Marydon 1.webp格式介绍 WebP 的优势体现在它具有更优的图像数据压缩算法,能带来更小的 ...
- oracle 存储过程 调用动态sql
oracle 存储过程 调用动态sql CreationTime--2018年8月16日11点25分 Author:Marydon 1.错误实现方式 --开始时间拼接' 00:00:00' V_S ...
- OFBiz实战(1):整合Groovy+FreeMaker
这是OFBiz实战系列的第一篇文件,该系列的目的是整合Groovy+FreeMaker开发一个图书管理系统,阐述在此过程中碰到的一系列问题,以及如何解决这些问题.第一篇文章说明如何使用Groovy+F ...
- OFBiz:添加实体栏位
如何添加实体栏位?这里演示为PostalAddress添加planet栏位.打开applications/party/entitydef/entitymodel.xml,找到PostalAddress ...
- 【转帖】Dubbo:来自于阿里巴巴的分布式服务框架
http://www.biaodianfu.com/dubbo.html Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被 ...
- LibRec:一个实现推荐系统的Java库包
LibRec是一个用于实现推系统 RS 的Java库包,实现推荐系统的两个经典问题: rating prediction(评分排行预测) 和 item ranking (项目排行),其内置了经典的机器 ...
- mosquitto配置文件详解
安装完成之后,所有配置文件会被放置于/etc/mosquitto/目录下,其中最重要的就是Mosquitto的配置文件,即mosquitto.conf,以下是详细的配置参数说明. # Config f ...
- jQuery开发插件的两种方式
最近挺多人写jQuery的,都是关于jQuery扩展方面的,使用方面的讲的比较多,但是关于详细的一个基础的过程讲的比较少一点,做web开发的基本上都会用到jQuery,本人就根据jQuery的使用经验 ...
- SQL Server 错误(待补充)
1.问题:在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接. 解决方法: 打开“ ...
- CakePHP不支持path/to路径,前后台无法方法
本来想把前后台分离,可是阅读了cakephp的说明,才发现.cakephp根本就不支持path/to路径. cakephp官网给出的 管理员分离方式:http://book.cakephp.org/2 ...