All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.flipkart.hbaseobjectmapper.HBAdmin Maven / Gradle / Ivy

Go to download

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

There is a newer version: 1.19
Show newest version
package com.flipkart.hbaseobjectmapper;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
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})
 */
public class HBAdmin {
    private final Connection connection;

    /**
     * Constructs {@link HBAdmin} object
     *
     * @param connection HBase connection
     */
    public HBAdmin(Connection connection) {
        this.connection = connection;
    }

    /**
     * Create table represented by the class
     *
     * @param hbRecordClass Class that represents the HBase table
     * @param            Data type of row key
     * @param            Entity type
     * @throws IOException When HBase call fails
     * @see Admin#createTable(TableDescriptor)
     */
    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());
        }
    }

    /**
     * Deletes HBase table
     *
     * @param hbRecordClass Class that represents the HBase table
     * @param            Data type of row key
     * @param            Entity type
     * @throws IOException When HBase call fails
     * @see Admin#deleteTable(TableName)
     */
    public , T extends HBRecord> void deleteTable(Class hbRecordClass) throws IOException {
        try (Admin admin = connection.getAdmin()) {
            WrappedHBTable wrappedHBTable = new WrappedHBTable<>(hbRecordClass);
            admin.deleteTable(wrappedHBTable.getName());
        }
    }

    /**
     * Enable a
     *
     * @param hbRecordClass Class that represents the HBase table
     * @param            Data type of row key
     * @param            Entity type
     * @throws IOException When HBase call fails
     * @see Admin#enableTable(TableName)
     */
    public , T extends HBRecord> void enableTable(Class hbRecordClass) throws IOException {
        try (Admin admin = connection.getAdmin()) {
            WrappedHBTable wrappedHBTable = new WrappedHBTable<>(hbRecordClass);
            admin.enableTable(wrappedHBTable.getName());
        }
    }

    /**
     * Disable a table
     *
     * @param hbRecordClass Class that represents the HBase table
     * @param            Data type of row key
     * @param            Entity type
     * @throws IOException When HBase call fails
     * @see Admin#disableTable(TableName)
     */
    public , T extends HBRecord> void disableTable(Class hbRecordClass) throws IOException {
        try (Admin admin = connection.getAdmin()) {
            WrappedHBTable wrappedHBTable = new WrappedHBTable<>(hbRecordClass);
            admin.disableTable(wrappedHBTable.getName());
        }
    }

    /**
     * Checks whether table exists
     *
     * @param hbRecordClass Class that represents the HBase table
     * @param            Data type of row key
     * @param            Entity type
     * @return true if table exists
     * @throws IOException When HBase call fails
     * @see Admin#tableExists(TableName)
     */
    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());
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy