Return-Path: X-Original-To: apmail-hbase-commits-archive@www.apache.org Delivered-To: apmail-hbase-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id B597D18668 for ; Mon, 4 May 2015 20:45:08 +0000 (UTC) Received: (qmail 58609 invoked by uid 500); 4 May 2015 20:45:07 -0000 Delivered-To: apmail-hbase-commits-archive@hbase.apache.org Received: (qmail 58560 invoked by uid 500); 4 May 2015 20:45:07 -0000 Mailing-List: contact commits-help@hbase.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@hbase.apache.org Delivered-To: mailing list commits@hbase.apache.org Received: (qmail 58545 invoked by uid 99); 4 May 2015 20:45:07 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 04 May 2015 20:45:07 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 7E669E0D59; Mon, 4 May 2015 20:45:07 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: stack@apache.org To: commits@hbase.apache.org Message-Id: <1e7ad7d281dd4a10a14c98f30bb53c07@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: hbase git commit: Fixed example code Date: Mon, 4 May 2015 20:45:07 +0000 (UTC) Repository: hbase Updated Branches: refs/heads/master 1f02fffd0 -> 0dfb36472 Fixed example code Signed-off-by: stack Project: http://git-wip-us.apache.org/repos/asf/hbase/repo Commit: http://git-wip-us.apache.org/repos/asf/hbase/commit/0dfb3647 Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/0dfb3647 Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/0dfb3647 Branch: refs/heads/master Commit: 0dfb3647237523204765e95cc1c161751e8a8987 Parents: 1f02fff Author: Lars Francke Authored: Thu Apr 30 09:37:49 2015 +0200 Committer: stack Committed: Mon May 4 13:44:54 2015 -0700 ---------------------------------------------------------------------- src/main/asciidoc/_chapters/hbase_apis.adoc | 109 +++++++++++------------ 1 file changed, 53 insertions(+), 56 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hbase/blob/0dfb3647/src/main/asciidoc/_chapters/hbase_apis.adoc ---------------------------------------------------------------------- diff --git a/src/main/asciidoc/_chapters/hbase_apis.adoc b/src/main/asciidoc/_chapters/hbase_apis.adoc index 85dbad1..6d2777b 100644 --- a/src/main/asciidoc/_chapters/hbase_apis.adoc +++ b/src/main/asciidoc/_chapters/hbase_apis.adoc @@ -36,102 +36,99 @@ See <> for more information. == Examples -.Create a Table Using Java +.Create, modify and delete a Table Using Java ==== [source,java] ---- package com.example.hbase.admin; +package util; + 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.HColumnDescriptor; +import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.HTableDescriptor; 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.ConnectionFactory; import org.apache.hadoop.hbase.io.compress.Compression.Algorithm; -import org.apache.hadoop.conf.Configuration; -import static com.example.hbase.Constants.*; +public class Example { -public class CreateSchema { + private static final String TABLE_NAME = "MY_TABLE_NAME_TOO"; + private static final String CF_DEFAULT = "DEFAULT_COLUMN_FAMILY"; public static void createOrOverwrite(Admin admin, HTableDescriptor table) throws IOException { - if (admin.tableExists(table.getName())) { - admin.disableTable(table.getName()); - admin.deleteTable(table.getName()); + if (admin.tableExists(table.getTableName())) { + admin.disableTable(table.getTableName()); + admin.deleteTable(table.getTableName()); } admin.createTable(table); } - public static void createSchemaTables (Configuration config) { - try { - final Admin admin = new Admin(config); + public static void createSchemaTables(Configuration config) throws IOException { + try (Connection connection = ConnectionFactory.createConnection(config); + Admin admin = connection.getAdmin()) { + HTableDescriptor table = new HTableDescriptor(TableName.valueOf(TABLE_NAME)); table.addFamily(new HColumnDescriptor(CF_DEFAULT).setCompressionType(Algorithm.SNAPPY)); System.out.print("Creating table. "); createOrOverwrite(admin, table); System.out.println(" Done."); - - admin.close(); - } catch (Exception e) { - e.printStackTrace(); - System.exit(-1); } } -} ----- -==== - -.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)); + public static void modifySchema (Configuration config) throws IOException { + try (Connection connection = ConnectionFactory.createConnection(config); + Admin admin = connection.getAdmin()) { - // Create a new table. + TableName tableName = TableName.valueOf(TABLE_NAME); + if (admin.tableExists(tableName)) { + System.out.println("Table does not exist."); + System.exit(-1); + } - System.out.print("Creating table_assetmeta. "); - admin.createTable(table_assetmeta); - System.out.println(" Done."); + HTableDescriptor table = new HTableDescriptor(tableName); - // Update existing table - HColumnDescriptor newColumn = new HColumnDescriptor("NEWCF"); - newColumn.setCompactionCompressionType(Algorithm.GZ); - newColumn.setMaxVersions(HConstants.ALL_VERSIONS); - admin.addColumn(tableName, newColumn); + // Update existing table + HColumnDescriptor newColumn = new HColumnDescriptor("NEWCF"); + newColumn.setCompactionCompressionType(Algorithm.GZ); + newColumn.setMaxVersions(HConstants.ALL_VERSIONS); + admin.addColumn(tableName, newColumn); - // Update existing column family - HColumnDescriptor existingColumn = new HColumnDescriptor(CF_DEFAULT); - existingColumn.setCompactionCompressionType(Algorithm.GZ); - existingColumn.setMaxVersions(HConstants.ALL_VERSIONS); - table_assetmeta.modifyFamily(existingColumn) - admin.modifyTable(tableName, table_assetmeta); + // Update existing column family + HColumnDescriptor existingColumn = new HColumnDescriptor(CF_DEFAULT); + existingColumn.setCompactionCompressionType(Algorithm.GZ); + existingColumn.setMaxVersions(HConstants.ALL_VERSIONS); + table.modifyFamily(existingColumn); + admin.modifyTable(tableName, table); - // Disable an existing table - admin.disableTable(tableName); + // Disable an existing table + admin.disableTable(tableName); - // Delete an existing column family - admin.deleteColumn(tableName, CF_DEFAULT); + // Delete an existing column family + admin.deleteColumn(tableName, CF_DEFAULT.getBytes("UTF-8")); - // Delete a table (Need to be disabled first) - admin.deleteTable(tableName); + // Delete a table (Need to be disabled first) + admin.deleteTable(tableName); + } + } + public static void main(String... args) throws IOException { + Configuration config = HBaseConfiguration.create(); - admin.close(); - } catch (Exception e) { - e.printStackTrace(); - System.exit(-1); + //Add any necessary configuration files (hbase-site.xml, core-site.xml) + config.addResource(new Path(System.getenv("HBASE_CONF_DIR"), "hbase-site.xml")); + config.addResource(new Path(System.getenv("HADOOP_CONF_DIR"), "core-site.xml")); + createSchemaTables(config); + modifySchema(config); } } ----