Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dvimont/66f0431ed6d5e6b86fe03653a298b958 to your computer and use it in GitHub Desktop.
Save dvimont/66f0431ed6d5e6b86fe03653a298b958 to your computer and use it in GitHub Desktop.
package org.commonvox.hbase_column_manager_gist;
import java.io.IOException;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.NamespaceNotFoundException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.commonvox.hbase_column_manager.ColumnDefinition;
import org.commonvox.hbase_column_manager.ColumnDefinitionNotFoundException;
import org.commonvox.hbase_column_manager.ColumnValueInvalidException;
import org.commonvox.hbase_column_manager.MConnectionFactory;
import org.commonvox.hbase_column_manager.RepositoryAdmin;
/**
* THIS GIST DEMONSTRATES BASIC USAGE OF THE ColumnManagerForHBase API FOR
* (1) CREATION OF A ColumnDefinition PERTAINING TO A SPECIFIED COLUMN-FAMILY
* (2) ACTIVATION OF COLUMN-ENFORCEMENT FOR THE COLUMN-FAMILY, and
* (3) EXAMPLES OF EXCEPTIONS THROWN WHEN PUTS ARE SUBMITTED WITH INVALID COLUMN QUALIFIER OR VALUE
*
* Successful running of this application requires access to an active instance
* of HBase. For install instructions for a standalone instance of HBase, please
* refer to:
* https://hbase.apache.org/book.html#quickstart
*
* For all available versions of hbase-column-manager, please consult Maven Central Repository:
* http://bit.ly/ColumnManagerMaven
*
* FULL INSTRUCTIONS for installation and activation of ColumnManager for HBase available here:
* http://bit.ly/ColumnManagerJavadocs
*
*/
public final class ColumnManagerForHBaseGistWithColumnDefs {
protected static final String MY_NAMESPACE_NAME = "myTestNamespace";
static final TableName MY_TABLE_NAME = TableName.valueOf(MY_NAMESPACE_NAME, "myTestTable");
static final byte[] MY_COLUMN_FAMILY_NAME = Bytes.toBytes("cf");
static final byte[] MY_FIRST_COLUMN_QUALIFIER = Bytes.toBytes("myFirstColumn");
static final byte[] MY_SECOND_COLUMN_QUALIFIER = Bytes.toBytes("mySecondColumn");
static final byte[] MY_ROW_ID01 = Bytes.toBytes("rowId01");
static final byte[] MY_ROW_ID02 = Bytes.toBytes("rowId02");
public static void main(final String[] args) throws Exception {
try (Connection connection = MConnectionFactory.createConnection();
Admin admin = connection.getAdmin()) {
// use standard hbase-client API to create namepace/table & add rows
if (!namespaceExists(admin, MY_NAMESPACE_NAME)) {
admin.createNamespace(NamespaceDescriptor
.create(MY_NAMESPACE_NAME).build());
}
if (!admin.tableExists(MY_TABLE_NAME)) {
admin.createTable(new HTableDescriptor(MY_TABLE_NAME)
.addFamily(new HColumnDescriptor(MY_COLUMN_FAMILY_NAME)));
}
// Add a column definition for the column-family and activate column enforcement for it
RepositoryAdmin repositoryAdmin = new RepositoryAdmin(connection);
repositoryAdmin.addColumnDefinition(MY_TABLE_NAME, MY_COLUMN_FAMILY_NAME,
new ColumnDefinition(MY_FIRST_COLUMN_QUALIFIER)
.setColumnValidationRegex("https?://.*"));
repositoryAdmin.setColumnDefinitionsEnforced(true, MY_TABLE_NAME, MY_COLUMN_FAMILY_NAME);
// Use standard HBase API to submit PUT transactions to the table
try (Table table = connection.getTable(MY_TABLE_NAME)) {
// PUT a row with valid column qualifier and valid value
table.put(new Put(MY_ROW_ID01)
.addColumn(MY_COLUMN_FAMILY_NAME, MY_FIRST_COLUMN_QUALIFIER,
Bytes.toBytes("http://www.google.com")));
// attempt to PUT a row with an invalid column QUALIFIER; catch the resulting exception
try {
table.put(new Put(MY_ROW_ID02)
.addColumn(MY_COLUMN_FAMILY_NAME, MY_SECOND_COLUMN_QUALIFIER,
Bytes.toBytes("Hello ColumnManager!")));
} catch (ColumnDefinitionNotFoundException e) {
System.out.println(
"Invalid column qualifier submitted in PUT to table; exception successfully caught: "
+ e.getMessage());
}
// attempt to PUT a row with an invalid column VALUE; catch the resulting exception
try {
table.put(new Put(MY_ROW_ID02)
.addColumn(MY_COLUMN_FAMILY_NAME, MY_FIRST_COLUMN_QUALIFIER,
Bytes.toBytes("ftp://www.google.com")));
} catch (ColumnValueInvalidException e) {
System.out.println(
"Invalid column value submitted in PUT to table; exception successfully caught: "
+ e.getMessage());
}
}
// use standard hbase-client API to delete table & namespace
admin.disableTable(MY_TABLE_NAME); // Disable a table before deleting it!
admin.deleteTable(MY_TABLE_NAME);
admin.deleteNamespace(MY_NAMESPACE_NAME);
}
}
/**
* Checks to see whether a namespace exists.
*
* @param admin Standard Admin object
* @param namespaceName Name of namespace
* @return true If namespace exists
* @throws IOException If IO problem encountered
*/
static boolean namespaceExists(final Admin admin, final String namespaceName)
throws IOException {
try {
admin.getNamespaceDescriptor(namespaceName);
} catch (NamespaceNotFoundException e) {
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment