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