【问题标题】:Connecting to Mapr-DB (M3) from a Java client从 Java 客户端连接到 Mapr-DB (M3)
【发布时间】:2016-01-07 15:37:50
【问题描述】:

我正在尝试通过一个简单的 Java 应用程序与 MapR-DB 表进行交互,该应用程序在 M3 MapR 集群的一个节点中运行。似乎我能够连接到集群,但显然我无法正确连接到表。这是代码中的一个sn-p:

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 集群运行示例的体面示例吗?

【问题讨论】:

    标签: java hbase mapr


    【解决方案1】:

    要连接到 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$ 
    

    【讨论】:

    • 谢谢@Ranjit!您能否提供用于创建 HBaseConfiguration 的 hbase-site.xml 配置?再次感谢您!
    • 如果您使用的是 Maven,pom.xml 也可能很有用 :)
    • 将 hbase-site.xml 添加到我之前的回复中。
    • 现在已弃用
    【解决方案2】:

    MapR 允许您根据您的安装使用:

    • HBase
    • MapR-DB

    表的“名称”定义这是“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 客户端)

    我有一个可用的小项目here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-07
      相关资源
      最近更新 更多