1 开发环境
在进行Hbase开发前,需要安装JDK、Hadoop和HBase,选择一款合适的开发IDE,具体安装方法就不介绍了,网上有很多参考资料,这里给出我的开发环境:
操作系统:Ubuntu 14.04 LTS
Java版本:jdk1.7.0_79
Hadoop版本:hadoop-2.6.0-cdh5.7.1
HBase版本:hbase-1.2.0-cdh5.7.1
Ecipse版本:Eclipse Java EE LunaRelease
使用Maven构建项目,在pom.xml中添加hbase的依赖如下:
<repositories>
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.0-cdh5.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.6.0-cdh5.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.0-cdh5.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.2.0-cdh5.7.1</version>
</dependency>
</dependencies>
2 初始化配置
首先需要设置HBase的配置,如ZooKeeper的地址、端口号等等。可以通过org.apache.hadoop.conf.Configuration.set方法手工设置HBase的配置信息,也可以直接将HBase的hbase-site.xml配置文件引入项目即可。下面给出配置代码:
// 声明静态配置
private static Configuration conf = null;
static {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "localhost");
conf.set("hbase.zookeeper.property.clientPort", "2181");
}
3 常见API的使用
HBase的常用操作包括建表、插入表数据、删除表数据、获取一行数据、表扫描、删除列族、删除表等等,下面给出具体代码。
3.1 创建数据库表
// 创建数据库表
public static void createTable(String tableName, String[] columnFamilys) throws IOException {
// 建立一个数据库的连接
Connection conn = ConnectionFactory.createConnection(conf);
// 创建一个数据库管理员
HBaseAdmin hAdmin = (HBaseAdmin) conn.getAdmin();
if (hAdmin.tableExists(tableName)) {
System.out.println(tableName + "表已存在");
conn.close();
System.exit(0);
} else {
// 新建一个表描述
HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tableName));
// 在表描述里添加列族
for (String columnFamily : columnFamilys) {
tableDesc.addFamily(new HColumnDescriptor(columnFamily));
}
// 根据配置好的表描述建表
hAdmin.createTable(tableDesc);
System.out.println("创建" + tableName + "表成功");
}
conn.close();
}
3.2 添加一条数据
// 添加一条数据
public static void addRow(String tableName, String rowKey, String columnFamily, String column, String value)
throws IOException {
// 建立一个数据库的连接
Connection conn = ConnectionFactory.createConnection(conf);
// 获取表
HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));
// 通过rowkey创建一个put对象
Put put = new Put(Bytes.toBytes(rowKey));
// 在put对象中设置列族、列、值
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
// 插入数据,可通过put(List<Put>)批量插入
table.put(put);
// 关闭资源
table.close();
conn.close();
}
3.3 获取一条数据
// 通过rowkey获取一条数据
public static void getRow(String tableName, String rowKey) throws IOException {
// 建立一个数据库的连接
Connection conn = ConnectionFactory.createConnection(conf);
// 获取表
HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));
// 通过rowkey创建一个get对象
Get get = new Get(Bytes.toBytes(rowKey));
// 输出结果
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
System.out.println(
"行键:" + new String(CellUtil.cloneRow(cell)) + "\t" +
"列族:" + new String(CellUtil.cloneFamily(cell)) + "\t" +
"列名:" + new String(CellUtil.cloneQualifier(cell)) + "\t" +
"值:" + new String(CellUtil.cloneValue(cell)) + "\t" +
"时间戳:" + cell.getTimestamp());
}
// 关闭资源
table.close();
conn.close();
}
3.4 全表扫描
// 全表扫描
public static void scanTable(String tableName) throws IOException {
// 建立一个数据库的连接
Connection conn = ConnectionFactory.createConnection(conf);
// 获取表
HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));
// 创建一个扫描对象
Scan scan = new Scan();
// 扫描全表输出结果
Resu