10.3 WeiUtil 类

package com.atguigu.weibo;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
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.util.Bytes;

import java.io.IOException;
import java.util.Map;

public class WeiboUtil {
    public static Configuration conf;

    static {
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "hadoop201:2181,hadoop202:2181,hadoop203:2181");
    }

    /**
     * 创建命名空间
     *
     * @param nameSpace 命名空间的名字
     */
    public static void createNameSpace(String nameSpace) throws IOException {
        if (nameSpaceExists(nameSpace)) return;
        Admin admin = getAdmin();
        // 命名空间描述器
        NamespaceDescriptor descriptor = NamespaceDescriptor.create(nameSpace).build();
        // 创建命名空间
        admin.createNamespace(descriptor);
        admin.close();
    }

    /**
     * 创建表
     *
     * @param tableName 带命名空间的表名
     * @param versions  最大版本数(能最多存储多少个版本)
     * @param cfs       多个列
     */
    public static void createTable(String tableName, int versions, String... cfs) throws IOException {
        // 如果表已经存在, 则直接返回
        if (tableExists(tableName)) return;

        Admin admin = getAdmin();
        HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
        for (String cf : cfs) {
            HColumnDescriptor cDescriptor = new HColumnDescriptor(Bytes.toBytes(cf));
            cDescriptor.setMaxVersions(versions);
            descriptor.addFamily(cDescriptor);
        }
        admin.createTable(descriptor);
        admin.close();
    }


    public static Admin getAdmin() throws IOException {
        // 获取连接到 HBase 的连接对象
        Connection conn = ConnectionFactory.createConnection(conf);
        // 获取 Admin对象, 做一些 DDL 的操作
        Admin admin = conn.getAdmin();
        return admin;
    }

    /**
     * 判断指定的表是否存在
     *
     * @param tableName 需要判断的表的表名
     * @return 存在返回true, 否则就返回false
     * @throws IOException
     */
    public static boolean tableExists(String tableName) throws IOException {
        Admin admin = getAdmin();
        boolean exists = admin.tableExists(TableName.valueOf(tableName));
        admin.close();
        return exists;
    }

    /**
     * 判断指定的命名空间是否存在
     *
     * @param nameSpace
     * @return
     */
    public static boolean nameSpaceExists(String nameSpace) {
        Admin admin = null;
        try {
            admin = getAdmin();
            NamespaceDescriptor descriptor = admin.getNamespaceDescriptor(nameSpace);
            Map<String, String> conf = descriptor.getConfiguration();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }


}
Copyright © 尚硅谷大数据 2019 all right reserved,powered by Gitbook
该文件最后修订时间: 2018-11-20 18:14:19

results matching ""

    No results matching ""