java客户端操作Hbase
首先,配置好 hadoop+hbase 环境后,确保 hbase 正常启动。
1. 搭建开发环境
1.1、运行 Eclipse,创建一个新的 Java工程“ HBaseHelloWorld”,右键项目根目录,选择 “ Properties”->“Java Build Path”->“Library”->“Add External JARs”,将 HBase解压后根目录下的 hbase-***.jar、 hbase-***-tests.jar和 lib子目录下所有 jar 包添加到本工程的 Classpath下,如图 1所示。
1.2、按照步骤 1中的操作,将自己所连接的 HBase的配置文件 hbase-site.xml添加到本工程的 Classpath中,如下所示为配置文件的一个示例。
<configuration>
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<property>
<name>hbase.rootdir</name>
<value>hdfs://hostname:9000/hbase</value>
</property>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
</configuration>
2.下面可以在 Eclipse环境下进行 HBase编程了。
具体操作 hbase代码如下:
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
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.util.Bytes;
public class HBaseJavaAPI {
/**
* 创建表
* @throws IOException
*/
public void createTable(String tableName, String[] columns) throws IOException {
Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(conf);
if (admin.tableExists(tableName)) {
System.out.println("表已经存在!");
} else {
HTableDescriptor desc = new HTableDescriptor(tableName);
for (String column : columns) {
desc.addFamily(new HColumnDescriptor(column));
}
admin.createTable(desc);
System.out.println("表创建成功!");
}
}
/**
* 插入一行记录
* @param tablename
* 表名
* @param row
* 行名称
* @param columnFamily
* 列族名
* @param columns
* (列族名:column)组合成列名
* @param values
* 行与列确定的值
*/
public void insertRecord(String tablename, String row, String columnFamily, String[] columns, String[] values) {
try {
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, tablename);
Put put = new Put(Bytes.toBytes(row));
for (int i = 0; i < columns.length; i++) {
put.add(Bytes.toBytes(columnFamily),
Bytes.toBytes(String.valueOf(columns[i])),
Bytes.toBytes(values[i]));
table.put(put);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除一行记录
* @param tablename
* 表名
* @param rowkey
* 行名
* @throws IOException
*/
public void deleteRow(String tablename, String rowkey) throws IOException {
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, tablename);
List list = new ArrayList();
Delete d1 = new Delete(rowkey.getBytes());
list.add(d1);
table.delete(list);
System.out.println("删除行成功!");
}
/**
* 查找一行记录
* @param tablename
* 表名
* @param rowkey
* 行名
*/
public static void selectRow(String tablename, String rowKey)
throws IOException {
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, tablename);
Get g = new Get(rowKey.getBytes());
Result rs = table.get(g);
for (KeyValue kv : rs.raw()) {
System.out.print(new String(kv.getRow()) + " ");
System.out.print(new String(kv.getFamily()) + ":");
System.out.print(new String(kv.getQualifier()) + " ");
System.out.print(kv.getTimestamp() + " ");
System.out.println(new String(kv.getValue()));
}
}
/**
* 查询表中所有行
* @param tablename
*/
public void scanAllRecord(String tablename) {
try {
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, tablename);
Scan s = new Scan();
ResultScanner rs = table.getScanner(s);
for (Result r : rs) {
KeyValue[] kv = r.raw();
for (int i = 0; i < kv.length; i++) {
System.out.print(new String(kv[i].getRow()) + " ");
System.out.print(new String(kv[i].getFamily()) + ":");
System.out.print(new String(kv[i].getQualifier()) + " ");
System.out.print(kv[i].getTimestamp() + " ");
System.out.println(new String(kv[i].getValue()));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除表操作
* @param tablename
* @throws IOException
*/
public void deleteTable(String tablename) throws IOException {
try {
Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(conf);
admin.disableTable(tablename);
admin.deleteTable(tablename);
System.out.println("表删除成功!");
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
}
}
}