最近项目中使用了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表池,这些源码里已经帮我们做好了。
下面是实现上述操作的代码:
[mw_shl_code=bash,true]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));
}
}
[/mw_shl_code]
2.下面是得到数据表后,对表的一些操作,比较简单。参数row代表行键,family代表列族,qualifier代表列名,value是要对列增加的值。这里还要注意一下,这里批量添加数据的方法并不是事务的。
[mw_shl_code=bash,true]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<String, String> map)
throws Exception {
if (row == null || row.equals("") || family == null
|| family.equals("") || map == null)
throw new Exception("null exists in the arguments");
List<Put> putList = new ArrayList<Put>();
for (Map.Entry<String, String> 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<String, String> 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<Put> putList = new ArrayList<Put>();
for (Map.Entry<String, String> 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();
}
}
}
[/mw_shl_code]
|