hbase实战 java_1006-HBase操作实战(JAVA API状态)

一、准备阶段

开发环境:

hadoop:hadoop-2.4.0

hbase:hbase-0.94.11-security

eclipse:Juno Service Release 2

二、创建hbasedemo项目

1、通过 Eclipse 创建一个新 Java project

2、右击项目根文件夹,选择“Propertiesà> Java Build Pathà> Libraryà>  Add  External  JARs”

3、加入jar文件到classpath

3.1 解压 HBase 安装文件

3.2 拷贝hbase-0.94.7-security.jar、hbase-0.94.11-security-tests.jar到project

3.3lib子文件夹下全部的 jar 包加入到本project的 Build Path下

三、使用HBase JAVA API

初始化和释放资源方法:

public void init() 数据初始化

public void destory()  释放资源

验证方法:

public void testCreate() 验证创建表

public void testDrop() 验证删除表

public void testPut()验证加入表数据

public void testGetByRowKey() 验证怎样通过rowkey获取数据

public void testScanSToEnd() 验证范围行数据获取

public void testScanAll()  验证全表扫描

方法:

public List get(String tableName, String rowKey)  通过rowKey插入表数据

public List scan(String tableName)  通过表名查询全部信息

public List scan(String tableName,String startRow,String stopRow) 查询范围行方法

public void create(String tableName, String[] cfs)  通过表名和列族信息创建表

public void drop(String tableName) 删除表

public void put(String tableName, List values) 依据提供的表和对象信息插入hbase

第一步: 封装HBase中的存储单元Cell对象/**

* 封装HBase中存储单元cell对象

* @author shenfl

* @version:V1.0

* @Date:2015-6-8

*/

public class CellBean {

//行健

private String rowKey;

//列族

private String columnFamilly;

//列名

private String columnName;

//cell值

private String columnValue;

public String getRowKey() {

return rowKey;

}

public void setRowKey(String rowKey) {

this.rowKey = rowKey;

}

public String getColumnFamilly() {

return columnFamilly;

}

public void setColumnFamilly(String columnFamilly) {

this.columnFamilly = columnFamilly;

}

public String getColumnName() {

return columnName;

}

public void setColumnName(String columnName) {

this.columnName = columnName;

}

public String getColumnValue() {

return columnValue;

}

public void setColumnValue(String columnValue) {

this.columnValue = columnValue;

}

}

第二步: 使用HBase JAVA API 操作HBase  数据库

/**

* 使用HBase JAVA API操作

*

* @author shenfl

*

*/

