繁体   English   中英

使用 Spark 读取带有 where 子句的 HBase 表

[英]Read HBase table with where clause using Spark

我正在尝试使用 Spark Scala API 读取 HBase 表。

示例代码:

conf.set("hbase.master", "localhost:60000")
conf.set("hbase.zookeeper.quorum", "localhost")
conf.set(TableInputFormat.INPUT_TABLE, tableName)
val hBaseRDD = sc.newAPIHadoopRDD(conf, classOf[TableInputFormat], classOf[ImmutableBytesWritable], classOf[Result])
println("Number of Records found : " + hBaseRDD.count())

如果我使用newAPIHadoopRDD如何添加where子句?

或者我们需要使用任何Spark Hbase Connector来实现这一点?

我看到了下面的 Spark Hbase 连接器,但我没有看到任何带有 where 子句的示例代码。

https://github.com/nerdammer/spark-hbase-connector

您可以使用 HortonWorks 的 SHC 连接器来实​​现这一点。

https://github.com/hortonworks-spark/shc

这是 Spark 2 的代码示例。

 val catalog =
        s"""{
            |"table":{"namespace":"default", "name":"my_table"},
            |"rowkey":"id",
            |"columns":{
            |"id":{"cf":"rowkey", "col":"id", "type":"string"},
            |"name":{"cf":"info", "col":"name", "type":"string"},
            |"age":{"cf":"info", "col":"age", "type":"string"}
            |}
            |}""".stripMargin

    val spark = SparkSession
        .builder()
        .appName("hbase spark")
        .getOrCreate()

    val df = spark
        .read
        .options(
            Map(
                HBaseTableCatalog.tableCatalog -> catalog
            )
        )
        .format("org.apache.spark.sql.execution.datasources.hbase")
        .load()

    df.show()

然后,您可以在数据帧上使用任何方法。 前任 :

df.where(df("age") === 20)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM