簡體   English   中英

從Java客戶端連接到Mapr-DB(M3)

[英]Connecting to Mapr-DB (M3) from a Java client

我正在嘗試從M3 MapR集群的節點內運行的簡單Java應用程序與MapR-DB表進行交互。 看來我可以連接到群集,但是顯然我無法正確連接到表。 這是代碼片段:

Configuration configuration = new Configuration();
configuration.set("hbase.zookeeper.quorum", "192.168.2.1,192.168.2.2,192.168.2.3");
configuration.set("hbase.zookeeper.property.clientPort", "5181");
configuration.set("mapr.htable.impl", "com.mapr.fs.MapRHTable");
configuration.set("hbase.table.namespace.mappings", "*:/user/mapr/");

configuration = HBaseConfiguration.create(configuration);

HConnection connection = HConnectionManager.createConnection(configuration);

System.out.println("Is Master running? " + connection.isMasterRunning());

String tableName = args[0];

HTable table = (HTable) connection.getTable(tableName.getBytes());

for (HColumnDescriptor columnFamily : table.getTableDescriptor().getColumnFamilies()) {
    System.out.println("Column family: " + columnFamily.getNameAsString());
}

我有一個名為“ / user / mapr / test_table”的表(我在MapR Web控制台中看到它,可以通過hbase shell對其進行訪問)。 使用表名稱的任何合理參數運行代碼只會返回此異常:

org.apache.hadoop.hbase.TableNotFoundException: /user/mapr/test_table
    at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.getHTableDescriptor(HConnectionManager.java:2750)
    at org.apache.hadoop.hbase.client.HTable.getTableDescriptor(HTable.java:701)
    at it.noovle.bigdata.hadoop.MaprDBLocalTest.main(MaprDBLocalTest.java:49)
  • 在許多地方,我讀到MapR-DB不需要通過Zookeeper連接。 一般而言還是僅M7正確? 我目前正在運行M3。
  • 是否有通過Java HBase API解決MapR-DB表的特定方法? hbase shell我只使用'/ user / mapr / test_table'。
  • 有人可以分享一個有關M3集群運行示例的不錯示例嗎?

要連接到MapR-DB,您不需要連接到Zookeeper。 要打開表,您必須提供絕對路徑。 例如/ user / mapr / test_table。 在下面附加一個簡單的示例:

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseExample {
    public static void main(String[] args) throws IOException {
        Configuration config = HBaseConfiguration.create();
        HTable table = new HTable(config, "/user/mapr/test_table");
        Put p = new Put(Bytes.toBytes("row1"));
        p.add(Bytes.toBytes("cf1"), Bytes.toBytes("col2"),
                Bytes.toBytes("ABC"));

        table.put(p);
        Get g = new Get(Bytes.toBytes("row1"));
        Result r = table.get(g);
        byte[] value = r.getValue(Bytes.toBytes("cf1"),
                Bytes.toBytes("col2"));

        String valueStr = Bytes.toString(value);
        System.out.println("GET: " + valueStr);
        Scan s = new Scan();
        s.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("col2"));
        ResultScanner scanner = table.getScanner(s);
        try {
            for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
                System.out.println("Found row: " + rr);
            }

        } finally {
            scanner.close();
        }
    }
}

這是hbase-site.xml,它取自MapR沙箱。

-bash-4.1$ cat ./hbase/hbase-0.98.7/conf/hbase-site.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>

  <property>
    <name>hbase.rootdir</name>
    <value>maprfs:///hbase</value>
  </property>

  <property>
<name>hbase.cluster.distributed</name>
<value>true</value>
  </property>

  <property>
<name>hbase.zookeeper.quorum</name>
<value>maprdemo</value>
  </property>

  <property>
<name>hbase.zookeeper.property.clientPort</name>
<value>5181</value>
  </property>

  <property>
    <name>dfs.support.append</name>
    <value>true</value>
  </property>

  <property>
    <name>hbase.fsutil.maprfs.impl</name>
    <value>org.apache.hadoop.hbase.util.FSMapRUtils</value>
  </property>

  <property>
    <name>hbase.regionserver.handler.count</name>
    <value>30</value>
    <!-- default is 25 -->
  </property>

  <!-- uncomment this to enable fileclient logging
  <property>
    <name>fs.mapr.trace</name>
    <value>debug</value>
  </property>
  -->

  <!-- Allows file/db client to use 64 threads -->
  <property>
    <name>fs.mapr.threads</name>
    <value>64</value>
  </property>

</configuration>
-bash-4.1$ 

MapR允許您根據您的安裝使用:

  • HBase的
  • 地圖數據庫

該表的“名稱”定義此表是“ MapR-DB”還是“ Hbase”表:

  • 完整路徑(帶有/)是MapR-DB表/apps/my_tables/users
  • 簡單字符串是Hbase表的users

然后,根據表的名稱,客戶端訪問群集的方式也會有所不同:

  • MapR DB:將使用CLDB(默認值為server:7222)
  • HBase:將使用Zookeeper(默認為server:5181)

客戶端代碼在MapR-DB或Hbase之間不會更改,只有表名和“配置”會更改,我對配置的意思是:

  1. 您必須安裝和配置MapR Client
  2. 您必須確保將所有依賴項放入類路徑(hadoop客戶端,hbase-mapr客戶端)

這里有一個小項目

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM