繁体   English   中英

如何从Spark中的Hbase表读取数据?

[英]How can i read data from Hbase table in Spark?

我在Hbase中有一个表,其中包含以下数据:

ROW COLUMN+CELL
1   column=brid:, timestamp=1470047093100, value=a1234
1   column=custid:, timestamp=1470046713207, value=811411
2   column=brid:, timestamp=1470047231583, value=a6789
2   column=custid:, timestamp=1470047156905, value=848727431

我正在尝试将此数据读入Spark,然后将表内的数据打印到控制台。 我实现此目的的代码如下:

val conf = new SparkConf().setAppName("Spark Base").setMaster("local[*]")
val sc = new SparkContext(conf)

val hbaseConf = HBaseConfiguration.create()
hbaseConf.set("hbase.zookeeper.quorum", "127.0.0.1")
hbaseConf.set("hbase.zookeeper.property.clientPort", "5181") 
hbaseConf.set(TableInputFormat.INPUT_TABLE, "/path/to/custid1") 

val hbaseData = sc.newAPIHadoopRDD(hbaseConf, classOf[TableInputFormat], classOf[ImmutableBytesWritable], classOf[Result])

hbaseData.map(row => Bytes.toString(row._2.getValue("custid".getBytes(), "brid".getBytes()))).collect().foreach(println)
println("Number of Records found : " + hbaseData.count())
sc.stop()

输出看起来像这样:

null
null
Number of Records found : 2

该计数是正确的,因为Hbase表中只有两条记录。 但是为什么将值显示为null? 而且,我如何才能真正在表中打印值?

谢谢。

row._2.getValue("custid".getBytes(), "brid".getBytes())采用参数列族,限定符(列名),在您的情况下,您有2个列族和空字符串作为限定符。 由于custid:bird无效,所以返回null。

尝试打印一些内容: row._2.getValue("bird".getBytes(), "".getBytes())

暂无
暂无

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

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