谷粒微博学习笔记一:Utils&constants

 Utils:主要是一些工具类,用以完成一些基本操作如:创建命名空间、创建表、判断表是否存在等功能

package tyh.utils;

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 tyh.constants.constants;

import java.io.IOException;

/**
 * 1、创建命名空间
 * 2、判断表是否存在
 * 3、创建表
 */
public class HBaseUtil {

    //一、创建命名空间
    public static void CreateNameSpace(String nameSpace) {

        Admin admin = null;

        try {
            //1、获取connection对象,需要用到configuration对象,但configuration是全局的,所有操作统一要用的,
            // 没必要每次操作都弄一个新的,统一调用constants中的configuration对象
            // Configuration configuration = HBaseConfiguration.create();
            Connection connection = ConnectionFactory.createConnection(constants.CONFIGURATION);

            //2、获取admin对象
            admin = connection.getAdmin();

            //3、创建命名空间描述器
            NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(nameSpace).build();

            //4、创建命名空间
            admin.createNamespace(namespaceDescriptor);
        } catch (MasterNotRunningException e) {
            //master没有启动
            e.printStackTrace();
        } catch (ZooKeeperConnectionException e) {
            //zookeeper连接不上
            e.printStackTrace();
        } catch (NamespaceExistException e) {
            //命名空间已存在
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //5、关闭资源
            if (admin != null) {
                try {
                    admin.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //二、判断表是否存在
    public static boolean TableExists(String tableName) {

        Admin admin = null;
        boolean exists = false;

        try {
            //1、获取connection对象
            Connection connection = ConnectionFactory.createConnection(constants.CONFIGURATION);

            //2、获取admin对象
            admin = connection.getAdmin();

            //3、判断表是否存在
            exists = admin.tableExists(TableName.valueOf(tableName));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (admin != null) {
                try {
                    //4、关闭资源
                    admin.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //5、返回结果
        return exists;
    }

    //三、创建表
    //这里抛出异常是为了根据实际情况对异常进行处理
    public static void CreateTable(String tableName, int Versions, String... cfs) throws IOException {
        //1、判断是否有列族信息
        if (cfs.length == 0){
            System.out.println("请设置列族信息!!");
            return;
        }

        //2、判断表是否存在
        if (TableExists(tableName)){
            System.out.println("该表已存在!!");
            return;
        }

        //3、获取connection对象
        Connection connection = ConnectionFactory.createConnection(constants.CONFIGURATION);

        //4、获取admin对象
        Admin admin = connection.getAdmin();

        //5、创建表描述器
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));

        for (String cf : cfs) {
            //6、创建列族描述器
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(Bytes.toBytes(cf));

            //7、设置数据最大版本数
            hColumnDescriptor.setMaxVersions(Versions);

            //8、添加列族信息
            hTableDescriptor.addFamily(hColumnDescriptor);
        }

        //9、创建表
        admin.createTable(hTableDescriptor);

        //10、关闭资源
        admin.close();
    }
}

 constants用来存放一些不能修改的参数

package tyh.constants;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;


public class constants {
    //final关键字用来指定这个变量不能被修改
    //1、configuration,HBase的配置信息
    public static final Configuration CONFIGURATION = HBaseConfiguration.create();

    //2、命名空间
    public static final String NAMESPACE = "WeiBo";

    //3、微博内容表
    public static final String CONTENT_TABLE = "WeiBo:content";
    //微博内容表的列族,用来存放用户发布的微博
    public static final String CONTENT_TABLE_CF = "info";
    //设置最大版本数
    public static final int CONTENT_TABLE_VERSIONS = 1;

    //4、用户关系表
    public static final String RELATION_TABLE = "WeiBo:relation";
    //用户关系表的列族
    //用来存放你关注的人的用户id
    public static final String RELATION_TABLE_ATTEND = "attends";
    //用来存放关注你的用的的id
    public static final String RELATION_TABLE_FANS = "fans";
    //设置最大版本数:
    public static final int RELATION_TABLE_VERSIONS = 1;

    //收件箱表
    public static final String INBOX_TABLE = "WeiBo:inbox";
    //收件箱表列族:用来存放你关注的人近期发布的一些微博
    public static final String INBOX_TABLE_CONCERN = "concern";
    //设置最大版本数,即你可以接收到的你关注的人最近发布的微博的数量,每人2条
    public static final int INBOX_TABLE_VERSIONS = 2;
}

要说的都在代码里了

猜你喜欢

转载自blog.csdn.net/tyh1579152915/article/details/109289486