HBase Examples: A Comprehensive Guide
 throws Exception {
Configuration config = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(config);
Admin admin = connection.getAdmin();
TableName tableName = TableName.valueOf(mytable);
TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName)
.setColumnFamily(ColumnFamilyDescriptorBuilder.of(cf))
.build();
admin.createTable(tableDescriptor);
admin.close();
connection.close();
}
}
Example 2: Inserting Data
Once we have a table, we can insert data into it. Here's an example of how to insert a row into the "mytable" table:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
public class InsertDataExample {
public static void main(String[] args) throws Exception {
Configuration config = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(config);
TableName tableName = TableName.valueOf(mytable);
Table table = connection.getTable(tableName);
Put put = new Put(Bytes.toBytes(row1));
put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(column1), Bytes.toBytes(value1));
table.put(put);
table.close();
connection.close();
}
}
Example 3: Retrieving Data
To retrieve data from a table, we use the get
command. Here's an example of how to retrieve a row from the "mytable" table:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
public class RetrieveDataExample {
public static void main(String[] args) throws Exception {
Configuration config = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(config);
TableName tableName = TableName.valueOf(mytable);
Table table = connection.getTable(tableName);
Get get = new Get(Bytes.toBytes(row1));
Result result = table.get(get);
byte[] value = result.getValue(Bytes.toBytes(cf), Bytes.toBytes(column1));
System.out.println(Bytes.toString(value));
table.close();
connection.close();
}
}
Conclusion
In this article, we explored some common examples of using HBase through the "hbase-examples.jar" library. We learned how to create a table, insert data into it, and retrieve data from it. These examples provide a solid foundation for understanding and working with HBase. With its distributed and scalable nature, HBase is a powerful tool for handling large amounts of data efficiently.
To learn more about HBase and its various features, refer to the official Apache HBase documentation ( Happy HBase coding!