😁 作者:Teddy (公众号:鸡仓故事汇)
⏰ 时间:2021-03-09 14:08:15
☁️ 天气:多云转晴
* 今日分享
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import java.io.IOException;
public class HBaseConnection {
public static void main(String[] args) throws IOException {
//HBsae配置信息
Configuration configuration = HBaseConfiguration.create();
//注意根据自己zookeeper.znode.parent的配置信息进行修改。 (解释1)
configuration.set("zookeeper.znode.parent","/hbase-unsecure");
configuration.set("hbase.zookeeper.quorum","192.168.8.30");
configuration.set("hbase.zookeeper.property.clientPort","2181");
// HBaseAdmin baseAdmin = new HBaseAdmin(configuration);
Admin admin = ConnectionFactory.createConnection(configuration).getAdmin();
if(admin !=null){
try {
//获取到数据库所有表信息
HTableDescriptor[] allTable = admin.listTables();
for (HTableDescriptor hTableDescriptor : allTable) {
System.out.println(hTableDescriptor.getNameAsString());
}
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
参数说明:
- configuration.set(“zookeeper.znode.parent”,"/hbase-unsecure");
一定要与hbase-site-xml里面的配置信息 zookeeper.znode.parent 保持一致
- 地址配置:configuration.set(“hbase.zookeeper.quorum”,“192.168.8.30”);
- 端口号配置:configuration.set(“hbase.zookeeper.property.clientPort”,“2181”);
问题:
- 没有hadoop环境导致连接异常
java.io.IOException: HADOOP_HOME or hadoop.home.dir are not set
ok!到这里就大功告成,小编(Teddy)在这里先感谢大家的到来。
虽然不是太详细,小编已经很努力,给小编来个一键三连(点赞,关注,收藏),小编会越来越努力。。。