2016-08-01 64 views
0

我在HBase的一个表,它具有以下数据:我如何从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 

我试图读取该数据为星火,然后打印表里面的数据到控制台。我对完成这个代码如下:

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() 

输出看起来是这样的:因为只有两个在HBase的表中的记录

null 
null 
Number of Records found : 2 

计数是正确的。但为什么它显示值为空?而且,我如何才能实际打印表格中的值?

谢谢。

回答

0

row._2.getValue("custid".getBytes(), "brid".getBytes())需要的参数列族,预选赛(列名),你的情况,你有2个家庭和空字符串作为预选赛。因为custid:bird是无效的列名返回null。

打印某物的尝试:row._2.getValue("bird".getBytes(), "".getBytes())