编写一个Java程序,在HBase中创建表dept,emp,列都为:data。
时间: 2024-02-25 18:58:12 浏览: 86
hbase访问方式之javaapi共3页.pdf.zip
好的,以下是使用Java在HBase中创建表dept和emp,列族都为"data"的程序示例:
```java
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.TableDescriptor;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
import org.apache.hadoop.hbase.io.compress.CompressionAlgorithm;
import org.apache.hadoop.hbase.regionserver.BloomType;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseTableCreator {
public static void main(String[] args) throws Exception {
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "localhost");
Connection connection = ConnectionFactory.createConnection(config);
Admin admin = connection.getAdmin();
createTable(admin, "dept", "data");
createTable(admin, "emp", "data");
admin.close();
connection.close();
}
private static void createTable(Admin admin, String tableName, String columnFamily) throws Exception {
TableName name = TableName.valueOf(tableName);
TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(name)
.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(columnFamily))
.setCompressionType(CompressionAlgorithm.GZ)
.setBloomFilterType(BloomType.ROWCOL)
.build())
.build();
admin.createTable(tableDescriptor);
System.out.println("Table " + tableName + " created successfully.");
}
}
```
该程序使用了前面提到的方法建立与HBase数据库的连接,然后通过createTable()方法创建名为"dept"和"emp"的表,列族均为"data"。
最后,需要注意在使用完后关闭与HBase数据库的连接。
希望这个示例能够帮助到你。
阅读全文