import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
public class HelloHBase {
public static void main(String[] args) throws Exception{
//定義運作環境及相關配置
Configuration config = HBaseConfiguration.create();
config.addResource(new Path(ClassLoader.getSystemResource("hbase-site.xml").toURI()));
config.addResource(new Path(ClassLoader.getSystemResource("core-site.xml").toURI()));
try( Connection connection = ConnectionFactory.createConnection(config);
Admin admin = connection.getAdmin() ){
// 定義表名
TableName tname = TableName.valueOf("mytable") ;
//定義表
//HTableDescriptor table = new HTableDescriptor(tname) ;
TableDescriptorBuilder tableDescBuilder = TableDescriptorBuilder.newBuilder(tname);
//定義列族
// HColumnDescriptor mycf = new HColumnDescriptor("mycf") ;
ColumnFamilyDescriptor mycf = ColumnFamilyDescriptorBuilder
.newBuilder(Bytes.toBytes("mycf")).build() ;
tableDescBuilder.setColumnFamily( mycf);
TableDescriptor table = tableDescBuilder.build();
//Admin 類是HBase API中負責管理建表、改表、删表等中繼資料操作的管理接口
Admin admin = connection.getAdmin() ;
// 執行建表操作
admin.createTable( table) ;
}
}
}