HBase提供了Java API,可以通过API来实现对HBase的一些操作。
package hbase;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
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 HBaseOperation {
private Configuration config;
private HBaseAdmin admin;
public HBaseOperation() {
}
public HBaseOperation(Configuration config)
throws MasterNotRunningException, ZooKeeperConnectionException,
IOException {
this.config = config;
this.admin = new HBaseAdmin(config);
}
/**
* 创建表
*
* @param tableName
* 表名
* @param family
* 列族名
*/
public void createTable(String tableName, String[] colFamilies)
throws IOException {
HTableDescriptor desc = new HTableDescriptor(
TableName.valueOf(tableName));
for (int i = 0; i < colFamilies.length; i++) {
desc.addFamily(new HColumnDescriptor(colFamilies[i]));
}
if (this.admin.tableExists(tableName)) {
System.out.println("table exists!");
System.exit(0);
} else {
this.admin.createTable(desc);
System.out.println("create table success!");
}
}
/**
* 删除表
*
* @param tableName
* 表名
*/
public void deleteTable(String tableName) throws IOException {
if (this.admin.tableExists(tableName)) {
admin.deleteTable(tableName);
System.out.println("delete table success!");
} else {
System.out.println("table not exists!");
}
}
/**
*
* @param tableName
* 表名
* @param rowkey
* 行键
* @param family
* 列族名
* @param qualifier
* 列名
* @param value
* 列的值
*/
public void insertRecord(String tableName, String rowkey, String family,
String qualifier, String value) throws IOException {
HTable table = new HTable(config, tableName);// HTabel负责跟记录相关的操作如增删改查等
Put put = new Put(Bytes.toBytes(rowkey));// 设置rowkey
put.add(Bytes.toBytes(family), Bytes.toBytes(qualifier),
Bytes.toBytes(value));// 将指定的列族中的某一列以及该列的值添加到put实例中
table.put(put);
System.out.println("insert record success!");
table.close();
}
/**
* 根据rowkey查询某一行记录
*
* @param tableName
* 表名
* @param rowkey
* 行键
* @return
*/
public Result getRecord(String tableName, String rowkey) throws IOException {
HTable table = new HTable(config, tableName);
Get get = new Get(Bytes.toBytes(rowkey));
Result result = table.get(get);
table.close();
return result;
}
/**
* 查询某一列的值
*
* @param tableName
* 表名
* @param rowkey
* 行键
* @param family
* 列族名
* @param qualifier
* 列名
*/
public Result getRecordByColumn(String tableName, String rowkey,
String family, String qualifier) throws IOException {
HTable table = new HTable(config, tableName);
Get get = new Get(Bytes.toBytes(rowkey));
get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
Result result = table.get(get);
table.close();
return result;
}
/**
* 遍历查询所有记录
*
* @param tableName
* 表名
*/
public List<Result> getAllRecord(String tableName) throws IOException {
HTable table = new HTable(config, tableName);
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
List<Result> list = new ArrayList<Result>();
for (Result result : scanner) {
list.add(result);
}
table.close();
return list;
}
/**
* 删除一行记录
*
* @param tableName
* 表名
* @param rowkey
* 行键
*/
public void deleteRow(String tableName, String rowkey) throws IOException {
HTable table = new HTable(config, tableName);
Delete delete = new Delete(Bytes.toBytes(rowkey));
table.delete(delete);
System.out.println("row are deleted!");
table.close();
}
public void deleteColumn(String tableName, String rowkey, String family,
String qualifier) throws IOException {
HTable table = new HTable(config, tableName);
Delete delete = new Delete(Bytes.toBytes(rowkey));
delete.deleteColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
table.delete(delete);
System.out.println("clomun are deleted!");
table.close();
}
}
测试类:
package hbase;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Result;
public class HBaseTest {
public static void main(String[] args) throws MasterNotRunningException,
ZooKeeperConnectionException, IOException {
// 1.初始化HBaseOperation
Configuration config = new Configuration();
config.set("hbase.zookeeper.quorum", "H01,H02,H03");
config.set("hbase.zookeeper.property.clientPort", "2181");
HBaseOperation hbase = new HBaseOperation(config);
// 2. 测试相应操作
// 2.1 创建表
String tableName = "blog";
String colFamilies[] = { "article", "author" };
hbase.createTable(tableName, colFamilies);
// 2.2 插入一条记录
hbase.insertRecord(tableName, "row1", "article", "title", "hadoop");
hbase.insertRecord(tableName, "row1", "author", "name", "tom");
hbase.insertRecord(tableName, "row1", "author", "nickname", "tt");
// 2.2 查询一条记录
Result rs1 = hbase.getRecord(tableName, "row1");
for (Cell cell : rs1.rawCells()) {
System.out.println(new String(cell.getRowArray()));
System.out.println(new String(cell.getFamilyArray()));
System.out.println(new String(cell.getQualifierArray()));
System.out.println(new String(cell.getValueArray()));
}
// 2.3 查询所有记录
List<Result> list = hbase.getAllRecord(tableName);
Iterator<Result> it = list.iterator();
while (it.hasNext()) {
Result rs2 = it.next();
for (Cell cell : rs2.rawCells()) {
System.out.println("row key is : "
+ new String(cell.getRowArray()));
System.out.println("family is : "
+ new String(cell.getFamilyArray()));
System.out.println("qualifier is:"
+ new String(cell.getQualifierArray()));
System.out.print("timestamp is:" + cell.getTimestamp());
System.out.println("Value is : "
+ new String(cell.getValueArray()));
}
}
}
}
242



被折叠的 条评论
为什么被折叠?



