我正在尝试使用 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 子句的示例代码。
最佳答案
您可以使用 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)
关于java - 使用 Spark 通过 where 子句读取 HBase 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40082170/