Revert "Fixed example code"

Reverted because missing associated JIRA reference.

This reverts commit 0dfb364723.
This commit is contained in:
stack 2015-05-11 10:02:06 -07:00
parent ec51d7b2e6
commit e1628106ae
1 changed files with 56 additions and 53 deletions

View File

@ -36,66 +36,74 @@ See <<external_apis>> for more information.
== Examples == Examples
.Create, modify and delete a Table Using Java .Create a Table Using Java
==== ====
[source,java] [source,java]
---- ----
package com.example.hbase.admin; package com.example.hbase.admin;
package util;
import java.io.IOException; import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.io.compress.Compression.Algorithm; import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
import org.apache.hadoop.conf.Configuration;
public class Example { import static com.example.hbase.Constants.*;
private static final String TABLE_NAME = "MY_TABLE_NAME_TOO"; public class CreateSchema {
private static final String CF_DEFAULT = "DEFAULT_COLUMN_FAMILY";
public static void createOrOverwrite(Admin admin, HTableDescriptor table) throws IOException { public static void createOrOverwrite(Admin admin, HTableDescriptor table) throws IOException {
if (admin.tableExists(table.getTableName())) { if (admin.tableExists(table.getName())) {
admin.disableTable(table.getTableName()); admin.disableTable(table.getName());
admin.deleteTable(table.getTableName()); admin.deleteTable(table.getName());
} }
admin.createTable(table); admin.createTable(table);
} }
public static void createSchemaTables(Configuration config) throws IOException { public static void createSchemaTables (Configuration config) {
try (Connection connection = ConnectionFactory.createConnection(config); try {
Admin admin = connection.getAdmin()) { final Admin admin = new Admin(config);
HTableDescriptor table = new HTableDescriptor(TableName.valueOf(TABLE_NAME)); HTableDescriptor table = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
table.addFamily(new HColumnDescriptor(CF_DEFAULT).setCompressionType(Algorithm.SNAPPY)); table.addFamily(new HColumnDescriptor(CF_DEFAULT).setCompressionType(Algorithm.SNAPPY));
System.out.print("Creating table. "); System.out.print("Creating table. ");
createOrOverwrite(admin, table); createOrOverwrite(admin, table);
System.out.println(" Done."); System.out.println(" Done.");
}
}
public static void modifySchema (Configuration config) throws IOException { admin.close();
try (Connection connection = ConnectionFactory.createConnection(config); } catch (Exception e) {
Admin admin = connection.getAdmin()) { e.printStackTrace();
TableName tableName = TableName.valueOf(TABLE_NAME);
if (admin.tableExists(tableName)) {
System.out.println("Table does not exist.");
System.exit(-1); System.exit(-1);
} }
}
HTableDescriptor table = new HTableDescriptor(tableName); }
----
====
.Add, Modify, and Delete a Table
====
[source,java]
----
public static void upgradeFrom0 (Configuration config) {
try {
final Admin admin = new Admin(config);
TableName tableName = TableName.valueOf(TABLE_ASSETMETA);
HTableDescriptor table_assetmeta = new HTableDescriptor(tableName);
table_assetmeta.addFamily(new HColumnDescriptor(CF_DEFAULT).setCompressionType(Algorithm.SNAPPY));
// Create a new table.
System.out.print("Creating table_assetmeta. ");
admin.createTable(table_assetmeta);
System.out.println(" Done.");
// Update existing table // Update existing table
HColumnDescriptor newColumn = new HColumnDescriptor("NEWCF"); HColumnDescriptor newColumn = new HColumnDescriptor("NEWCF");
@ -107,28 +115,23 @@ public class Example {
HColumnDescriptor existingColumn = new HColumnDescriptor(CF_DEFAULT); HColumnDescriptor existingColumn = new HColumnDescriptor(CF_DEFAULT);
existingColumn.setCompactionCompressionType(Algorithm.GZ); existingColumn.setCompactionCompressionType(Algorithm.GZ);
existingColumn.setMaxVersions(HConstants.ALL_VERSIONS); existingColumn.setMaxVersions(HConstants.ALL_VERSIONS);
table.modifyFamily(existingColumn); table_assetmeta.modifyFamily(existingColumn)
admin.modifyTable(tableName, table); admin.modifyTable(tableName, table_assetmeta);
// Disable an existing table // Disable an existing table
admin.disableTable(tableName); admin.disableTable(tableName);
// Delete an existing column family // Delete an existing column family
admin.deleteColumn(tableName, CF_DEFAULT.getBytes("UTF-8")); admin.deleteColumn(tableName, CF_DEFAULT);
// Delete a table (Need to be disabled first) // Delete a table (Need to be disabled first)
admin.deleteTable(tableName); admin.deleteTable(tableName);
}
}
public static void main(String... args) throws IOException {
Configuration config = HBaseConfiguration.create();
//Add any necessary configuration files (hbase-site.xml, core-site.xml) admin.close();
config.addResource(new Path(System.getenv("HBASE_CONF_DIR"), "hbase-site.xml")); } catch (Exception e) {
config.addResource(new Path(System.getenv("HADOOP_CONF_DIR"), "core-site.xml")); e.printStackTrace();
createSchemaTables(config); System.exit(-1);
modifySchema(config);
} }
} }
---- ----