1. 程式人生 > >Hbase Java API調用實例

Hbase Java API調用實例

dep ble oar client dmi ktr bsp str exception

  • pom依賴

  hbase.version使用與Hbase數據庫兼容的版本

        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>${hbase.version}</version>
            <scope>runtime</scope>
</dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-common</artifactId> <version>${hbase.version}</version> <scope>runtime</scope> </dependency
> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>${hbase.version}</version> <scope>runtime</scope> </dependency>
  • 配置信息
static Configuration configuration = HBaseConfiguration.create();
static {
      configuration.set("hbase.zookeeper.quorum", "localhost");
      configuration.set("hbase.zookeeper.property.clientPort", "2181");
}
  • 查詢所有表
private String[] getHbaseTables(Configuration configuration) {
        ArrayList<String> tables = new ArrayList<>();
        try {
            HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);
            if (hBaseAdmin != null) {
                TableName[] tableNames = hBaseAdmin.listTableNames();
                for (TableName tableName : tableNames) {
                    tables.add(tableName.getNameAsString());
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return tables.toArray(new String[tables.size()]);
    }

Hbase Java API調用實例