一、
几个主要 Hbase API 类和数据模型之间的对应关系:
1、 HBaseAdmin
关系: org.apache.hadoop.hbase.client.HBaseAdmin
作用:提供了一个接口来管理 HBase 数据库的表信息。它提供的方法包括:创建表,删 除表,列出表项,使表有效或无效,以及添加或删除表列族成员等。
2、 HBaseConfiguration
关系: org.apache.hadoop.hbase.HBaseConfiguration
作用:对 HBase 进行配置
3、 HTableDescriptor
关系: org.apache.hadoop.hbase.HTableDescriptor
作用:包含了表的名字极其对应表的列族
4、 HColumnDescriptor
关系: org.apache.hadoop.hbase.HColumnDescriptor
作用:维护着关于列族的信息,例如版本号,压缩设置等。它通常在创建表或者为表添 加列族的时候使用。列族被创建后不能直接修改,只能通过删除然后重新创建的方式。
列族被删除的时候,列族里面的数据也会同时被删除。
5、 HTable
关系: org.apache.hadoop.hbase.client.HTable
作用:可以用来和 HBase 表直接通信。此方法对于更新操作来说是非线程安全的。
6、 Put
关系: org.apache.hadoop.hbase.client.Put
作用:用来对单个行执行添加操作
7、 Get
关系: org.apache.hadoop.hbase.client.Get
作用:用来获取单个行的相关信息
8、 Result
关系: org.apache.hadoop.hbase.client.Result
作用:存储 Get 或者 Scan 操作后获取表的单行值。使用此类提供的方法可以直接获取值 或者各种 Map 结构( key-value 对)
代码实现
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Append;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
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;
import org.junit.Test;
public class javaapidemo3 {
protected static Connection conn;
private static final String ZK_QUORUM = "hbase.zookeeper.quorum";
private static final String ZK_CLIENT_PORT = "hbase.zookeeper.property.clientPort";
private static final String HBASE_POS = "100.168.1.182";
private static final String ZK_POS = "Master:2181,Slave0:2181,Slave1:2181,Slave2:2181";
private static final String ZK_PORT_VALUE = "2181";
/*** 静态构造,在调用静态方法前运行, 初始化连接对象 * */
static {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.rootdir", "hdfs://"+ HBASE_POS + ":9000/hbase");
conf.set(ZK_QUORUM, ZK_POS);
conf.set(ZK_CLIENT_PORT, ZK_PORT_VALUE);
try {
conn = ConnectionFactory.createConnection(conf);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
HTable table = new HTable(conf, "fuzuxian");
byte[] row = Bytes.toBytes("kr001");
Put put = new Put(row);
put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("zuxianfu"));
put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("24"));
put.add(Bytes.toBytes("info"), Bytes.toBytes("number"), Bytes.toBytes(100000));
table.put(put);
table.close();
String tableName = "fuzuxian";
String rowKey = "fu";
String columnFamily = "age";
String column = "xian";
String value = "haha";
addRow(tableName , rowKey, columnFamily, column, value);
}
public static void addRow(String tableName, String rowKey, String columnFamily,
String column, String value) throws IOException {
HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
table.put(put);
table.close();
conn.close();
}
public static void getRow(String tableName, String rowKey) throws IOException {
HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
System.out.println(
"\u884c\u952e:" + new String(CellUtil.cloneRow(cell)) + "\t" +
"\u5217\u65cf:" + new String(CellUtil.cloneFamily(cell)) + "\t" +
"\u5217\u540d:" + new String(CellUtil.cloneQualifier(cell)) + "\t" +
"\u503c:" + new String(CellUtil.cloneValue(cell)) + "\t" +
"\u65f6\u95f4\u6233:" + cell.getTimestamp());
}
table.close();
conn.close();
}
public static void scanTable(String tableName) throws IOException {
HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner results = table.getScanner(scan);
for (Result result : results) {
for (Cell cell : result.rawCells()) {
System.out.println(
"\u884c\u952e:" + new String(CellUtil.cloneRow(cell)) + "\t" +
"\u5217\u65cf:" + new String(CellUtil.cloneFamily(cell)) + "\t" +
"\u5217\u540d:" + new String(CellUtil.cloneQualifier(cell)) + "\t" +
"\u503c:" + new String(CellUtil.cloneValue(cell)) + "\t" +
"\u65f6\u95f4\u6233:" + cell.getTimestamp());
}
}
results.close();
table.close();
conn.close();
}
public static void delRow(String tableName, String rowKey) throws IOException {
HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(rowKey));
table.delete(delete);
table.close();
conn.close();
}
public static void delRows(String tableName, String[] rows) throws IOException {
HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));
List list = new ArrayList();
for (String row : rows) {
Delete delete = new Delete(Bytes.toBytes(row));
list.add(delete);
}
table.delete(list);
table.close();
conn.close();
}
public static void delColumnFamily(String tableName, String columnFamily)
throws IOException {
HBaseAdmin hAdmin = (HBaseAdmin) conn.getAdmin();
hAdmin.deleteColumn(tableName, columnFamily);
conn.close();
}
public static void deleteTable(String tableName) throws IOException {
HBaseAdmin hAdmin = (HBaseAdmin) conn.getAdmin();
if (hAdmin.tableExists(tableName)) {
hAdmin.disableTable(tableName);
hAdmin.deleteTable(tableName);
System.out.println("删除" + tableName + "表成功");
conn.close();
} else {
System.out.println("需要删除的" + tableName + "表不存在");
conn.close();
System.exit(0);
}
}
public static void appendData(String tableName, String rowKey, String columnFamily,
String column, String value) throws IOException {
HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));
Append append = new Append(Bytes.toBytes(rowKey));
append.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
table.append(append);
table.close();
conn.close();
}
public static boolean checkAndPut(String tableName, String rowKey,
String columnFamilyCheck, String columnCheck, String valueCheck,
String columnFamily, String column, String value) throws IOException {
HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
boolean result = table.checkAndPut(Bytes.toBytes(rowKey), Bytes.toBytes(columnFamilyCheck),
Bytes.toBytes(columnCheck), Bytes.toBytes(valueCheck), put);
table.close();
conn.close();
return result;
}
public static boolean checkAndDelete(String tableName, String rowKey,
String columnFamilyCheck, String columnCheck, String valueCheck,
String columnFamily, String column) throws IOException {
HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(rowKey));
delete.addColumn(Bytes.toBytes(columnFamilyCheck), Bytes.toBytes(columnCheck));
boolean result = table.checkAndDelete(Bytes.toBytes(rowKey),
Bytes.toBytes(columnFamilyCheck), Bytes.toBytes(columnCheck),
Bytes.toBytes(valueCheck), delete);
table.close();
conn.close();
return result;
}
public static long incrementColumnValue(String tableName, String rowKey,
String columnFamily, String column, long amount) throws IOException {
HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));
long result = table.incrementColumnValue(Bytes.toBytes(rowKey),
Bytes.toBytes(columnFamily), Bytes.toBytes(column), amount);
table.close();
conn.close();
return result;
}
}
得到的结果:
hbase(main):005:0> describe 'fuzuxian'
Table fuzuxian is ENABLED
fuzuxian
COLUMN FAMILIES DESCRIPTION
{NAME => 'age', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => '
NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE =
> '0'}
{NAME => 'number', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING =
> 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOP
E => '0'}
2 row(s) in 0.0300 seconds
hbase(main):006:0> scan 'fuzuxian'
ROW COLUMN+CELL
fu column=age:xian, timestamp=1533539685733, value=haha
1 row(s) in 0.0780 seconds
参考博客:
http://www.cnblogs.com/liuwei6/p/6842536.html