下面代码提供了操作HBase表最基本功能:增删改差。废话不多说,直接上代码。
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
public class OperateTable {
private static Configuration conf = HBaseConfiguration.create();
private static Connection connection = null;
private static Admin admin = null;
private static TableName tableName = TableName.valueOf("my_ns:mytable");
public static void main(String[] args) throws Exception {
deleteTable();
createTable();
addData();
deleteData();
retrieveCells();
readValueFromSpecifiedCell();
}
//创建表
public static void createTable() throws Exception {
connection = ConnectionFactory.createConnection(conf);
admin = connection.getAdmin();
admin.createNamespace(NamespaceDescriptor.create("my_ns").build());
HTableDescriptor htd = new HTableDescriptor(tableName);
HColumnDescriptor hcd1 = new HColumnDescriptor("foo");
HColumnDescriptor hcd2 = new HColumnDescriptor("bar");
htd.addFamily(hcd1);
htd.addFamily(hcd2);
admin.createTable(htd);
System.out.println("Table created");
admin.close();
connection.close();
}
//删除表
public static void deleteTable() throws Exception {
connection = ConnectionFactory.createConnection(conf);
admin = connection.getAdmin();
if (admin.tableExists(tableName)) {
try {
admin.disableTable(tableName);
admin.deleteTable(tableName);
} catch (Exception e) {
e.printStackTrace();
}
}
// delete existed namespace my_ns
admin.deleteNamespace("my_ns");
admin.close();
}
//删除表数据
public static void deleteData() throws Exception {
connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(tableName);
Delete d = new Delete(Bytes.toBytes("rk1"));
//删除指定列的多个版本(To delete multiple versions of specific columns),
//一个版本(version)对于一个timestamp
d.addColumns(Bytes.toBytes("foo"), Bytes.toBytes("age"));
table.delete(d);
table.close();
}
//修改表,删除一个列族
public static void modifyTable() throws Exception {
connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
if (admin.tableExists(tableName)) {
admin.disableTable(tableName);
HTableDescriptor htd = admin.getTableDescriptor(tableName);
//remove HColumnDescriptor bar
htd.removeFamily(Bytes.toBytes("bar"));
//create HColumnDescriptor baz for new column family
HColumnDescriptor hcd = new HColumnDescriptor("baz");
htd.addFamily(hcd);
//modify target table struture
admin.modifyTable(tableName, htd);
admin.enableTable(tableName);
admin.close();
connection.close();
}
}
//新增、更新数据
public static void addData() throws Exception {
connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(tableName);
// Create a Put operation for the specified row key rk1
Put put1 = new Put(Bytes.toBytes("rk1"));
//Add the specified column and value to this Put operation.
put1.addColumn(Bytes.toBytes("foo"), Bytes.toBytes("name"), Bytes.toBytes("Andrew"));
put1.addColumn(Bytes.toBytes("foo"), Bytes.toBytes("address"), Bytes.toBytes("Irving"));
put1.addColumn(Bytes.toBytes("foo"), Bytes.toBytes("age"), Bytes.toBytes("23"));
// Create Put operation for the specified row key rk1
Put put2 = new Put(Bytes.toBytes("rk1"));
put2.addColumn(Bytes.toBytes("foo"), Bytes.toBytes("id"), Bytes.toBytes("AC123"));
put2.addColumn(Bytes.toBytes("foo"), Bytes.toBytes("workshop"), Bytes.toBytes("AI"));
// Create Put operation for the specified row key rk2
Put put3 = new Put(Bytes.toBytes("rk2"));
put3.addColumn(Bytes.toBytes("bar"), Bytes.toBytes("name"), Bytes.toBytes("Lisa"));
put3.addColumn(Bytes.toBytes("bar"), Bytes.toBytes("address"), Bytes.toBytes("Long Beach"));
put3.addColumn(Bytes.toBytes("bar"), Bytes.toBytes("age"), Bytes.toBytes("20"));
table.put(put1);
table.put(put2);
table.put(put3);
table.close();
}
//遍历Cell数据
public static void retrieveCells() throws Exception {
connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(tableName);
Get g = new Get(Bytes.toBytes("rk1"));
Result r = table.get(g);
for (Cell cell : r.rawCells()) {
System.out.println(
"rowkey : " + Bytes.toString(r.getRow()) +
" family:qualifier : " + Bytes.toString(CellUtil.cloneQualifier(cell)) +
" value : " + Bytes.toString(CellUtil.cloneValue(cell))
);
}
}
//读取指定Cell中对应的值
public static void readValueFromSpecifiedCell() throws Exception {
connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(tableName);
Get g = new Get(Bytes.toBytes("rk2"));
g.addFamily(Bytes.toBytes("bar"));
Result r = table.get(g);
byte[] b = r.getValue(Bytes.toBytes("bar"), Bytes.toBytes("name"));
System.out.println("name : " + Bytes.toString(b));
}
}
运行结果如下: