[英]How to connect to remote HBase in Java?
我有一個獨立的 HBase 服務器。 這是我的 hbase-site.xml:
<configuration>
<property>
<name>hbase.rootdir</name>
<value>file:///hbase_data</value>
</property>
</configuration>
我正在嘗試編寫一個 Java 程序來操作 HBase 中的數據。
如果我在 HBase 服務器上運行該程序,它工作正常。 但我不知道如何配置它以進行遠程訪問。
Configuration config = HBaseConfiguration.create();
HTable table = new HTable(config, "test");
Scan s = new Scan();
我嘗試添加 IP 和端口,它不起作用:
config.set("hbase.master", "146.169.35.28:60000")
誰能告訴我該怎么做?
謝謝!
這是我們用來創建用於連接到 HBase 的 HTable 的系統的片段
Configuration hConf = HBaseConfiguration.create(conf);
hConf.set(Constants.HBASE_CONFIGURATION_ZOOKEEPER_QUORUM, hbaseZookeeperQuorum);
hConf.setInt(Constants.HBASE_CONFIGURATION_ZOOKEEPER_CLIENTPORT, hbaseZookeeperClientPort);
HTable hTable = new HTable(hConf, tableName);
高溫高壓
編輯:示例值:
public static final String HBASE_CONFIGURATION_ZOOKEEPER_QUORUM = "hbase.zookeeper.quorum";
public static final String HBASE_CONFIGURATION_ZOOKEEPER_CLIENTPORT = "hbase.zookeeper.property.clientPort";
...
hbaseZookeeperQuorum="PDHadoop1.corp.CompanyName.com,PDHadoop2.corp.CompanyName.com";
hbaseZookeeperClientPort=10000;
tableName="HBaseTableName";
hbase.master
是@Deprecated。 客戶端使用 Zookeeper 獲取其 HBase 服務器的當前主機名/端口。
@Deprecated
config.set("hbase.master", "146.169.35.28:60000")
Hadoop 和 HBase 對 DNS 和/etc/hosts
配置非常敏感。 確保您的主機名不指向127.0.0.1
否則它將啟動許多僅在 localhost 上偵聽的服務。 盡量不要在設置中的任何地方使用 IP 地址。
我的/etc/hosts
:
192.168.2.3 cloudera-vm # Added by NetworkManager
127.0.0.1 localhost.localdomain localhost
127.0.1.1 cloudera-vm-local localhost
/etc/hbase/hbase-site.xml
應該設置set distributed=false
(因為您僅將其用於測試):
<property>
<name>hbase.cluster.distributed</name>
<value>false</value>
</property>
/etc/zookeeper/zoo.cfg
# the port at which the clients will connect
clientPort=2181
server.0=cloudera-vm:2888:3888
我的 Java 進程列表:
root@cloudera-vm:~# jps
1643 TaskTracker
1305 JobTracker
1544 SecondaryNameNode
2037 Bootstrap
9622 DataNode
10144 Jps
9468 NameNode
1948 RunJar
9746 HMaster
簡而言之,這就是我使用的:
Configuration hBaseConfig = HBaseConfiguration.create();
hBaseConfig.setInt("timeout", 120000);
hBaseConfig.set("hbase.master", "*" + hbaseHost + ":9000*");
hBaseConfig.set("hbase.zookeeper.quorum",zookeeperHost);
hBaseConfig.set("hbase.zookeeper.property.clientPort", "2181");
對於 hBaseHost 和 zookeeperHost,我只需傳遞安裝了 zookeeper 的集群計算機的 ip 地址。 當然你也可以參數化端口號。 我不是 100% 確定這是確保成功連接的最佳方式,但到目前為止它沒有任何問題。
據我所知,如果你想連接到遠程 hbase 服務器,普通的 java 客戶端不起作用,我們只是聲明配置並嘗試連接到遠程 hbase,如珍貴答案中所述。
我已經嘗試過上面的東西,但從未成功過。 相反,我使用 Thrift API 連接到遠程服務器,
此鏈接是使用 Thrift API java 客戶端的最佳示例。它確實有效。我正在使用相同的客戶端。 但是在仔細使用它之前,通過代碼 go 並發出那些你不需要的項目。 我還提供了成功工作的示例代碼。
public class ThriftClient
{
port = 9090;
//Connection to hbase
TTransport transport = new TSocket(hostname, port);
TProtocol protocol = new TBinaryProtocol(transport, true, true);
Hbase.Client client = new Hbase.Client(protocol);
transport.open();
int z=Link.length();
byte[] tablename = bytes("YOUR TABLE NAME");
// Create the demo table with two column families, entry: and unused:
ArrayList<ColumnDescriptor> columns = new ArrayList<ColumnDescriptor>();
ColumnDescriptor col = null;
col = new ColumnDescriptor();
col.name = ByteBuffer.wrap(bytes("YOUR_COLUMN_FAMILY_NAME"));
col.maxVersions = 10;
columns.add(col);
System.out.println("creating table: " + utf8(tablename));
try
{
client.createTable(ByteBuffer.wrap(tablename), columns);
}
catch (AlreadyExists ae)
{
System.out.println("WARN: " + ae.message);
}
Map<ByteBuffer, ByteBuffer> dummyAttributes = null;
boolean writeToWal = false;
// Test UTF-8 handling
byte[] invalid = {(byte) 'f', (byte) 'o', (byte) 'o', (byte) '-',
(byte) 0xfc, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1};
byte[] valid = {(byte) 'f', (byte) 'o', (byte) 'o', (byte) '-',
(byte) 0xE7, (byte) 0x94, (byte) 0x9F, (byte) 0xE3, (byte) 0x83,
(byte) 0x93, (byte) 0xE3, (byte) 0x83, (byte) 0xBC, (byte) 0xE3,
(byte) 0x83, (byte) 0xAB};
ArrayList<Mutation> mutations;
// Run some operations on a bunch of rows
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumIntegerDigits(10);
nf.setGroupingUsed(false);
byte[] row=bytes("YOUR ROW NAME");
mutations = new ArrayList<Mutation>();
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("YOUR_COLUMN_FAMILY_NAME:YOUR_COLUMN_NAME")), ByteBuffer.wrap(bytes("YOUR_ROW_VALUE")), writeToWal));
client.mutateRow(ByteBuffer.wrap(tablename), ByteBuffer.wrap(row), mutations, dummyAttributes);
transport.close();
// Helper to translate byte[]'s to UTF8 strings
private static String utf8(byte[] buf) {
try {
return decoder.decode(ByteBuffer.wrap(buf)).toString();
} catch (CharacterCodingException e) {
return "[INVALID UTF-8]";
}
}
// Helper to translate strings to UTF8 bytes
private static byte[] bytes(String s) {
try {
return s.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
}
就我而言,在玩了很多 /etc/hosts 之后,我最終在日志文件“hbase-bgi-master-servername.log”中找到了以下行:
"2017-11-21 19:56:32,999 INFO [RS:0;servername:45553] regionserver.HRegionServer: 充當 servername.local.lan,45553,1511290584538, RpcServer on servername.local.lan/172.0.1.2:45553 , sessionid=0x15fdff039790002"
始終確保完整的主機名(在我的例子中是“servername.local.lan”)實際上在客戶端和服務器端都指向服務器的 IP。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.