首先需要将hbase安装目录下lib文件夹中的jar文件全部加入到项目类路径下,另外还需要将hadoop相关jar包也加入。
这里需要用到的主要API介绍一下。
Configuration:HBase参数配置对象。
Connection:HBase连接对象,通过ConnectionFactory工厂方法创建,需要传入一个参数配置对象。
Admin:通过Admin实现创建表,删除表操作。
TableName:取代字符串形式的tableName。
Table:使用Table可以新增数据put(),查询数据getScanner()。
下面通过代码来演示如何做常规的操作:
1、初始化配置,构建一个HBase连接器对象。
private static Connection connection;
static{
Configuration config = HBaseConfiguration.create();
config.set("hbase.rootdir", "hdfs://server:9000/hbase");
config.set("hbase.zookeeper.quorum", "server:2181");
try {
connection = ConnectionFactory.createConnection(config);
} catch (IOException e) {
e.printStackTrace();
}
}
2、建表。
public static boolean createTable(String tableName,String columnFamily){
boolean success = false;
Admin admin = null;
TableName tn = TableName.valueOf(tableName);
try {
admin = connection.getAdmin();
if(!admin.tableExists(tn)){
HTableDescriptor desc = new HTableDescriptor(tn);
desc.addFamily(new HColumnDescriptor(columnFamily.getBytes()));
admin.createTable(desc);
System.out.println("table ["+tableName+"] create ok.");
success = true;
}else{
System.out.println("table ["+tableName+"] already exists.");
success = false ;
}
} catch (Exception e) {
success = false ;
}
return success;
}
3、插入数据。
public static boolean putData(String tableName,String rowKey,String columnFamily,String qualifier,Object value){
boolean success = false;
Admin admin = null;
TableName table = TableName.valueOf(tableName);
try {
admin = connection.getAdmin();
if(admin.tableExists(table)){
Table t = connection.getTable(table);
Put put = new Put(rowKey.getBytes());
put.addColumn(columnFamily.getBytes(), qualifier.getBytes(), value.toString().getBytes());
t.put(put);
success = true;
}else{
System.out.println("table ["+tableName+"] does not exist.");
success = false;
}
} catch (Exception e) {
success = false;
}
return success;
}
4、查看数据。
@SuppressWarnings("deprecation")
public static void getValueByTable(String tableName){
TableName tn = TableName.valueOf(tableName.getBytes());
try{
Table table = connection.getTable(tn);
ResultScanner rs = table.getScanner(new Scan());
for(Result r:rs){
System.out.println("row:"+new String(r.getRow()));
for(KeyValue kv:r.raw()){
System.out.println("column family:"+new String(kv.getFamilyArray())
+" qualifier "+new String(kv.getQualifierArray())
+" value "+new String(kv.getValueArray()));
}
}
}catch(Exception e){
e.printStackTrace();
}
}
5、删除表。
public static boolean dropTable(String tableName){
boolean success = false;
Admin admin = null;
try {
admin = connection.getAdmin();
TableName table = TableName.valueOf(tableName);
if(admin.tableExists(table)){
admin.disableTable(table);
admin.deleteTable(table);
success = true;
}
} catch (Exception e) {
success = false;
}finally{
IOUtils.closeQuietly(admin);
}
return success;
}
6、以上几段代码展示了,如何初始化hbase连接,创建表,添加数据,查看数据,删除表的操作,下面通过运行,查看效果。这里代码在windows的eclipse ide编码的,为了演示,我们通过ant构建插件sshexec来将代码直接提交到hbase服务器上运行。这里给出完整的构建脚本build.xml。
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." name="hbase" default="sshexec">
<property name="main.class" value="com.xxx.hbase.test.HBaseTest"/>
<property name="src.dir" value="${basedir}/src" />
<property name="java.lib.dir" value="${basedir}/lib"/>
<property name="localpath.dir" value="${basedir}"/>
<property name="dest.dir" value="${basedir}/dest"/>
<property name="classes.dir" value="${basedir}/classes"/>
<property name="third.lib.dir" value="${basedir}/lib"/>
<property name="remote.host" value="10.119.9.149"/>
<property name="remote.username" value="root"/>
<property name="remote.password" value="root"/>
<property name="remote.home" value="~"/>
<path id="compile.classpath">
<fileset dir="${java.lib.dir}">
<include name="tools.jar"/>
</fileset>
<fileset dir="${third.lib.dir}">
<include name="*.jar"/>
</fileset>
</path>
<path id="run.classpath">
<path refid="compile.classpath"/>
<pathelement location="${classes.dir}" />
</path>
<target name="clean">
<delete dir="${classes.dir}"/>
<delete dir="${dest.dir}"/>
<echo level="info">清理完毕</echo>
</target>
<target name="init" depends="clean">
<mkdir dir="${classes.dir}"/>
<mkdir dir="${dest.dir}"/>
</target>
<target name="compile" depends="init">
<javac srcdir="${src.dir}" destdir="${classes.dir}" source="1.8" target="1.8"
includeAntRuntime="false" debug="false" verbose="false" encoding="UTF-8">
<classpath refid="compile.classpath"/>
</javac>
</target>
<target name="build" depends="compile">
<jar jarfile="${dest.dir}/hbasetest.jar">
<fileset dir="${classes.dir}" includes="**/*.*"/>
</jar>
</target>
<target name="ssh" depends="build">
<scp file="${dest.dir}/hbasetest.jar"
todir="${remote.username}@${remote.host}:${remote.home}"
password="${remote.password}" trust="true"/>
</target>
<target name="sshexec" depends="ssh">
<sshexec host="${remote.host}" username="${remote.username}"
password="${remote.password}" trust="true"
command="hadoop jar ${remote.home}/hbasetest.jar ${main.class}"/>
</target>
</project>
7、先运行创建表的任务。
运行截图:
生成表:
8、再运行添加数据方法:
运行构建成功截图:
验证数据是否添加:
9、运行查看数据:
构建结果:
10、删除表,调用删除表api之前,需要先调用禁用表api方法。
构建截图:
验证是否删除:
最后,附上完整的java代码部分:
package com.xxx.hbase.test;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
public class HBaseTest {
private static Connection connection;
static{
Configuration config = HBaseConfiguration.create();
config.set("hbase.rootdir", "hdfs://server:9000/hbase");
config.set("hbase.zookeeper.quorum", "server:2181");
try {
connection = ConnectionFactory.createConnection(config);
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean createTable(String tableName,String columnFamily){
boolean success = false;
Admin admin = null;
TableName tn = TableName.valueOf(tableName);
try {
admin = connection.getAdmin();
if(!admin.tableExists(tn)){
HTableDescriptor desc = new HTableDescriptor(tn);
desc.addFamily(new HColumnDescriptor(columnFamily.getBytes()));
admin.createTable(desc);
System.out.println("table ["+tableName+"] create ok.");
success = true;
}else{
System.out.println("table ["+tableName+"] already exists.");
success = false ;
}
} catch (Exception e) {
success = false ;
}
return success;
}
public static boolean dropTable(String tableName){
boolean success = false;
Admin admin = null;
try {
admin = connection.getAdmin();
TableName table = TableName.valueOf(tableName);
if(admin.tableExists(table)){
admin.disableTable(table);
admin.deleteTable(table);
success = true;
}
} catch (Exception e) {
success = false;
}finally{
IOUtils.closeQuietly(admin);
}
return success;
}
public static boolean putData(String tableName,String rowKey,
String columnFamily,String qualifier,Object value){
boolean success = false;
Admin admin = null;
TableName table = TableName.valueOf(tableName);
try {
admin = connection.getAdmin();
if(admin.tableExists(table)){
Table t = connection.getTable(table);
Put put = new Put(rowKey.getBytes());
put.addColumn(columnFamily.getBytes(),
qualifier.getBytes(), value.toString().getBytes());
t.put(put);
success = true;
}else{
System.out.println("table ["+tableName+"] does not exist.");
success = false;
}
} catch (Exception e) {
success = false;
}
return success;
}
@SuppressWarnings("deprecation")
public static void getValueByTable(String tableName){
TableName tn = TableName.valueOf(tableName.getBytes());
try{
Table table = connection.getTable(tn);
ResultScanner rs = table.getScanner(new Scan());
for(Result r:rs){
System.out.println("row:"+new String(r.getRow()));
for(KeyValue kv:r.raw()){
System.out.println("column family:"
+new String(kv.getFamilyArray())
+" qualifier "+new String(kv.getQualifierArray())
+" value "+new String(kv.getValueArray()));
}
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception{
//createTable("stu", "info");
//putData("stu", "1", "info", "id",1);
//putData("stu", "1", "info", "name","xxx");
//getValueByTable("stu");
dropTable("stu");
}
}