FusionInsight平臺——HBase客戶端工具封裝EveHBase

FusionInsight平臺——HBase客戶端工具封裝EveHBase

簡介

  • 針對華爲HBase封裝的客戶端EveHBase
  • 支持普通客戶端、安全模式客戶端,
  • 功能包括:建表、預分區、建索引、異步請求、Put、Get、Scan、結果自動解析爲JavaBean等

項目地址

使用示例

  • 創建普通客戶端
// HBase配置
// 需從HBase的配置文件目錄中分別下載core-site.xml,hdfs-site.xml,hbase-site.xml
Configuration hbaseConf = HBaseConfiguration.create();
hbaseConf.addResource(new Path("./conf/core-site.xml"));
hbaseConf.addResource(new Path("./conf/hdfs-site.xml"));
hbaseConf.addResource(new Path("./conf/hbase-site.xml"));

// 構建客戶端
// 當3個配置文件存在項目根目錄下時,可以不指定config
HBaseClient client = new EveHBase.Builder()
        .config(hbaseConf)
        .build();
  • 創建安全模式客戶端
// HBase配置
// ……

// 安全認證配置
// 需從集羣管理界面下載對應用戶的kerberos文件
SecurityConf securityConf = new SecurityConf(
        "test",
        "./kerberos/user.keytab",
        "./kerberos/krb5.conf");

// 構建安全模式客戶端
HBaseClient client = new EveHBase.Builder()
        .config(hbaseConf)
        .enableSafeSupport(securityConf)
        .build();

  • 自定義線程池
ExecutorServiceAdapter adapter = new ExecutorServiceAdapter() {
            @Override
            public ExecutorService generateExecutorService() {
                return Executors.newFixedThreadPool(4);
            }
        };
HBaseClient client = new EveHBase.Builder()
        .config(hbaseConf)
        .pool(adapter)
        .build();
  • 建表
EveTable eveTable = new EveTable.Builder()
        .table("tb_test")
        .familys("family01")
        .encoding(DataBlockEncoding.FAST_DIFF)
        .compression(Compression.Algorithm.SNAPPY)
        .build();

client.create(eveTable);
  • 關閉並刪除表
client.disableAndDelete("tableName");
  • 預分區
String[] splitKeys = {"aaa", "ddd", "hhh", "vvv"};
client.multiSplit("tb_test", splitKeys);
  • 創建索引
client.createIndex(
    "tb_test", 
    "family01",
    "phone",
    "tb_test_phone_idx");
  • 爲需要自動解析的JavaBean添加註解
public class Person {

    @RowKey
    public String rowkey;

    // 列簇, 對應family 
    @ColumnFamily
    public Info f;

    public class Info {

        // 列簇中的字段, 對應qualifier
        public String address;

        public String phone;

        public String name;

        public String age;
        
    }
    
}
  • 執行Get同步請求
String[] qualifiers = {"address", "phone", "name", "age", "lac"};
EveGet eveGet = new EveGet.Builder()
        .table("tb_test")
        .addRowkey("ccc_21312312")
        .addRowkey("hhh_24242348")
        .select("f", qualifiers)
        .build();

try {
    List<Person> personList = client.get(eveGet, Person.class);
    System.out.println("person = " + personList);
} catch (IOException e) {
    e.printStackTrace();
}
  • 執行Get異步請求
client.getAsync(eveGet, new ResultCallback<Person>() {
    @Override
    public void onSuccessful(List<Person> results) {
        for (Person result : results) {
            System.out.println("result = " + result);
        }
    }

    @Override
    public void onFailed(Exception e) {
        System.out.println("e = " + e);
        e.printStackTrace();
    }
});
  • 如果不需要自動解析JavaBean, 那麼可以這樣做
// 傳入Result.class將返回HBase原生的結果
List<Result> personList = client.get(eveGet, Result.class);
  • 執行Scan同步請求
String[] qualifiers = {"address", "phone", "name", "age", "lac"};

EveScan eveScan = new EveScan.Builder()
        .table("tb_test")
        .startRow("aaa")
        .endRow("hhh")
//      .filter(...)
        .select("f", qualifiers)
        .build();
try {
    List<Person> personList = client.scan(eveScan, Person.class);
    System.out.println("personList = " + personList);
} catch (IOException e) {
    e.printStackTrace();
}
  • 執行Scan異步請求
client.scanAsync(eveScan, new ResultCallback<Person>() {
    @Override
    public void onSuccessful(List<Person> results) {
        for (Person result : results) {
            System.out.println("result = " + result);
        }
    }

    @Override
    public void onFailed(Exception e) {

    }
});
  • 提交Put同步請求
List<Put> putList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
    Put put = new Put(Bytes.toBytes("ggg_132131" + i));
    put.addColumn(Bytes.toBytes("f"), Bytes.toBytes("phone"), Bytes.toBytes("1891234567"+ i));
    putList.add(put);
}
try {
    client.put("tb_test", putList);
} catch (IOException e) {
    e.printStackTrace();
}
  • 使用Put緩衝器入庫
PutBuffer buffer = client.createPutBuffer("test_table", 1000, 5000);
for (int i = 0; i < 100_000; i++) {
    Put put = new Put(Bytes.toBytes("ggg_132131" + i));
    put.addColumn(Bytes.toBytes("f"), Bytes.toBytes("phone"), Bytes.toBytes("1891234567" + i));
    
    buffer.put(put);
}
buffer.flush();
  • 提交Put異步請求
client.putAsync("tb_test", putList, new PutCallback() {
    @Override
    public void onSuccessful() {
        System.out.println("GetTest.onSuccessful 提交Put成功!");
    }

    @Override
    public void onFailed(Exception e, List<Put> puts) {
        System.out.println("GetTest.onFailed 提交Put失敗!");
        System.out.println("失敗的 puts = " + puts);
    }
});
  • 提交Put異步請求, 不要回調
client.putAsync("tb_test", putList, null);

LICENSE

Copyright 2019 ALion

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章