Home > Blockchain >  How to create a table in HBASE using Java API
How to create a table in HBASE using Java API

Time:10-19

I want to create a table in HBASE using Java

I found this example to create a table in HBASE enter image description here

How can I create a table in Hbase with the Java api, please help with some code or documentation

I am working with java 1.8.0.112 and the project dependencies are

    <dependencies>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase</artifactId>
            <version>2.4.1</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>2.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>3.0.1</version>
        </dependency>
    </dependencies>

CodePudding user response:

In the more recent versions of the Java API, you can use the class TableDescriptorBuilder and related classes.

Try something like this:

Connection conn; // get the HBase connection as you usually do
Admin admin = conn.getAdmin();

TableDescriptorBuilder tBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(TABLE_NAME));
ColumnFamilyDescriptor CFD = ColumnFamilyDescriptorBuilder.newBuilder(COL_FAMILY_NAME).build();
TableDescriptor tDesc = tBuilder.setColumnFamily(CFD).build();
admin.createTable(tDesc);

CodePudding user response:

    Connection connexion = ConnectionFactory.createConnection(config);
    Admin admin = connexion.getAdmin();

    byte[] family = Bytes.toBytes("cf");
    TableName tname;
    tname = TableName.valueOf("MONITOR:ZEMP");
    TableDescriptorBuilder tableDescBuilder = TableDescriptorBuilder.newBuilder(tname);
    ColumnFamilyDescriptorBuilder columnDescBuilder = ColumnFamilyDescriptorBuilder
            .newBuilder(Bytes.toBytes(ByteBuffer.wrap(family)))
            .setCompressionType(Compression.Algorithm.SNAPPY)
            .setDataBlockEncoding(DataBlockEncoding.FAST_DIFF);
    tableDescBuilder.setColumnFamily(columnDescBuilder.build());

    if (!admin.tableExists(tname)) {
        TableDescriptor desc = tableDescBuilder.build();
        admin.createTable(desc);
        System.out.println(" Table created ");
    }
    else {
        System.out.println(" Table already exists ");
    }
  • Related