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基本操作的更多相关文章

  1. Key/Value之王Memcached初探:二、Memcached在.Net中的基本操作

    一.Memcached ClientLib For .Net 首先,不得不说,许多语言都实现了连接Memcached的客户端,其中以Perl.PHP为主. 仅仅memcached网站上列出的语言就有: ...

  2. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

  3. Android Notification 详解——基本操作

    Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...

  4. 三、Redis基本操作——List

    小喵的唠叨话:前面我们介绍了Redis的string的数据结构的原理和操作.当时我们提到Redis的键值对不仅仅是字符串.而这次我们就要介绍Redis的第二个数据结构了,List(链表).由于List ...

  5. 二、Redis基本操作——String(实战篇)

    小喵万万没想到,上一篇博客,居然已经被阅读600次了!!!让小喵感觉压力颇大.万一有写错的地方,岂不是会误导很多筒子们.所以,恳请大家,如果看到小喵的博客有什么不对的地方,请尽快指正!谢谢! 小喵的唠 ...

  6. 一、Redis基本操作——String(原理篇)

    小喵的唠叨话:最近京东图书大减价,小喵手痒了就买了本<Redis设计与实现>[1]来看看.这里权当小喵看书的笔记啦.这一系列的模式,主要是先介绍Redis的实现原理(可能很大一部分会直接照 ...

  7. Linq查询基本操作

    摘要:本文介绍Linq查询基本操作(查询关键字) - from 子句 - where 子句 - select子句 - group 子句 - into 子句 - orderby 子句 - join 子句 ...

  8. C++ map的基本操作和使用

    原文地址:http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可 ...

  9. python之最强王者(10)———文件(File)、输入输出的基本操作

    1. Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 2.打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式. ...

随机推荐

  1. 机器学习数学基础- gradient descent算法(上)

    为什么要了解点数学基础 学习大数据分布式计算时多少会涉及到机器学习的算法,所以理解一些机器学习基础,有助于理解大数据分布式计算系统(比如spark)的设计.机器学习中一个常见的就是gradient d ...

  2. 深入理解JavaScript系列(转载)

    深入理解JavaScript系列 深入理解JavaScript系列(1):编写高质量JavaScript代码的基本要点 深入理解JavaScript系列(2):揭秘命名函数表达式 深入理解JavaSc ...

  3. android:hint属性对TextView的影响

    近期看到同事写的一段代码,非常easy吧就是: <LinearLayout android:layout_width="wrap_content" android:layou ...

  4. Python Unicode 转换 字符串

    estimate_price = "\u00a340\u00a0\u00a0-\u00a060" sold_price = "Sold for \u00a345" ...

  5. php比较函数,判断安全函数

    一.字符串比较函数: int strcasecmp ( string $str1 , string $str2 ) int strcmp ( string $str1 , string $str2 ) ...

  6. [Jobdu] 题目1507:不用加减乘除做加法

    题目描述: 写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号. 输入: 输入可能包含多个测试样例.对于每个测试案例,输入为两个整数m和n(1<=m,n<=100 ...

  7. 3、Cocos2dx 3.0游戏开发找小三之搭建开发环境

    尊重开发人员的劳动成果.转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27107295 搭建开发环境 使用 Cocos2d- ...

  8. 在linux平台下,设置core dump文件属性(位置,大小,文件名等)

    在linux平台下,设置core dump文件生成的方法: 1) 在终端中输入ulimit -c 如果结果为0,说明当程序崩溃时,系统并不能生成core dump. 2) 使用ulimit -c un ...

  9. Cocos2d-x 3.4 初体验——安装教程

    电脑系统window7 32位 1.首先从官网下载cocos2d-x并解压 http://cn.cocos2d-x.org/download/ 解压后的文件夹中有一个setup.py,双击运行.需要安 ...

  10. C语言 · P1001(大数乘法)

    算法提高 P1001   时间限制:1.0s   内存限制:256.0MB      当两个比较大的整数相乘时,可能会出现数据溢出的情形.为避免溢出,可以采用字符串的方法来实现两个大数之间的乘法.具体 ...