hbase的Java基本操作 建表,建列簇操作
private static Connection connection; private static Admin admin; public static void createTable(String tableName, String[] fields) throws IOException { if(admin.tableExists(TableName.valueOf(tableName))) { deleteTable(tableName); } //2.TableDescriptorBuilder.newBuilder构建表描述构建器 TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName)); for (int i=0;i<fields.length;i++) { //3.创建列簇构造描述器 ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(fields[i])); //4.构建列簇描述 ColumnFamilyDescriptor cfDes = columnFamilyDescriptorBuilder.build(); //建立表与列簇的关联关系 tableDescriptorBuilder.setColumnFamily(cfDes); } TableDescriptor tableDescriptor = tableDescriptorBuilder.build(); admin.createTable(tableDescriptor); } public static void main(String[] args) throws IOException { //用HBaseConfiguration.create();创建HBase的配置 Configuration configuration = HBaseConfiguration.create(); //用ConnectionFactory.createConnection创建HBase()连接 connection = ConnectionFactory.createConnection(configuration); // 创建表,要给予HBase获取Admin对象 admin = connection.getAdmin(); String tablename = "WATER_BILL"; //1.创建表和列 String[] filed = {"H1", "H2", "H3"}; createTable(tablename, filed); }