亲宝软件园·资讯

展开

java连接虚拟机Hbase无反应

iRossini 人气:0

问题说明

自己用虚拟机安装了Hbase、zookeeper,然后想用java访问。

代码如下

 import java.io.IOException; 
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
 
 public class HbaseTest {
	 public static void main(String[] args) throws  MasterNotRunningException, IOException{
		String tableName = "myTest";
 
		Configuration conf = HBaseConfiguration.create();
		conf.set("hbase.master", "192.168.48.129:16010");
		conf.set("hbase.zookeeper.property.clientPort", "2181");
//		conf.set("hbase.zookeeper.quorum", "master");
		conf.set("hbase.zookeeper.quorum", "192.168.48.129");
 
		Connection conn = ConnectionFactory.createConnection(conf);
		Table table = conn.getTable(TableName.valueOf(tableName));
 
		Get g = new Get(Bytes.toBytes("row1"));
		Result result = table.get(g);
 
		byte [] value = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"));
		byte[] value1 = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age"));
 
		table.close();
		System.out.println(result.toString());
		System.out.println(Bytes.toString(value));
		System.out.println(Bytes.toString(value1));
		}    
 }

结果一直卡在result这一样没反应。

解决方案

1.由于Hbase自带有zookeeper

可以先禁用掉自带的zookeeper。

命令:vim   hbase/conf/hbase-env.sh

2.由于这里只是想测试下java连接hbae

也并没有搭建Hdoop环境,所以Hbase的hhbase-size.xml配置如下

3.再对zookeeper进行配置 zoo.cfg

4.因为是在windows环境下

用java访问虚拟机的Hbase,所以还需要以下配置

命令:vim /etc/host  添加一行,IP   hostname

命令:vim /etc/sysconfig/network-scripts/ifcfg-eth0 ,修改设备名

windows中相应配置:C:\Windows\System32\drivers\etc  在HOST文件中添加IP hostname

所有的配置就到这里结束,启动zookeeper,启动Hbase

java连接Hbase测试

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
 
 public class HbaseTest {
        public static void main(String[] args) throws  MasterNotRunningException, IOException{
              String tableName = "myTest";
              Configuration conf = HBaseConfiguration.create();
              conf.set("hbase.master", "192.168.48.129:16010");
              conf.set("hbase.zookeeper.property.clientPort", "2181");
//            conf.set("hbase.zookeeper.quorum", "master");
              conf.set("hbase.zookeeper.quorum", "192.168.48.129");
              Connection conn = ConnectionFactory.createConnection(conf);
              Table table = conn.getTable(TableName.valueOf(tableName));
              Get g = new Get(Bytes.toBytes("row1"));
              Result result = table.get(g);
              byte [] value = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"));
              byte[] value1 = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age"));
              table.close();
              System.out.println(result.toString());
              System.out.println(Bytes.toString(value));
              System.out.println(Bytes.toString(value1));
              }  
 }

我个人使用如上方法能够解决连接Hbase无反应的问题。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论