範例一:新增Table
執行方法
測試用打包檔 tsmcHBase_100204.jar
$ /opt/hadoop/bin/hadoop jar tsmcHBase_100204.jar CreateTable
程式碼
package tsmc;
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.client.HBaseAdmin;
public class CreateTable {
public static void createHBaseTable(String tablename, String family)
throws IOException {
// HTableDescriptor contains the name of an HTable, and its column
// families
// HTableDescriptor 用來描述table的屬性
HTableDescriptor htd = new HTableDescriptor(tablename);
// HColumnDescriptor HColumnDescriptor contains information about a
// column family such as the number of versions, compression settings,
// etc.
// HTableDescriptor 透過 add() 方法來加入Column family
htd.addFamily(new HColumnDescriptor(family));
// HBaseConfiguration 能接收 hbase-site.xml 的設定值
HBaseConfiguration config = new HBaseConfiguration();
// 檔案的操作則使用 HBaseAdmin
HBaseAdmin admin = new HBaseAdmin(config);
// 檢查
if (admin.tableExists(tablename)) {
System.out.println("Table: " + tablename + "Existed.");
} else {
System.out.println("create new table: " + tablename);
// 建立
admin.createTable(htd);
}
}
static public void main(String argv[]) throws IOException {
String tablename = "tsmc";
String family = "Detail";
System.out.println("1. create table :" + tablename);
createHBaseTable(tablename, family);
}
}