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

提供 maven 依赖:

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

    <dependencies>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.6.0-mr1-cdh5.14.0</version>
        </dependency>


        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.2.0-cdh5.14.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>1.2.0-cdh5.14.0</version>
        </dependency>


        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                    <!--    <verbal>true</verbal>-->
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*/RSA</exclude>
                                    </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();
    }
发布了88 篇原创文章 · 获赞 114 · 访问量 3011

猜你喜欢

转载自blog.csdn.net/hongchenshijie/article/details/103816453