创建Maven Demo项目,在pom.xml中引入HBase Client jar包:
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.1.2</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.6.1</version>
</dependency>
HBase Client API 示例代码:
package com.xinyuan.hadoop.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
/**
* HBase客户端工具类
*
* Created by sofar on 15-12-1.
*/
public class HBaseClientUtils {
private static Logger logger = LoggerFactory.getLogger(HBaseClientUtils.class);
private static Configuration configuration = null;
private static Connection connection = null;
static {
try {
//创建hadoop配置对象
configuration = HBaseConfiguration.create();
//设置HBase Zookeeper集群主机名称,多个主机以逗号隔开,也可以将hbase-site.xml文件放入类路径加载.
//configuration.set("hbase.zookeeper.quorum", "hadoop-master,hadoop-slave1,hadoop-slave2");
configuration.iterator().forEachRemaining((entry) -> {
logger.debug("链接配置[Key={}, value={}]", entry.getKey(), entry.getValue());
});
logger.debug("初始化HBase链接,配置文件[{}]", configuration);
//创建HBase Client链接对象
connection = ConnectionFactory.createConnection(configuration);
} catch (IOException e) {
logger.error("初始化连接失败,失败原因[{}]", e.getMessage());
e.printStackTrace();
}
}
public static Connection getConnection() throws IOException {
if (connection == null) {
logger.error("获取链接失败,请检查配置!");
throw new HBaseIOException("获取HBase链接失败");
}
return connection;
}
public static boolean closeConnection() throws IOException {
if (connection != null) {
connection.close();
}
return connection.isClosed() ? true : false;
}
public static void create(String tableName, String columnFamily) throws IOException {
//HBaseAdmin admin = new HBaseAdmin(configuration);
Admin admin = getConnection().getAdmin();
if (admin.tableExists(TableName.valueOf(tableName))) {
logger.warn("表【{}】已存在", tableName);
} else {
HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
desc.addFamily(new HColumnDescriptor(columnFamily));
admin.createTable(desc);
logger.debug("表【{}】已创建", tableName);
}
}
public static void put(String tableName, String row, String columnFamily, String column, String data) throws IOException {
Table table = getConnection().getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(row));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(data));
table.put(put);
logger.debug("表【{}】插入数据成功,数据为【row={}, columnFamily={}, column={}】", new String[] {tableName, row, columnFamily, column});
}
/**
* 根据表名称和行值获取记录
* @param tableName
* @param row
* @throws IOException
*/
public static void get(String tableName, String row) throws IOException {
Table table = getConnection().getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(row));
logger.debug("查询结果【{}】", Bytes.toString(table.get(get).value()));
}
}