You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@hbase.apache.org by "Jean-Marc Spaggiari (JIRA)" <ji...@apache.org> on 2014/06/12 13:29:02 UTC

[jira] [Comment Edited] (HBASE-11337) Document how to create a table using Java

    [ https://issues.apache.org/jira/browse/HBASE-11337?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14029051#comment-14029051 ] 

Jean-Marc Spaggiari edited comment on HBASE-11337 at 6/12/14 11:28 AM:
-----------------------------------------------------------------------

You're welcome ;)

In this example method createIfNotExist is not used (old cut&paste, sorry) and can be removed. Also there is some strange "Unknown macro" that I don't know where it's coming from.

Here is a simplified and cleaned version:
{code}
  public static void createOrOverwrite(HBaseAdmin admin, HTableDescriptor table) throws IOException {
    if (admin.tableExists(table.getName())) {
      admin.disableTable(table.getName());
      admin.deleteTable(table.getName());
    }
    admin.createTable(table);
  }

  public static void createSchemaTables (Configuration config) {
    try {
      final HBaseAdmin admin = new HBaseAdmin(config);
      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);
    }
  }
{code}


was (Author: jmspaggi):
You're welcome ;)

In this example method createIfNotExist is not used (old cut&paste, sorry) and can be removed. Also there is some strange "Unknown macro" that I don't know where it's coming from.

Here is a simplified and cleaned version:
{code}
  public static void createOrOverwrite(HBaseAdmin admin, HTableDescriptor table) throws IOException {
    if (admin.tableExists(table.getName())) {
      admin.disableTable(table.getName());
      admin.deleteTable(table.getName());
    }
    admin.createTable(table);
  }

  public static void createSchemaTables (Configuration config) {
    try {
      final HBaseAdmin admin = new HBaseAdmin(config);
      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);
    }
}
{code}

> Document how to create a table using Java
> -----------------------------------------
>
>                 Key: HBASE-11337
>                 URL: https://issues.apache.org/jira/browse/HBASE-11337
>             Project: HBase
>          Issue Type: Bug
>          Components: Admin, documentation
>            Reporter: Misty Stanley-Jones
>            Assignee: Misty Stanley-Jones
>         Attachments: HBASE-11337.patch
>
>
> Example code from [~jmspaggi]
> {quote}
> package com.cloudera.sa.hp.hbase.admin;
> import java.io.IOException;
> import org.apache.hadoop.hbase.HBaseConfiguration;
> import org.apache.hadoop.hbase.HColumnDescriptor;
> import org.apache.hadoop.hbase.HTableDescriptor;
> import org.apache.hadoop.hbase.TableName;
> import org.apache.hadoop.hbase.client.HBaseAdmin;
> import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
> import org.apache.hadoop.conf.Configuration;
> import static com.cloudera.sa.hp.hbase.Constants.*;
> public class CreateSchema {
>   public static void createIfNotExist(HBaseAdmin admin, HTableDescriptor table) throws IOException {
>     if (admin.tableExists(table.getName())) {
>       admin.disableTable(table.getName());
>       admin.deleteTable(table.getName());
>     }
>     admin.createTable(table);
>   }
>   public static void main(String[] args) {
>     /**********************************************************************/
>     /***** Create application schema. *************************************/
>     /**********************************************************************/
>     Configuration config = HBaseConfiguration.create();
>     config.set("hbase.zookeeper.quorum", "192.168.56.102"); // Here we are running zookeeper locally
>     try {
>       final HBaseAdmin admin = new HBaseAdmin(config);
>       HTableDescriptor table_assetmeta = new HTableDescriptor(TableName.valueOf(TABLE_ASSETMETA));
>       table_assetmeta.addFamily(new HColumnDescriptor(CF_DEFAULT).setCompressionType(Algorithm.GZ));
>       System.out.print("Creating table_assetmeta. ");
>       admin.createTable(table_assetmeta);
>       System.out.println(" Done.");
>       admin.close();
>     } catch (Exception e) {
>       e.printStackTrace();
>     }
>   }
> }
> {quote}



--
This message was sent by Atlassian JIRA
(v6.2#6252)