public class HBaseTest {

Configuration config = null;

// 创建操作表對象

HBaseAdmin admin = null;

// hbase的连接

HConnection conn = null;

@Before

public void init() {

config = HBaseConfiguration.create();

// HBase仅仅须要知道ZooKeeper。就能够操作RegionServer上的數據,设置HBase 连接ZooKeeper

config.set("hbase.zookeeper.quorum", "192.168.2.35:2181,192.168.2.36:2181,192.168.2.37:2181");

try {

conn = HConnectionManager.createConnection(config);

admin = new HBaseAdmin(config);

} catch (Exception e) {

e.printStackTrace();

}

}

@After

public void destory() {

try {

admin.close();

conn.close();

} catch (IOException e) {

e.printStackTrace();

}

}

@Test

public void testCreate() {

String tableName = "account";

String[] columnFamilies = new String[] { "info", "other" };

create(tableName, columnFamilies);

}

//@Test

public void testDrop() {

drop("account");

}

@Test

public void testPut() {

// 设置表对象列表

List cbList = new ArrayList();

// 设置表名account

String tableName = "account";

CellBean e = null;

for(int i=0;i<3;i++){

e = new CellBean();

e.setRowKey("g20142500"+i);

e.setColumnFamilly("info");

e.setColumnName("username");

e.setColumnValue("shenfl"+i);

cbList.add(e);

e = new CellBean();

e.setRowKey("g20142500"+i);

e.setColumnFamilly("other");

e.setColumnName("career");

e.setColumnValue("singer"+i);

cbList.add(e);

}

put(tableName, cbList);

}

@Test

public void testGetByRowKey() {

String tableName = "account";

String rowKey = "g201425001";

List cells = get(tableName, rowKey);

StringBuffer info = new StringBuffer();

for (Cell c : cells) {

//使用CellUtil方法输出相应列。 hbase0.96 版本号使用CellUtil函数

info.append(new String(CellUtil.cloneFamily(c))).append(":")

.append(new String(CellUtil.cloneQualifier(c))).append("\t")

.append(new String(CellUtil.cloneValue(c))).append("\n");

}

System.out.println(info);

}

@Test

public void testScanSToEnd(){

StringBuffer sb = new StringBuffer();

String tableName = "account";

String startRow = "g201425000";

String stopRow = "g201425002";

List rsList = scan(tableName, startRow, stopRow);

byte[] rowKey = null;

byte[] username = null;

byte[] career = null;

for(Result rs:rsList){

rowKey = rs.getRow();

username = rs.getValue(Bytes.toBytes("info"), Bytes.toBytes("username"));

career = rs.getValue(Bytes.toBytes("other"), Bytes.toBytes("career"));

sb.append(new String(rowKey)).append("\t")

.append(new String(username)).append("\t")

.append(new String(career)).append("\n");

}

System.out.println(sb.toString());

}

@Test

public void testScanAll() {

StringBuffer sb = new StringBuffer();

List rsList = scan("account");

for (Result rs : rsList) {

List listCells = rs.listCells();

for (Cell c : listCells) {

// 使用CellUtil方法输出相应列。 hbase0.96 版本号使用CellUtil函数

sb.append(new String(CellUtil.cloneRow(c))).append("\t")

.append(new String(CellUtil.cloneFamily(c))).append(":")

.append(new String(CellUtil.cloneQualifier(c))).append("\t")

.append(new String(CellUtil.cloneValue(c))).append("\n");

}

}

System.out.println(sb);

}

/**

* 通过rowKey插入表数据

* @param tableName 表名

* @param rowKey行健

* @return

*/

public List get(String tableName, String rowKey) {

HTableInterface table = null;

Get get = null;

Result rs = null;

List listCells = new ArrayList();

try {

table = conn.getTable(tableName);

get = new Get(Bytes.toBytes(rowKey));

rs = table.get(get);

listCells = rs.listCells();

} catch (IOException e) {

e.printStackTrace();

}finally{

try {

table.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return listCells;

}

/**

* 查询全部行

*

* @param tableName

* @return

*/

public List scan(String tableName) {

HTableInterface table = null;

List rsList = new ArrayList();

try {

table = conn.getTable(tableName);

Scan scan = new Scan();

ResultScanner scanner = table.getScanner(scan);

Iterator iterator = scanner.iterator();

Result rs = null;

while (iterator.hasNext()) {

rs = (Result) iterator.next();

rsList.add(rs);

}

} catch (Exception e) {

e.printStackTrace();

}finally{

try {

table.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return rsList;

}

/**

* 查询范围行

* @param tableName 表名

* @param startRow 開始的行健

* @param stopRow 结束行健

* @return

*/

public List scan(String tableName,String startRow,String stopRow) {

HTableInterface table = null;

List rsList = new ArrayList();

try {

table = conn.getTable(tableName);

Scan scan = new Scan();

scan.setStartRow(Bytes.toBytes(startRow));

scan.setStopRow(Bytes.toBytes(stopRow));

ResultScanner rs = table.getScanner(scan);

for(Result v:rs){

rsList.add(v);

}

} catch (Exception e) {

e.printStackTrace();

}finally{

try {

table.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return rsList;

}

/**

* 创建壁报

*

* @param tableName

* 表名

* @param cfs

* 列族

*/

public void create(String tableName, String[] cfs) {

if (cfs == null || cfs.length == 0) {

return;

}

try {

// 校验表是否存储

if (admin.tableExists(tableName)) {

return;

}

// 创建表

HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));

// 创建列族

for (String cf : cfs) {

desc.addFamily(new HColumnDescriptor(cf));

}

// 创建表

admin.createTable(desc);

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 删除表

*

* @param tableName

* 表名

*/

public void drop(String tableName) {

try {

if(admin.tableExists(tableName)){

admin.disableTable(tableName);

admin.deleteTable(tableName);

}

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 向指定表插入数据

*

* @param tableName

* 表名

* @param values

* 数据

*/

public void put(String tableName, List values) {

if (StringUtils.isBlank(tableName) || values == null || values.size() == 0) {

return;

}

Put put = null;

HTableInterface table = null;

try {

table = conn.getTable(tableName);

for (CellBean v : values) {

put = new Put(Bytes.toBytes(v.getRowKey()));

put.add(Bytes.toBytes(v.getColumnFamilly()), Bytes.toBytes(v.getColumnName()),

Bytes.toBytes(v.getColumnValue()));

table.put(put);

}

} catch (Exception e) {

// 实际生产环境要通过记录日志。比如: logger.warn("xxxxx",e);

e.printStackTrace();

}finally{

try {

table.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

參考文章:

1、HBase连接池 -- HTablePool被Deprecated之后

http://blog.csdn.net/u010967382/article/details/38046821

2、HBase Java API 介绍

http://www.cnblogs.com/NicholasLee/archive/2012/09/13/2683432.html

3、HBase Java API 操作案例

http://www.programcreek.com/java-api-examples/index.php?api=org.apache.hadoop.hbase.HTableDescriptor

http://hbase.apache.org/apidocs/org/apache/hadoop/hbase/client/Admin.html

http://blog.csdn.net/hadoop_/article/details/11481215

版权声明:本文博主原创文章,博客,未经同意不得转载。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值