關於hbase的HBaseAdmin對象的創建,新舊版本對比

一、hbase的java API

在重寫以前的代碼的時候,換了新版本的hbase,導致出現了很多問題,在此記錄一下,新舊版本創建HBaseAdmin的方式

二、舊的版本

創建的HBaseAdmin裏面傳入的是configuration。

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
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.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;
public class HbaseDemoTest {
 // 聲明靜態配置
 static Configuration conf = null;
 
 private static final String ZK_CONNECT_STR = "hadoop01:2181,hadoop02:2181";
 
 static {
 conf = HBaseConfiguration.create();
 conf.set("hbase.zookeeper.quorum", ZK_CONNECT_STR);
 }
 /*
 * 創建表
 * @tableName 表名
 * @family 列簇列表
 */
 public static void creatTable(String tableName, String[] family) throws Exception {
 HBaseAdmin admin = new HBaseAdmin(conf);
 HTableDescriptor desc = new HTableDescriptor(tableName);
 for (int i = 0; i < family.length; i++) {
 desc.addFamily(new HColumnDescriptor(family[i]));
 }
 if (admin.tableExists(tableName)) {
 System.out.println("table Exists!");
 System.exit(0);
 } else {
 admin.createTable(desc);
 System.out.println("create table Success!");
 }
 }
}

三、新版本

目前使用的版本是2.3.2的hbase。在創建HBaseAdmin的時候需要借用鏈接

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.HBaseAdmin;

import java.io.IOException;

/**
 
 * @version 1.0
 * @date 2020/6/12 11:50
  
 */
public class HbaseUtils {
    HBaseAdmin hBaseAdmin = null;
    Connection connection  = null;
    Configuration configuration = null;

    private HbaseUtils()   {
        configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "hadoop001:2181");
        try {
            connection = ConnectionFactory.createConnection(configuration);
            hBaseAdmin = (HBaseAdmin) connection.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static HbaseUtils instance = null;
    public static synchronized HbaseUtils getInstance(){
        if (instance == null){
            instance = new HbaseUtils();
        }
        return instance;
    }

    public void createTable(String tableName,String[]family) throws IOException {
        HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
       for(String f: family){
            tableDescriptor.addFamily(new HColumnDescriptor(f));
        }
        // 創建表
        hBaseAdmin.createTable(tableDescriptor );
        System.out.println("創建" + tableName + "表成功");

    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章