1.工具类

 package com.lixin.stuty.hbase;

 import java.io.IOException;

 import org.apache.commons.configuration.ConfigurationUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
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.client.Table;
/**
* hbase for version 1.1.1
* @author Administrator
*
*/
public class HBaseUtil {
public static final String ZK_QUORUM = "hbase.zookeeper.quorum";
public static final String ZK_CLIENTPORT = "hbase.zookeeper.property.clientPort";
private Configuration conf = HBaseConfiguration.create();
private Connection connection ;
private Admin admin; public HBaseUtil(String zk_quorum) {
conf.set(ZK_QUORUM, zk_quorum);
init();
} public HBaseUtil(String zk_quorum,String zk_clientPort) {
conf.set(ZK_QUORUM, zk_quorum);
conf.set(ZK_CLIENTPORT, zk_clientPort);
init();
} private void init(){
try {
//Connection 的创建是个重量级的工作,线程安全,是操作hbase的入口
connection = ConnectionFactory.createConnection(conf);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
public void close(){
try {
if(admin != null) admin.close();
if(connection!=null) connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建一个表
* @param table_name 表名称
* @param family_names 列族名称集合
* @throws IOException
*/
public void create(String table_name,String... family_names) throws IOException{
//获取TableName
TableName tableName = TableName.valueOf(table_name);
//table 描述
HTableDescriptor htabledes = new HTableDescriptor(tableName);
for(String family_name : family_names){
//column 描述
HColumnDescriptor family = new HColumnDescriptor(family_name);
htabledes.addFamily(family);
}
admin.createTable(htabledes);
}
/**
* 增加一条记录
* @param table_name 表名称
* @param row rowkey
* @param family 列族名称
* @param qualifier 列族限定符(可以为null)
* @param value 值
* @throws IOException
*/
public void addColumn(String table_name,String row, String family,String qualifier,String value) throws IOException{
//表名对象
TableName tableName = TableName.valueOf(table_name);
//表对象
Table table = connection.getTable(tableName);
// put对象 负责录入数据
Put put = new Put(row.getBytes());
put.addColumn(family.getBytes(), qualifier.getBytes(), value.getBytes());
table.put(put);
}
/**
* 判断表是否存在
*/
public boolean tableExist(String table_name) throws IOException{
return admin.tableExists(TableName.valueOf(table_name));
}
/**删除表*/
public void deleteTable(String table_name) throws IOException{
TableName tableName = TableName.valueOf(table_name);
if(admin.tableExists(tableName)){
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
}
/**
* 查询单个row的记录
* @param table_name 表明
* @param row 行键
* @param family 列族
* @param qualifier 列族成员
* @return
* @throws IOException
*/
public Cell[] getRow(String table_name,String row,String family,String qualifier) throws IOException{
Cell[] cells = null;
//check
if(StringUtils.isEmpty(table_name)||StringUtils.isEmpty(row)){
return null;
}
//Table
Table table = connection.getTable(TableName.valueOf(table_name));
Get get = new Get(row.getBytes());
//判断在查询记录时,是否限定列族和子列(qualifier).
if(StringUtils.isNotEmpty(family)&&StringUtils.isNotEmpty(qualifier)){
get.addColumn(family.getBytes(), qualifier.getBytes());
}
if(StringUtils.isNotEmpty(family)&&StringUtils.isEmpty(qualifier)){
get.addFamily(family.getBytes());
}
Result result = table.get(get);
cells = result.rawCells();
return cells;
}
/**
* 获取表中的所有记录,可以指定列族,列族成员,开始行键,结束行键.
* @param table_name
* @param family
* @param qualifier
* @param startRow
* @param stopRow
* @return
* @throws IOException
*/
public ResultScanner getScan(String table_name,String family,String qualifier,String startRow,String stopRow) throws IOException{
ResultScanner resultScanner = null; //Table
Table table = connection.getTable(TableName.valueOf(table_name));
Scan scan = new Scan();
if(StringUtils.isNotBlank(family)&& StringUtils.isNotEmpty(qualifier)){
scan.addColumn(family.getBytes(), qualifier.getBytes());
}
if(StringUtils.isNotEmpty(family)&& StringUtils.isEmpty(qualifier)){
scan.addFamily(family.getBytes());
}
if(StringUtils.isNotEmpty(startRow)){
scan.setStartRow(startRow.getBytes());
}
if(StringUtils.isNotEmpty(stopRow)){
scan.setStopRow(stopRow.getBytes());
}
resultScanner = table.getScanner(scan); return resultScanner;
}
}

2.测试:

 package com.lixin.stuty.hbase;

 import static org.junit.Assert.*;

 import java.io.IOException;
import java.util.Iterator; import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.junit.Test; public class HBaseUtilTest {
private HBaseUtil hu = null;
@Before
public void init(){
String zk_quorum = "172.21.135.148";
String zk_clientPort = "2181";
hu = new HBaseUtil(zk_quorum, zk_clientPort);
}
@Test
public void testCreate() throws IOException {
String table_name = "users";
String[] fanily_names = new String[]{"user_id","address","info"};
hu.create(table_name, fanily_names);
hu.close();
}
@Test
public void testIsExist() throws IOException{
String table_name = "sitech";
System.out.println(hu.tableExist(table_name));
hu.close();
}
@Test
public void testDelete() throws IOException{
String table_name = "person1";
hu.deleteTable(table_name);
hu.close();
}
@Test
public void testGetRow() throws IOException{
String table_name = "users";
String row = "xiaoming";
String family = "address";
String qualifier = "";
Cell[] cells = hu.getRow(table_name, row, family, qualifier);
for(Cell cell : cells){
String recode_row = Bytes.toString(CellUtil.cloneRow(cell));
String family1 = Bytes.toString(CellUtil.cloneFamily(cell));
String qualifier1 = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(recode_row+"\t"+family1+"\t"+qualifier1+"\t"+value);
}
hu.close();
}
@Test
public void testGetScanner() throws IOException{
String table_name = "users";
String family = "address";
String qualifier = "city";
String startRow = "xiaoming";
String stopRow = "xiaoming"; ResultScanner resultScanner = hu.getScan(table_name, family, qualifier, startRow, stopRow);
Iterator<Result> iterator = resultScanner.iterator();
while(iterator.hasNext()){
Result result = iterator.next();
Cell[] rawCells = result.rawCells();
for(Cell cell : rawCells){
String recode_row = Bytes.toString(CellUtil.cloneRow(cell));
String family1 = Bytes.toString(CellUtil.cloneFamily(cell));
String qualifier1 = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(recode_row+"\t"+family1+"\t"+qualifier1+"\t"+value);
}
}
hu.close();
}
}

Hbase-1.1.1-java API的更多相关文章

  1. HBase 二次开发 java api和demo

    1. 试用thrift python/java以及hbase client api.结论例如以下:     1.1 thrift的安装和公布繁琐.可能会遇到未知的错误,且hbase.thrift的版本 ...

  2. Ubuntu下搭建Hbase单机版并实现Java API访问

    工具:Ubuntu12.04 .Eclipse.Java.Hbase 1.在Ubuntu上安装Eclipse,可以在Ubuntu的软件中心直接安装,也可以通过命令安装,第一次安装失败了,又试了一次,开 ...

  3. HBase 增删改查Java API

    1. 创建NameSpaceAndTable package com.HbaseTest.hdfs; import java.io.IOException; import org.apache.had ...

  4. HBase里的官方Java API

    见 https://hbase.apache.org/apidocs/index.html

  5. Hbase框架原理及相关的知识点理解、Hbase访问MapReduce、Hbase访问Java API、Hbase shell及Hbase性能优化总结

    转自:http://blog.csdn.net/zhongwen7710/article/details/39577431 本blog的内容包含: 第一部分:Hbase框架原理理解 第二部分:Hbas ...

  6. hbase java API跟新数据,创建表

    package hbaseCURD; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import o ...

  7. HBase 相关API操练(二):Java API

    一.HBase Java编程 (1)HBase是用Java语言编写的,它支持Java编程: (2)HBase支持CRUD操作:Create,Read,Update和Delete: (3)Java AP ...

  8. HBase 学习之路(六)——HBase Java API 的基本使用

    一.简述 截至到目前(2019.04),HBase 有两个主要的版本,分别是1.x 和 2.x ,两个版本的Java API有所不同,1.x 中某些方法在2.x中被标识为@deprecated过时.所 ...

  9. HBase 系列(六)——HBase Java API 的基本使用

    一.简述 截至到目前 (2019.04),HBase 有两个主要的版本,分别是 1.x 和 2.x ,两个版本的 Java API 有所不同,1.x 中某些方法在 2.x 中被标识为 @depreca ...

  10. 通过Java Api与HBase交互(转)

    HBase提供了Java Api的访问接口,掌握这个就跟Java应用使用RDBMS时需要JDBC一样重要,本文将继续前两篇文章中blog表的示例,介绍常用的Api. import java.io.IO ...

随机推荐

  1. Unit02: CSS 概述 、 CSS 语法 、 CSS 选择器 、 CSS声明

    Unit02: CSS 概述 . CSS 语法 . CSS 选择器 . CSS声明 my.css p { color: yellow; } demo1.html <!DOCTYPE html&g ...

  2. rapidjson的read和write的sample

    头文件 #include "json/document.h" #include "json/prettywriter.h" #include "jso ...

  3. 批处理实现mysql的备份

    脚本 @echo off echo. echo MySQL数据库备份 echo ***************************** echo. echo 今天是 %date% echo 时间是 ...

  4. Car-eye-http-flv-module 实现nginx-rtmp-mudule HTTP方式的FLV直播功能

    nginx-rtmp-mudule RTMP 是一款优秀的Car-eye-http-flv-module 是在nginx-rtmp-mudule RTMP基础上修改的流媒体服务器,除了支持flash播 ...

  5. 【转】利用 Apache JMeter 测试 WebSphere 性能

    如果您预算紧张并且时间紧迫 —— 或者即使您不是这样 —— 那么,您可能希望考虑使用 JMeter 来对 Web 和其他应用程序进行压力测试.IBM 的 Greg Herringer 详细描述他使用这 ...

  6. $.each $.map 和 $().each $().map

    $.each :用于遍历数据,如json. $(function () { var data = [{'name': 'a', 'age': 12}, {'name': 'b', 'age': 12} ...

  7. mysql 优化(3)

    using filesort 不能利用索引来进行分组或排序,利用filesort算法在内存或者磁盘进行排序using temporary 先在内存中进行分组,归并等操作,不够利用磁盘 SELECT i ...

  8. Linux系统性能检测

    转自:http://www.cnblogs.com/itech/archive/2011/06/08/2075145.html 一 .uptime uptime命令用于查看服务器运行了多长时间以及有多 ...

  9. VS 2017 Region快捷键无法折叠

  10. cocos2dx切换播放的动画

    版本:cocos2dx 2.2.6 IDE: VS2012 语言:C++98 美术资源一共有两段动画的序列帧,一个是手绘马行走图,一个是分子人行走图. 程序要实现的目的就是在同一个位置,点击按钮可以实 ...