2016-08-01 69 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())