Hadoop Hbase通过行关键字、列(列族名:列名)和时间戳的三元组确定一个存储单元(cell),即由
{row key, column family, column name, timestamp} 可以唯一确定一个存储值,即一个键值对:
{row key, column family, column name, timestamp} -> value
下面演示了Hbase的基本操作。包括
1、创建表
2、删除表
3、添加记录
4、删除记录
5、查询记录等
忘记的时候可以过来看看。
- 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.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;
- public class HBaseOperation {
- // 相关属性
- private Configuration conf;
- private HBaseAdmin admin;
- public HBaseOperation(Configuration conf) throws IOException {
- this.conf = HBaseConfiguration.create(conf);
- this.conf.set("hbase.zookeeper.quorum", "namenode");
- this.conf.set("hbase.zookeeper.property.clientPort", "2181");
- this.admin = new HBaseAdmin(this.conf);
- }
- public HBaseOperation() throws IOException {
- Configuration cnf = new Configuration();
- this.conf = HBaseConfiguration.create(cnf);
- this.conf.set("hbase.zookeeper.quorum", "namenode");
- this.conf.set("hbase.zookeeper.property.clientPort", "2181");
- this.admin = new HBaseAdmin(this.conf);
- }
- /**
- * 创建表
- * @param tableName 表名
- * @param colFamilies <span style="white-space:pre"> </span>列族
- * @throws IOException
- */
- public void createTable(String tableName, String colFamilies[])
- throws IOException {
- if (this.admin.tableExists(tableName)) { // 表已经存在
- System.out.println("Table: " + tableName + " already exists !");
- this.admin.deleteTable(tableName); // 删除表
- }
- HTableDescriptor dsc = new HTableDescriptor(tableName); // 新建表描述
- int len = colFamilies.length;
- for (int i = 0; i < len; i++) {
- HColumnDescriptor family = new HColumnDescriptor(colFamilies[i]);
- dsc.addFamily(family); // 在表描述中添加列族
- }
- admin.createTable(dsc); // 创建表
- System.out.println("create table " + tableName + " ok.");
- }
- /**
- * 删除一张表
- * @param tableName
- * @throws IOException
- */
- public void deleteTable(String tableName) throws IOException {
- if (this.admin.tableExists(tableName)) {
- admin.disableTable(tableName);
- admin.deleteTable(tableName);
- System.out.println("delete table " + tableName + " ok.");
- } else {
- System.out.println("Table Not Exists !");
- }
- }
- /**
- * 添加记录
- * @param tableName 表名字
- * @param rowkey 行关键字
- * @param family 列族
- * @param qualifier 列名
- * @param value 值
- * @throws IOException
- */
- public void insertRecord(String tableName, String rowkey, String family,
- String qualifier, String value) throws IOException {
- HTable table = new HTable(this.conf, tableName);
- Put put = new Put(rowkey.getBytes());
- put.add(family.getBytes(), qualifier.getBytes(), value.getBytes());
- table.put(put);
- System.out.println("insert recored " + rowkey + " to table "
- + tableName + " ok.");
- table.close();
- }
- /**
- * 添加一组记录
- * @param tableName
- * @param putList
- * @throws IOException
- */
- public void insertRecordList(String tableName, List<Put> putList)
- throws IOException {
- HTable table = new HTable(this.conf, tableName);
- for (int i = 0; i < putList.size(); i++) {
- Put put = putList.get(i);
- table.put(put);
- }
- putList.clear();
- table.close();
- }
- /**
- * 删除一行数据
- * @param tableName 表名
- * @param rowkey 行关键字
- * @throws IOException
- */
- public void deleteRecord(String tableName, String rowkey)
- throws IOException {
- HTable table = new HTable(this.conf, tableName);
- Delete del = new Delete(rowkey.getBytes());
- table.delete(del);
- System.out.println("del recored " + rowkey + " ok.");
- table.close();
- }
- /**
- * 获取一条记录
- * @param tableName 表名
- * @param rowkey 行关键字
- * @return
- * @throws IOException
- */
- public Result getOneRecord(String tableName, String rowkey)
- throws IOException {
- HTable table = new HTable(this.conf, tableName);
- Get get = new Get(rowkey.getBytes()); //get对象
- Result rs = table.get(get);
- 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()));
- }
- table.close();
- return rs;
- }
- /**
- * 获取一张表的所有数据
- * @param tableName
- * @return
- * @throws IOException
- */
- public List<Result> getAllRecord(String tableName) throws IOException {
- HTable table = new HTable(this.conf, tableName);
- Scan scan = new Scan(); //Scan对象
- ResultScanner scanner = table.getScanner(scan);
- List<Result> list = new ArrayList<Result>();
- for (Result r : scanner) {
- list.add(r);
- }
- scanner.close();
- table.close();
- for (Result r : scanner) {
- for (KeyValue kv : r.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()));
- }
- }
- return list;
- }
- }