使用HbaseAPI 在指定的命名空间下创建表和列族

提供 maven 依赖:

<repositories>
        <repository>
            <id>clouderaid>
            <url>https://repository.cloudera.com/artifactory/cloudera-repos/url>
        repository>
    repositories>

    <dependencies>

        <dependency>
            <groupId>org.apache.hadoopgroupId>
            <artifactId>hadoop-clientartifactId>
            <version>2.6.0-mr1-cdh5.14.0version>
        dependency>


        <dependency>
            <groupId>org.apache.hbasegroupId>
            <artifactId>hbase-clientartifactId>
            <version>1.2.0-cdh5.14.0version>
        dependency>

        <dependency>
            <groupId>org.apache.hbasegroupId>
            <artifactId>hbase-serverartifactId>
            <version>1.2.0-cdh5.14.0version>
        dependency>


        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.testnggroupId>
            <artifactId>testngartifactId>
            <version>6.14.3version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
            <scope>compilescope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.0version>
                <configuration>
                    <source>1.8source>
                    <target>1.8target>
                    <encoding>UTF-8encoding>
                    
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-shade-pluginartifactId>
                <version>2.2version>
                <executions>
                    <execution>
                        <phase>packagephase>
                        <goals>
                            <goal>shadegoal>
                        goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <artifact>*:*artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SFexclude>
                                        <exclude>META-INF/*.DSAexclude>
                                        <exclude>META-INF/*/RSAexclude>
                                    excludes>
                                filter>
                            filters>
                        configuration>
                    execution>
                executions>
            plugin>
        plugins>
    build>

代码实现 :

    /**
     *
     * 创建一个命名空间 hbase__result
     * 并在该命名空间下 创建一个 test01表
     * 表中有 info 这个列族
     */
    @Test
    public void testNamespace() throws Exception {
    	//连接hbase
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181");
        //建立连接
        Connection connection = ConnectionFactory.createConnection(conf);
        //获取admin
        Admin admin = connection.getAdmin();


        try {
            //获取命名空间看是否存在,不存在会直接抛 NamespaceNotFoundException 异常(可以查看底层代码实现)  使用 try catch 捕捉
            NamespaceDescriptor rel = admin.getNamespaceDescriptor("hbase_result");
        } catch (NamespaceNotFoundException  e) {
            // 不存在创建一个命名空间
            NamespaceDescriptor namespace = NamespaceDescriptor.create("hbase_result").build();
            admin.createNamespace(namespace);
        }
        
		//创建表 和 列族
        TableName tableName = TableName.valueOf("hbase_result:test01");
        HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
        hTableDescriptor.addFamily(new HColumnDescriptor("info"));
        //判断表是否存在
        boolean exists = admin.tableExists(tableName);
        //不存在则创建表
        if (!exists) {
            admin.createTable(hTableDescriptor);
        }

		//删除命名空间
//        admin.deleteNamespace("hbase_result");

        //释放资源
        admin.close();
    }

你可能感兴趣的:(Hbase)