【问题标题】:How to connect hbase with Spark如何将 hbase 与 Spark 连接
【发布时间】:2017-07-20 19:02:09
【问题描述】:

我想从 hbase 加载数据,然后使用 Spark 继续它们! 我在 google cloud 和 hbase 1.2.5 上使用 Spark 2.0.2

在互联网上,我找到了一些使用 JavaHBaseContext 的示例,但我不知道在哪里可以找到此类,因为我没有任何名为 hbase-spark 的 jar 文件 hbase ?

我也发现了这段代码,它使用 HBaseConfiguration 和 ConnectionFactory 与 hbase 数据库建立连接:

    Configuration conf = HBaseConfiguration.create();
    conf.addResource(new Path("/etc/hbase/conf/core-site.xml"));
    conf.addResource(new Path("/etc/hbase/conf/hbase-site.xml"));
    conf.set(TableInputFormat.INPUT_TABLE, tableName);

    Connection connection = ConnectionFactory.createConnection(conf);

    Admin admin = connection.getAdmin(); 
    Table tab = connection.getTable(TableName.valueOf(tableName));
    byte [] row = Bytes.toBytes("TestSpark");
    byte [] family1 = Bytes.toBytes("MetaData");
    byte [] height = Bytes.toBytes("height");
    byte [] width = Bytes.toBytes("width");

    Put put = new Put(row);
    put.addColumn(family1, height, Bytes.toBytes("256"));
    put.addColumn(family1, width, Bytes.toBytes("384"));

    tab.put(put);

但我收到一个关于 Connection connection = ConnectionFactory.createConnection(conf); 的错误,即:

错误:未报告的异常IOException;必须被抓住或宣布 被扔 连接连接 = ConnectionFactory.createConnection(conf);

谁能告诉我如何从 hbase 加载数据以使用 Spark 继续?

PS : 我编写 Java 程序

【问题讨论】:

标签: java apache-spark hbase


【解决方案1】:

您提到的错误与Connection connection = ConnectionFactory.createConnection(conf); 可以抛出错误有关。就像消息说的那样,你必须用 try ..catch 覆盖它:

try {    
    Connection connection = ConnectionFactory.createConnection(conf);
}
catch (Exception e) //Replace Exception with the exception thown by ConnectionFactory 
{
... Do something.
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 2020-11-04
    • 2017-03-07
    相关资源
    最近更新 更多