hbase 1.0.1 java api_HBase-1.0.1.1的Java API使用记录

最近项目中使用了HBase作为数据记录,这里简单记录一下使用的Java API,网上很多API都是比较早期的,现在已经属于过时了,所以在这里总结一下。

1. 首先是Configuration类,这个配置类来自org.apache.hadoop.conf.Configuration,首先要初始化它,这是一个重量级操作。Connection接口,来自org.apache.hadoop.hbase.client.Connection,获得这个Connection的一个实例,需要使用ConnectionFactory.createConnection(conf); 创建这个Connection的实现类也是个重量级操作,因此,可以对它进行保留,以备下次使用这个连接类来获取Table实例。得到一个Table实例,可以使用这个方法:Table getTable(TableName tableName) throws IOException; 注意,Connection是线程安全的,而Table不是线程安全的,而且在新版API中,Connection不需要自己创建线程池来管理连接或HTable表池,这些源码里已经帮我们做好了。

下面是实现上述操作的代码:

package com.cyber_space.HBaseManager;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.HBaseConfiguration;

import org.apache.hadoop.hbase.TableName;

import org.apache.hadoop.hbase.client.Connection;

import org.apache.hadoop.hbase.client.ConnectionFactory;

import org.apache.hadoop.hbase.client.Table;

import com.cyber_space.util.Log;

import com.cyber_space.util.properties.HBaseProperties;

import com.cyber_space.util.properties.SysProperties;

public class HBaseUtil {

private static Configuration conf;

private static Connection hConnection;

static {

conf = HBaseConfiguration.create();

conf.set("hbase.zookeeper.quorum",

HBaseProperties.getValue("hbase.zookeeper.quorum"));

conf.set("hbase.zookeeper.property.clientPort",

HBaseProperties.getValue("hbase.zookeeper.property.clientPort"));

conf.set("hbase.rpc.protection",

HBaseProperties.getValue("hbase.rpc.protection"));

System.setProperty("hadoop.home.dir",

SysProperties.getValue("hadoop_home"));

try {

hConnection = ConnectionFactory.createConnection(conf);

} catch (IOException e) {

e.printStackTrace();

Log.logException(e);

}

}

public Configuration getConf() {

return conf;

}

public static Table getHTable(String tableName) throws IOException {

return hConnection.getTable(TableName.valueOf(tableName));

}

}

2.下面是得到数据表后,对表的一些操作,比较简单。参数row代表行键,family代表列族,qualifier代表列名,value是要对列增加的值。这里还要注意一下,这里批量添加数据的方法并不是事务的。

package com.cyber_space.HBaseManager;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

import org.apache.hadoop.hbase.client.Delete;

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.Table;

import org.apache.hadoop.hbase.util.Bytes;

public class FileHTable {

private Table mTable;

public FileHTable(String tableName) throws IOException {

this.mTable = HBaseUtil.getHTable(tableName);

}

/**

* 添加一行记录

* @param row

* @param familiy

* @param qualifier

* @param value

* @throws IOException

*/

public void putData(String row, String familiy, String qualifier,

String value) throws IOException {

Put put = new Put(Bytes.toBytes(row));

put.addColumn(Bytes.toBytes(familiy), Bytes.toBytes(qualifier),

Bytes.toBytes(value));

mTable.put(put);

}

/**

* 添加多行记录

* @param row

* @param family

* @param map

* @throws Exception

*/

public void putData(String row, String family, Map map)

throws Exception {

if (row == null || row.equals("") || family == null

|| family.equals("") || map == null)

throw new Exception("null exists in the arguments");

List putList = new ArrayList();

for (Map.Entry item : map.entrySet()) {

String qualifier = item.getKey();

String value = item.getValue();

Put put = new Put(Bytes.toBytes(row));

put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier),

Bytes.toBytes(value));

putList.add(put);

}

mTable.put(putList);

}

/**

* 删除一行数据

* @param row

* @throws IOException

*/

public void deleteData(String row) throws IOException {

Delete delete = new Delete(Bytes.toBytes(row));

mTable.delete(delete);

}

/**

* 添加单个数据

*

* @param row

* @param family

* @param qualifier

* @param value

* @throws IOException

*/

public void setData(String row, String family, String qualifier,

String value) throws IOException {

Put put = new Put(Bytes.toBytes(row));

put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier),

Bytes.toBytes(value));

mTable.put(put);

}

/**

* 添加多个数据

*

* @param row

* @param family

* @param items

* @throws IOException

*/

public void setData(String row, String family, Map items)

throws IOException {

if (row == null || row.equals("") || family == null

|| family.equals("") || items == null || items.size() < 0)

throw new NullPointerException("argument exist null or ''");

List putList = new ArrayList();

for (Map.Entry item : items.entrySet()) {

String qualifier = item.getKey();

String value = item.getValue();

Put put = new Put(Bytes.toBytes(row));

put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier),

Bytes.toBytes(value));

putList.add(put);

}

mTable.put(putList);

}

public void close() throws IOException {

if (mTable != null) {

mTable.close();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值