如何连接hbase和Spark

How to connect hbase with Spark

我想从 hbase 加载数据,然后使用 Spark 处理它们! 我在 google 云和 hbase 1.2.5

上使用 Spark 2.0.2

在互联网上,我找到了一些使用 JavaHBaseContext 的示例,但我不知道在哪里可以找到这个 class 因为我没有任何名为 hbase- 的 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); 的错误,即:

error: unreported exception IOException; must be caught or declared to be thrown Connection connection = ConnectionFactory.createConnection(conf);

你们谁能告诉我如何使用 Spark 从 hbase 加载数据?

PS : 我编程 Java

您提到的错误与 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.
}