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. python一些内建函数(map,zip,filter,reduce,yield等)

    python一些内建函数(map,zip,filter,reduce,yield等) map函数 Python实际上提供了一个内置的工具,map函数.这个函数的主要功能是对一个序列对象中的每一个元素应 ...

  2. bzoj 1257 [CQOI2007]余数之和——数论分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1257 \( n\%i = n - \left \lfloor n/i \right \rfl ...

  3. bzoj3258秘密任务

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3258 因为只走最短路,所以先正反两遍djkstra,新建边. 这里的边是单向边.所以要用原来 ...

  4. Linux服务器运维安全策略经验分享

    http://jxtm.jzu.cn/?p=3692 大家好,我是南非蚂蚁,今天跟大家分享的主题是:线上Linux服务器运维安全策略经验.安全是IT行业一个老生常谈的话题了,从之前的“棱镜门”事件中折 ...

  5. Hibernate Cannot release connection 了,有办法解决!

      问题:    系统采用Spring MVC 2.5 + Spring 2.5 + Hibernate 3.2架构,其中数据源连接池采用的是Apache commons DBCP.问题是这样的,系统 ...

  6. C#操作mysql数据库,往mysql读取或者写入数据

    最近在开发的一个项目,需要将数据存贮在mysql数据库中,于是需要写一个操作mysql的帮助类,我采用的是官方的,还是先给出一个链接,后面有时间的话,继续更新. http://blog.csdn.ne ...

  7. (转)Inno Setup入门(十一)——完成安装后执行某些程序

    本文转载自:http://blog.csdn.net/yushanddddfenghailin/article/details/17250901 有些时候我们的程序虽然能够很好的完成安装,但是程序的配 ...

  8. each函数遍历select标签下的所有option选项

    如下: <select id="asd" name="sweet1"> <option value=1>--四川--</optio ...

  9. 0908期 HTML 基础 第一讲

    HTML  常用属性.标签以及表格 HTML 超文本标记语言的简称. <html>    --开始标签 <head> 网页上的控制信息 <title>页面标题< ...

  10. Long.parseLong(String s) 其中s必须是数字形式的字符串,才能运用该函数转化为长整型。

    public class ConverTo { public static void main(String [] args) { String numberIn =args[0]; convertN ...