【问题标题】:read data from HBase by using Spark with JAVA使用 Spark 和 JAVA 从 HBase 读取数据
【发布时间】:2017-02-21 14:33:07
【问题描述】:

我想使用 JAVA 通过 Spark 访问 HBase。除了this 之外,我还没有找到任何示例。答案里写着,

你也可以用 Java 写这个

我从How to read from hbase using spark复制了这段代码:

import org.apache.hadoop.hbase.client.{HBaseAdmin, Result}
import org.apache.hadoop.hbase.{ HBaseConfiguration, HTableDescriptor }
import org.apache.hadoop.hbase.mapreduce.TableInputFormat
import org.apache.hadoop.hbase.io.ImmutableBytesWritable

import org.apache.spark._

object HBaseRead {
  def main(args: Array[String]) {
    val sparkConf = new SparkConf().setAppName("HBaseRead").setMaster("local[2]")
    val sc = new SparkContext(sparkConf)
    val conf = HBaseConfiguration.create()
    val tableName = "table1"

    System.setProperty("user.name", "hdfs")
    System.setProperty("HADOOP_USER_NAME", "hdfs")
    conf.set("hbase.master", "localhost:60000")
    conf.setInt("timeout", 120000)
    conf.set("hbase.zookeeper.quorum", "localhost")
    conf.set("zookeeper.znode.parent", "/hbase-unsecure")
    conf.set(TableInputFormat.INPUT_TABLE, tableName)

    val admin = new HBaseAdmin(conf)
    if (!admin.isTableAvailable(tableName)) {
      val tableDesc = new HTableDescriptor(tableName)
      admin.createTable(tableDesc)
    }

    val hBaseRDD = sc.newAPIHadoopRDD(conf, classOf[TableInputFormat], classOf[ImmutableBytesWritable], classOf[Result])
    println("Number of Records found : " + hBaseRDD.count())
    sc.stop()
  }
}

谁能给我一些提示如何找到正确的依赖项、对象和东西?

似乎HBaseConfigurationhbase-client 中,但我实际上坚持使用TableInputFormat.INPUT_TABLE。这不应该在同一个依赖项中吗?

有没有更好的方法用 spark 访问 hbase?

【问题讨论】:

    标签: java hadoop apache-spark hbase


    【解决方案1】:

    TableInputFormat 类在 hbase-server.jar 中,您需要在 pom.xml 中添加该依赖项。请在 Spark 用户列表中查看HBase and non-existent TableInputFormat

    <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase-server</artifactId>
        <version>1.3.0</version>
    </dependency>
    

    以下是使用 Spark 从 Hbase 读取的示例代码。

    public static void main(String[] args) throws Exception {
        SparkConf sparkConf = new SparkConf().setAppName("HBaseRead").setMaster("local[*]");
        JavaSparkContext jsc = new JavaSparkContext(sparkConf);
        Configuration hbaseConf = HBaseConfiguration.create();
        hbaseConf.set(TableInputFormat.INPUT_TABLE, "my_table");
        JavaPairRDD<ImmutableBytesWritable, Result> javaPairRdd = jsc.newAPIHadoopRDD(hbaseConf, TableInputFormat.class,ImmutableBytesWritable.class, Result.class);
        jsc.stop();
      }
    }
    

    【讨论】:

      【解决方案2】:

      是的。有。使用来自 Cloudera 的SparkOnHbase

      <dependency>
          <groupId>org.apache.hbase</groupId>
          <artifactId>hbase-spark</artifactId>
          <version>1.2.0-cdh5.7.0</version>
      </dependency>
      

      并且使用 HBase 扫描从您的 HBase 表中读取数据(如果您知道要检索的行的键,则使用 Bulk Get)。

      Configuration conf = HBaseConfiguration.create();
      conf.addResource(new Path("/etc/hbase/conf/core-site.xml"));
      conf.addResource(new Path("/etc/hbase/conf/hbase-site.xml"));
      JavaHBaseContext hbaseContext = new JavaHBaseContext(jsc, conf);
      
      Scan scan = new Scan();
      scan.setCaching(100);
      
      JavaRDD<Tuple2<byte[], List<Tuple3<byte[], byte[], byte[]>>>> hbaseRdd = hbaseContext.hbaseRDD(tableName, scan);
      
      System.out.println("Number of Records found : " + hBaseRDD.count())
      

      【讨论】:

      • 抱歉回复晚了,我只是尝试添加hbase-spark。我意识到 artefact-id 不在 maven-central 中。所以我在 pom 中添加了https://repository.cloudera.com/artifactory/cloudera-repos/ 作为存储库。它仍然显示The POM for org.apache.hbase:hbase-spark:jar:1.2.0-cdh5.7.0 is missing, no dependency information available,有什么建议吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-23
      • 1970-01-01
      • 1970-01-01
      • 2013-05-13
      • 2019-03-14
      相关资源
      最近更新 更多