HBASE-11337 Document how to create, modify, delete a table using Java (Misty Stanley-Jones)
This commit is contained in:
parent
6764275ff0
commit
91991b72af
|
@ -3300,7 +3300,7 @@ All the settings that apply to normal compactions (file size limits, etc.) apply
|
|||
</section>
|
||||
|
||||
</chapter> <!-- architecture -->
|
||||
|
||||
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="hbase_apis.xml"/>
|
||||
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="external_apis.xml"/>
|
||||
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="thrift_filter_language.xml"/>
|
||||
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="cp.xml" />
|
||||
|
|
|
@ -29,8 +29,10 @@
|
|||
*/
|
||||
-->
|
||||
<title>Apache HBase External APIs</title>
|
||||
<para> This chapter will cover access to Apache HBase either through non-Java languages, or through custom protocols.
|
||||
</para>
|
||||
<para> This chapter will cover access to Apache HBase either through non-Java languages, or
|
||||
through custom protocols. For information on using the native HBase APIs, refer to <link
|
||||
xlink:href="http://hbase.apache.org/apidocs/index.html">User API Reference</link> and the new <xref
|
||||
linkend="hbase_apis" /> chapter. </para>
|
||||
<section xml:id="nonjava.jvm">
|
||||
<title>Non-Java Languages Talking to the JVM</title>
|
||||
<para>Currently the documentation on this topic in the
|
||||
|
|
|
@ -0,0 +1,133 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<chapter
|
||||
version="5.0"
|
||||
xml:id="hbase_apis"
|
||||
xmlns="http://docbook.org/ns/docbook"
|
||||
xmlns:xlink="http://www.w3.org/1999/xlink"
|
||||
xmlns:xi="http://www.w3.org/2001/XInclude"
|
||||
xmlns:svg="http://www.w3.org/2000/svg"
|
||||
xmlns:m="http://www.w3.org/1998/Math/MathML"
|
||||
xmlns:html="http://www.w3.org/1999/xhtml"
|
||||
xmlns:db="http://docbook.org/ns/docbook">
|
||||
<!--
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
-->
|
||||
<title>Apache HBase APIs</title>
|
||||
<para>This chapter provides information about performing operations using HBase native APIs. This
|
||||
information is not exhaustive, and provides a quick reference in addition to the <link
|
||||
xlink:href="http://hbase.apache.org/apidocs/index.html">User API
|
||||
Reference</link>. The examples here are not comprehensive or complete, and should be used for
|
||||
purposes of illustration only.</para>
|
||||
<para>Apache HBase also works with multiple external APIs. See <xref linkend="external_apis" />
|
||||
for more information.</para>
|
||||
|
||||
<example>
|
||||
<title>Create a Table Using Java</title>
|
||||
<para>This example has been tested on HBase 0.96.1.1.</para>
|
||||
<programlisting>
|
||||
package com.example.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.example.hbase.Constants.*;
|
||||
|
||||
public class CreateSchema {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
</programlisting>
|
||||
</example>
|
||||
<example>
|
||||
<title>Add, Modify, and Delete a Table</title>
|
||||
<para>This example has been tested on HBase 0.96.1.1.</para>
|
||||
<programlisting>
|
||||
public static void upgradeFrom0 (Configuration config) {
|
||||
|
||||
try {
|
||||
final HBaseAdmin admin = new HBaseAdmin(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
|
||||
HColumnDescriptor newColumn = new HColumnDescriptor("NEWCF");
|
||||
newColumn.setCompactionCompressionType(Algorithm.GZ);
|
||||
newColumn.setMaxVersions(HConstants.ALL_VERSIONS);
|
||||
admin.addColumn(tableName, newColumn);
|
||||
|
||||
// Disable an existing table
|
||||
admin.disableTable(tableName);
|
||||
|
||||
// Delete an existing column family
|
||||
admin.deleteColumn(tableName, CF_DEFAULT);
|
||||
|
||||
// Delete a table (Need to be disabled first)
|
||||
admin.deleteTable(tableName);
|
||||
|
||||
|
||||
admin.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.exit(-1);
|
||||
}
|
||||
}
|
||||
</programlisting>
|
||||
</example>
|
||||
|
||||
</chapter>
|
Loading…
Reference in New Issue