我是新的Spark用户,我想将流数据保存到多个Hbase表中。当我想将数据保存在一个单独的数据库中时,我没有遇到任何问题,但是有多个我无法使用。
我尝试创建多个HTable,但后来我注意到这个类只用于与单个HBase表进行通信。
有没有办法做到这一点?
这是我尝试创建多个Htables的地方(当然不起作用,但这是主意)
//HBASE Tables
val tableFull = "table1"
val tableCategoricalFiltered = "table2"
// Add local HBase conf
val conf1 = HBaseConfiguration.create()
val conf2 = HBaseConfiguration.create()
conf1.set(TableInputFormat.INPUT_TABLE, tableFull)
conf2.set(TableInputFormat.INPUT_TABLE, tableCategoricalFiltered)
//Opening Tables
val tableInputFeatures = new HTable(conf1, tableFull)
val tableCategoricalFilteredFeatures = new HTable(conf2, tableCategoricalFiltered)
这是我尝试使用它们的地方(虽然有一个HTable工作)
events.foreachRDD { event =>
var j = 0
event.foreach { feature =>
if ( j <= 49 ) {
println("Feature " + j + " : " + featuresDic(j))
println(feature)
val p_full = new Put(new String("stream " + row_full).getBytes())
p_full.add(featuresDic(j).getBytes(), "1".getBytes(), new String(feature).getBytes())
tableInputFeatures.put(p_full)
if ( j != 26 || j != 27 || j != 28 || j != 29 ) {
val p_cat = new Put(new String("stream " + row_categorical).getBytes())
p_cat.add(featuresDic(j).getBytes(), "1".getBytes(), new String(feature).getBytes())
tableCategoricalFilteredFeatures.put(p_cat)
}else{
j = 0
row_full = row_full + 1
println("Feature " + j + " : " + featuresDic(j))
println(feature)
val p_full = new Put(new String("stream " + row_full).getBytes())
p_full.add(featuresDic(j).getBytes(), "1".getBytes(), new String(feature).getBytes())
tableInputFeatures.put(p_full)
val p_cat = new Put(new String("stream " + row_categorical).getBytes())
p_cat.add(featuresDic(j).getBytes(), "1".getBytes(), new String(feature).getBytes())
tableCategoricalFilteredFeatures.put(p_cat)
}
j = j + 1
}
}
答案 0 :(得分:0)
我确认哪种方式运行良好,使用hbase-rdd库。 https://github.com/unicredit/hbase-rdd
它易于使用。请参阅https://github.com/unicredit/hbase-rdd#writing-to-hbase了解使用情况。
您可以尝试使用MultiTableOutputFormat,因为我确认它与传统的mapreduce配合得很好。我还没有在Spark中使用它。