com.flipkart.hbaseobjectmapper.SyncHBAdmin Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hbase-object-mapper Show documentation
Show all versions of hbase-object-mapper Show documentation
HBase ORM is a light-weight, thread-safe and performant library that enables:
[1] object-oriented access of HBase rows (Data Access Object) with minimal code and good testability.
[2] reading from and/or writing to HBase tables in Hadoop MapReduce jobs.
This can also be used as an ORM for Bigtable.
The newest version!
package com.flipkart.hbaseobjectmapper;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.io.Serializable;
import java.util.Map;
/**
* A minimal administrative API (wrapper over HBase's {@link Admin})
*/
class SyncHBAdmin implements HBAdmin {
private final Connection connection;
/**
* Constructs {@link SyncHBAdmin} object
*
* @param connection HBase connection
*/
public SyncHBAdmin(Connection connection) {
this.connection = connection;
}
@Override
public void createNamespace(String namespace) throws IOException {
final NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(namespace).build();
try (Admin admin = connection.getAdmin()) {
admin.createNamespace(namespaceDescriptor);
}
}
@Override
public , T extends HBRecord> void createTable(Class hbRecordClass) throws IOException {
try (Admin admin = connection.getAdmin()) {
WrappedHBTable wrappedHBTable = new WrappedHBTable<>(hbRecordClass);
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(wrappedHBTable.getName());
for (Map.Entry e : wrappedHBTable.getFamiliesAndVersions().entrySet()) {
tableDescriptorBuilder.setColumnFamily(
ColumnFamilyDescriptorBuilder.newBuilder(
Bytes.toBytes(e.getKey())
).setMaxVersions(e.getValue()).build());
}
admin.createTable(tableDescriptorBuilder.build());
}
}
@Override
public , T extends HBRecord> void deleteTable(Class hbRecordClass) throws IOException {
try (Admin admin = connection.getAdmin()) {
WrappedHBTable wrappedHBTable = new WrappedHBTable<>(hbRecordClass);
admin.deleteTable(wrappedHBTable.getName());
}
}
@Override
public , T extends HBRecord> void enableTable(Class hbRecordClass) throws IOException {
try (Admin admin = connection.getAdmin()) {
WrappedHBTable wrappedHBTable = new WrappedHBTable<>(hbRecordClass);
admin.enableTable(wrappedHBTable.getName());
}
}
@Override
public , T extends HBRecord> void disableTable(Class hbRecordClass) throws IOException {
try (Admin admin = connection.getAdmin()) {
WrappedHBTable wrappedHBTable = new WrappedHBTable<>(hbRecordClass);
admin.disableTable(wrappedHBTable.getName());
}
}
@Override
public , T extends HBRecord> boolean tableExists(Class hbRecordClass) throws IOException {
try (Admin admin = connection.getAdmin()) {
WrappedHBTable wrappedHBTable = new WrappedHBTable<>(hbRecordClass);
return admin.tableExists(wrappedHBTable.getName());
}
}
}