對hbase進行增刪改查的操作例項
程式碼例項
定義連線需要的靜態常量
private static final String TABLE_NAME = “Student”;
private static final String[] FAMILYS = { “name”, “profile”, “score” };
private static final String[] ROW_KEYS = { “11”, “20”, “32” };
//建立HBaseConfiguration物件
Configuration conf = HBaseConfiguration.create();
//建立zookeeper的ip
conf.set(“hbase.zookeeper.quorum”, “localhost”);
// 建立連線物件
Connection conn = ConnectionFactory.createConnection(conf);
// 呼叫建立表格的方法
// createTable(conn);
String[][] columns1 = { { “name”, “”, “John” }, { “profile”, “age”, “20” }, { “profile”, “class”, “class1” },
{ “score”, “math”, “100” }, { “score”, “lang”, “95” } };
String[][] columns2 = { { “name”, “”, “Marry” }, { “name”, “alias”, “MY” }, { “profile”, “gender”, “female” },
{ “profile”, “class”, “class2” }, { “score”, “math”, “93” }, { “score”, “eng”, “90” } };
String[][] columns3 = { { “name”, “”, “Peter” }, { “profile”, “class”, “class3” }, { “score”, “math”, “87” },
{ “score”, “lang”, “85” } };
// 插入資料
// insert(conn,ROW_KEYS[0],columns1);
// insert(conn,ROW_KEYS[1],columns2);
// insert(conn,ROW_KEYS[2],columns3);
getDataByRK(conn, “11”);
//建立表格的方法
public static void createTable(Connection conn) throws IOException {
// 通過連線物件獲取表格管理物件Admin
Admin admin = conn.getAdmin();
// 建立一個表格的描述物件
TableName tableName = TableName.valueOf(TABLE_NAME);
HTableDescriptor tDesc = new HTableDescriptor(tableName);
// 建立列描述物件
HColumnDescriptor columnDesc1 = new HColumnDescriptor(Bytes.toBytes(FAMILYS[0]));
HColumnDescriptor columnDesc2 = new HColumnDescriptor(Bytes.toBytes(FAMILYS[1]));
HColumnDescriptor columnDesc3 = new HColumnDescriptor(Bytes.toBytes(FAMILYS[2]));
// 將列描述物件新增到表格描述物件中
tDesc.addFamily(columnDesc1);
tDesc.addFamily(columnDesc2);
tDesc.addFamily(columnDesc3);
// 呼叫Admin物件的建立表格的方法,建立表格
admin.createTable(tDesc);
// 關閉admin物件
admin.close();
}
向表格中插入資料
ublic static void insert(Connection conn, String rowKey, String[][] data) throws IOException {
// 通過連線物件獲取Table物件
Table table = conn.getTable(TableName.valueOf(TABLE_NAME));
// 建立Put物件
Put put = new Put(Bytes.toBytes(rowKey));
// 通過迴圈向Put物件中新增資料
for (String[] array : data) {
// put.add(family, qualifier, value)
// family->列族,qualifier->限定符(子列),value->單元格的值
put.addColumn(Bytes.toBytes(array[0]), Bytes.toBytes(array[1]), Bytes.toBytes(array[2]));
}
// 向表格中新增資料
table.put(put);
// 關閉表格
table.close();
//檢查資料
public static void getDataByRK(Connection conn, String rowKey) throws IOException {
// 通過連線物件獲取Table物件
Table table =conn.getTable(TableName.valueOf(TABLE_NAME));
// 建立Get物件
Get get = new Get(Bytes.toBytes(rowKey));
// 通過Table物件獲取查詢結果物件
Result result = table.get(get);
// 通過迴圈獲取每一個列族
for (String family : FAMILYS) {
System.out.println("family=" + family);
Map<byte[], byte[]> map=result.getFamilyMap(Bytes.toBytes(family));
for (byte[] qualifier : map.keySet()) {
String qualifierStr = new String(qualifier);
String value = new String(map.get(qualifier));
System.out.println("qualifier=" + qualifierStr + ",value=" + value);
}
}
// 關閉表格
table.close();
}