如何在Java中连接到远程HBase?

时间:2011-07-07 17:10:35

标签: hbase

我有一个Standlone 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")

谁能告诉我怎么做?

谢谢!

5 个答案:

答案 0 :(得分:27)

以下是我们用来创建HTable的系统片段,用于连接HBase

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);

HTH

编辑:示例值:

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";

答案 1 :(得分:12)

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

答案 2 :(得分:7)

简而言之,这就是我使用的:

    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%确定这是确保连接成功的最佳方法,但到目前为止它没有任何问题。

答案 3 :(得分:1)

据我所知,如果要连接到远程hbase服务器 普通的Java客户端不起作用,我们只是声明配置并尝试连接到远程hbase,如珍贵的答案所述。

我已尝试过上述内容,但从未成功过。 相反,我使用Thrift API连接到远程服务器,

This链接是使用Thrift API java客户端的最佳示例。它肯定有效。我使用相同的。 但在使用之前,请仔细检查代码并发出不需要的项目。 我也提供了成功运作的示例代码。

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;
    }
}
}

答案 4 :(得分:0)

在我使用/ etc / hosts玩很多东西后,我最终在日志文件“hbase-bgi-master-servername.log”中找到以下行:

  

“2017-11-21 19:56:32,999 INFO [RS:0;服务器名称:45553] regionserver.HRegionServer:在servername.local.lan / 172.0上用作servername.local.lan,45553,1511290584538,RpcServer。 1.2:45553,sessionid = 0x15fdff039790002“

始终确保完整的主机名(在我的情况下为“servername.local.lan”)实际上指向客户端和服务器端的服务器IP。

相关问题