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. 监控mysql状态并发送Email

    */10 * * * *  /shell/mysql_status.sh > /dev/null 2>&1 #!/bin/bash port55=`/usr/sbin/lsof - ...

  2. Ubuntu 16.04配置OpenGL教程

    sudo apt-get install build-essential sudo apt-get install libgl1-mesa-dev sudo apt-get install libgl ...

  3. MySQL COUNT(*) & COUNT(1) & COUNT(col) 比较分析

    在面试的时候我们会经常遇到这个问题: MySQL 中,COUNT(*).COUNT(1).COUNT(col) 有区别吗? 有区别. 接下来我们分析一下这三者有什么样的区别. 一.SQL Syntax ...

  4. tomcat:A docBase * inside the host appBase has been specifi, and will be ignored

    警告: A docBase  D:\apache-tomcat-8.5.12\webapps\webapps\projectname inside the host appBase has been ...

  5. 【BZOJ】1007: [HNOI2008]水平可见直线(凸包)

    题目 传送门:QWQ 分析 在下面维护一个凸壳 好久没写博客了...... 代码 #include <bits/stdc++.h> using namespace std; ; ,INF= ...

  6. ERROR无法从静态上下文中引用非静态变量

    ERROR无法从静态上下文中引用非静态变量 2012-06-16 20:58:52 分类: Java 什么是“static”? 学习过java.C++或C的人都应该认识这个关键字.用这个关键字修饰的变 ...

  7. java实现二叉树demo

    二叉树(BinaryTree)是n(n≥0)个结点的有限集,它或者是空集(n=0),或者由一个根结点及两棵互不相交的.分别称作这个根的左子树和右子树的二叉树组成.    这个定义是递归的.由于左.右子 ...

  8. buffer cache 深度解析

    本文首先详细介绍了oracle中buffer cache的概念以及所包含的内存结构.然后结合各个后台进程(包括DBWRn.CKPT.LGWR等)深入介绍了oracle对于buffer cache的管理 ...

  9. System.Security.Cryptography.CryptographicException: 系统找不到指定的文件

    默认为false 改为true

  10. [CSAPP] Chapter 1 Overview of Computer

    1.1 information is bits + context All computer programs are just a sequence of bits, each with a val ...