Use the Java API
You can use the Apache HBase Java API to communicate with Apache HBase. The Java API is one of the most common ways to communicate with HBase.
The following sample uses Apache HBase APIs to create a table and put a row into that table. The
table name, column family name, qualifier (or column) name, and a unique ID for the
row are defined. Together, these define a specific cell. Next, the table is created
and the text “Hello, World!” is inserted into this cell.
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
public class CreateAndPut {
private static final TableName TABLE_NAME = TableName.valueOf("test_table_example");
private static final byte[] CF_NAME = Bytes.toBytes("test_cf");
private static final byte[] QUALIFIER = Bytes.toBytes("test_column");
private static final byte[] ROW_ID = Bytes.toBytes("row01");
public static void createTable(final Admin admin) throws IOException {
if(!admin.tableExists(TABLE_NAME)) {
TableDescriptor desc = TableDescriptorBuilder.newBuilder(TABLE_NAME)
.setColumnFamily(ColumnFamilyDescriptorBuilder.of(CF_NAME))
.build();
admin.createTable(desc);
}
}
public static void putRow(final Table table) throws IOException {
table.put(new Put(ROW_ID).addColumn(CF_NAME, QUALIFIER, Bytes.toBytes("Hello, World!")));
}
public static void main(String[] args) throws IOException {
Configuration config = HBaseConfiguration.create();
try (Connection connection = ConnectionFactory.createConnection(config); Admin admin = connection.getAdmin()) {
createTable(admin);
try(Table table = connection.getTable(TABLE_NAME)) {
putRow(table);
}
}
}
}