實驗八 客戶端 API 高階應用
阿新 • • 發佈:2021-01-18
實驗目的
1、理解 HBase 的常用概念
2、熟悉 Hbase 常用的 API
3、掌握 Hbase 常用的 API 的高階應用
實驗環境
1、64 位電腦,8G 以上記憶體
2、win10 系統
3、HBase 執行環境
4、Myeclipse
課時:
2 課時
實驗步驟:
1、在部門組織結構圖如下:
2、表結構如下:
3、表儲存資料如下:
4、在實驗七 客戶端 API 基本應用上進行。
5、在 HBTest 專案中新建類,類名為 HBTest2。
6、將 HBase 相關的包引入該類。
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
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.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
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.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
7、將以下程式碼複製到該類中,檢查有沒有錯誤。
private Configuration config = null;
private Connection connection = null;
private Table table = null;
public void init() throws Exception {
config = HBaseConfiguration.create();// 初始化配置
config.set("hbase.zookeeper.quorum", "192.168.189.129");// 設定 zookeeper 地址
//config.set("hbase.zookeeper.property.clientPort", "2181");//設定 zookeeper 埠
connection = ConnectionFactory.createConnection(config);
table = connection.getTable(TableName.valueOf("dept"));
}
/**
* 建立資料庫表 dept,並增加列族 info 和 subdept
*
* @throws Exception
*/
public void createTable() throws Exception {
// 建立表管理類物件
Admin admin = connection.getAdmin();
// 建立表描述類物件
TableName tableName = TableName.valueOf("dept"); // 表名稱
HTableDescriptor desc = new HTableDescriptor(tableName);
// 建立列族的描述類
HColumnDescriptor family = new HColumnDescriptor("info"); // 列族
// 將列族新增到表中
desc.addFamily(family);
HColumnDescriptor family2 = new HColumnDescriptor("subdept"); // 列族
// 將列族新增到表中
desc.addFamily(family2);
// 建立表
admin.createTable(desc);
System.out.println("建立表成功!");
}
/**
* 向 hbase 中插入前三行網路部、開發部、測試部的相關資料,
4
* 即加入表中的前三條資料
*
*/
public void insertData() throws Exception {
ArrayList<Put> arrayList = new ArrayList<Put>();
Put put = new Put(Bytes.toBytes("0_1"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("網路部"));
put.addColumn(Bytes.toBytes("subdept"), Bytes.toBytes("subdept1"),
Bytes.toBytes("1_1"));
put.addColumn(Bytes.toBytes("subdept"), Bytes.toBytes("subdept2"),
Bytes.toBytes("1_2"));
arrayList.add(put);
Put put1 = new Put(Bytes.toBytes("1_1"));
put1.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("開發部"));
put1.addColumn(Bytes.toBytes("info"), Bytes.toBytes("f_pid"),
Bytes.toBytes("0_1"));
Put put2 = new Put(Bytes.toBytes("1_2"));
put2.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("測試部"));
put2.addColumn(Bytes.toBytes("info"), Bytes.toBytes("f_pid"),
Bytes.toBytes("0_1"));
for (int i = 1; i <= 5; i++) {
put1.addColumn(Bytes.toBytes("subdept"), Bytes.toBytes("subdept"+i),
Bytes.toBytes("2_"+i));
put2.addColumn(Bytes.toBytes("subdept"), Bytes.toBytes("subdept"+i),
Bytes.toBytes("3_"+i));
}
arrayList.add(put1);
arrayList.add(put2);
//插入資料
table.put(arrayList);
//提交
//table.flushCommits();
System.out.println("資料插入成功!");
}
/**
* 向 hbase 中插入開發部、測試部下的所有子部門資料
* @throws Exception
*/
public void insertOtherData() throws Exception {
ArrayList<Put> arrayList = new ArrayList<Put>();
for (int i = 1; i <= 5; i++) {
Put put_development = new Put(Bytes.toBytes("2_"+i));
put_development.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"),
Bytes.toBytes("開發"+i+"組"));
put_development.addColumn(Bytes.toBytes("info"), Bytes.toBytes("f_pid"),
Bytes.toBytes("1_1"));
arrayList.add(put_development);
Put put_test = new Put(Bytes.toBytes("3_"+i));
put_test.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"),
Bytes.toBytes("測試"+i+"組"));
put_test.addColumn(Bytes.toBytes("info"), Bytes.toBytes("f_pid"),
Bytes.toBytes("1_2"));
arrayList.add(put_test);
}
//插入資料
table.put(arrayList);
System.out.println("插入其他資料成功!");
}
public void scanDataStep1() throws Exception {
// 建立全表掃描的 scan
Scan scan = new Scan();
System.out.println("查詢到的所有一級部門如下:");
// 列印結果集
ResultScanner results = table.getScanner(scan);
for(Result result : results){
System.out.println(Bytes.toString(result.getRow())+"===>");
for(Cell cell : result.listCells()) {
System.out.println("fam:"+new
String(cell.getFamilyArray(),cell.getFamilyOffset(), cell.getRowLength()));
System.out.println("qua:"+Bytes.toString(cell.getQualifierArray(),
cell.getQualifierOffset(), cell.getQualifierLength()));
System.out.println("val:"+Bytes.toString(cell.getValueArray(),
cell.getValueOffset(), cell.getValueLength()));
System.out.println("----------------------");
}
}
}
public void close(){
try {
table.close();
connection.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
8、在 HBTest 類的主函式中對該類進行例項化,如下程式所示,對類物件的函式有選擇的執行,執行後觀察每一個函式的執行結果,同時開啟 Hbase shell,用 scan 命令檢視資料表的內容,進行對比,確定函式的執行結果是否正確。
// TODO Auto-generated method stub
HBTest2 hbaseTest= new HBTest2();
try {
hbaseTest.init();
hbaseTest.createTable();//只需成功執行一次,
hbaseTest.insertData();//只需成功執行一次
hbaseTest.scanDataStep1();
hbaseTest.close();
} catch (Exception e) {
e.printStackTrace();
}
原始碼:
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
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.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
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.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
public class HBTest2 {
private Configuration config = null;
private Connection connection = null;
private Table table = null;
public void init() throws Exception {
config = HBaseConfiguration.create();// 初始化配置
config.set("hbase.zookeeper.quorum", "192.168.189.129");// 設定 zookeeper 地址
//config.set("hbase.zookeeper.property.clientPort", "2181");//設定 zookeeper 埠
connection = ConnectionFactory.createConnection(config);
table = connection.getTable(TableName.valueOf("dept"));
}
/**
* 建立資料庫表 dept,並增加列族 info 和 subdept
*
* @throws Exception
*/
public void createTable() throws Exception {
// 建立表管理類物件
Admin admin = connection.getAdmin();
// 建立表描述類物件
TableName tableName = TableName.valueOf("dept"); // 表名稱
HTableDescriptor desc = new HTableDescriptor(tableName);
// 建立列族的描述類
HColumnDescriptor family = new HColumnDescriptor("info"); // 列族
// 將列族新增到表中
desc.addFamily(family);
HColumnDescriptor family2 = new HColumnDescriptor("subdept"); // 列族
// 將列族新增到表中
desc.addFamily(family2);
// 建立表
admin.createTable(desc);
System.out.println("建立表成功!");
}
/**
* 向 hbase 中插入前三行網路部、開發部、測試部的相關資料,
4
* 即加入表中的前三條資料
*
*/
public void insertData() throws Exception {
ArrayList<Put> arrayList = new ArrayList<Put>();
Put put = new Put(Bytes.toBytes("0_1"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("網路部"));
put.addColumn(Bytes.toBytes("subdept"), Bytes.toBytes("subdept1"),
Bytes.toBytes("1_1"));
put.addColumn(Bytes.toBytes("subdept"), Bytes.toBytes("subdept2"),
Bytes.toBytes("1_2"));
arrayList.add(put);
Put put1 = new Put(Bytes.toBytes("1_1"));
put1.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("開發部"));
put1.addColumn(Bytes.toBytes("info"), Bytes.toBytes("f_pid"),
Bytes.toBytes("0_1"));
Put put2 = new Put(Bytes.toBytes("1_2"));
put2.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("測試部"));
put2.addColumn(Bytes.toBytes("info"), Bytes.toBytes("f_pid"),
Bytes.toBytes("0_1"));
for (int i = 1; i <= 5; i++) {
put1.addColumn(Bytes.toBytes("subdept"), Bytes.toBytes("subdept"+i),
Bytes.toBytes("2_"+i));
put2.addColumn(Bytes.toBytes("subdept"), Bytes.toBytes("subdept"+i),
Bytes.toBytes("3_"+i));
}
arrayList.add(put1);
arrayList.add(put2);
//插入資料
table.put(arrayList);
//提交
//table.flushCommits();
System.out.println("資料插入成功!");
}
/**
* 向 hbase 中插入開發部、測試部下的所有子部門資料
* @throws Exception
*/
public void insertOtherData() throws Exception {
ArrayList<Put> arrayList = new ArrayList<Put>();
for (int i = 1; i <= 5; i++) {
Put put_development = new Put(Bytes.toBytes("2_"+i));
put_development.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"),
Bytes.toBytes("開發"+i+"組"));
put_development.addColumn(Bytes.toBytes("info"), Bytes.toBytes("f_pid"),
Bytes.toBytes("1_1"));
arrayList.add(put_development);
Put put_test = new Put(Bytes.toBytes("3_"+i));
put_test.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"),
Bytes.toBytes("測試"+i+"組"));
put_test.addColumn(Bytes.toBytes("info"), Bytes.toBytes("f_pid"),
Bytes.toBytes("1_2"));
arrayList.add(put_test);
}
//插入資料
table.put(arrayList);
System.out.println("插入其他資料成功!");
}
public void scanDataStep1() throws Exception {
// 建立全表掃描的 scan
Scan scan = new Scan();
System.out.println("查詢到的所有一級部門如下:");
// 列印結果集
ResultScanner results = table.getScanner(scan);
for(Result result : results){
System.out.println(Bytes.toString(result.getRow())+"===>");
for(Cell cell : result.listCells()) {
System.out.println("fam:"+new
String(cell.getFamilyArray(),cell.getFamilyOffset(), cell.getRowLength()));
System.out.println("qua:"+Bytes.toString(cell.getQualifierArray(),
cell.getQualifierOffset(), cell.getQualifierLength()));
System.out.println("val:"+Bytes.toString(cell.getValueArray(),
cell.getValueOffset(), cell.getValueLength()));
System.out.println("----------------------");
}
}
}
public void close(){
try {
table.close();
connection.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
HBTest2 hbaseTest= new HBTest2();
try {
hbaseTest.init();
hbaseTest.createTable();//只需成功執行一次,
hbaseTest.insertData();//只需成功執行一次
hbaseTest.scanDataStep1();
hbaseTest.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
也可以到我的資源中下載完整專案客戶端api的應用 HBtest.zip,包含實驗七到實驗九