問題說明:自己用虛擬機安裝了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無反應的問題。