spark streaming hbase错误

xwbd5t1u  于 2021-06-09  发布在  Hbase
关注(0)|答案(1)|浏览(341)

我想将流数据插入 hbase ; 这是我的密码:

val tableName = "streamingz"
val conf = HBaseConfiguration.create()
conf.addResource(new Path("file:///opt/cloudera/parcels/CDH-5.4.7-1.cdh5.4.7.p0.3/etc/hbase/conf.dist/hbase-site.xml"))
conf.set(TableInputFormat.INPUT_TABLE, tableName)

val admin = new HBaseAdmin(conf)
if (!admin.isTableAvailable(tableName)) {
    print("-----------------------------------------------------------------------------------------------------------")
    val tableDesc = new HTableDescriptor(tableName)
    tableDesc.addFamily(new HColumnDescriptor("z1".getBytes()))
    tableDesc.addFamily(new HColumnDescriptor("z2".getBytes()))
    admin.createTable(tableDesc)
} else {
    print("Table already exists!!--------------------------------------------------------------------------------------")
}
val ssc = new StreamingContext(sc, Seconds(10))
val topicSet = Set("fluxAstellia")
val kafkaParams = Map[String, String]("metadata.broker.list" - > "10.32.201.90:9092")
val stream = KafkaUtils.createDirectStream[String, String, StringDecoder, StringDecoder](ssc, kafkaParams, topicSet)
val lines = stream.map(_._2).map(_.split(" ", -1)).foreachRDD(rdd => {
    if (!rdd.partitions.isEmpty) {
        val myTable = new HTable(conf, tableName)
        rdd.map(rec => {
            var put = new Put(rec._1.getBytes)
            put.add("z1".getBytes(), "name".getBytes(), Bytes.toBytes(rec._2))
            myTable.put(put)
        }).saveAsNewAPIHadoopDataset(conf)
        myTable.flushCommits()
    } else {
        println("rdd is empty")
    }

})

ssc.start()
ssc.awaitTermination()

}
}

我有个错误:

:66: error: value _1 is not a member of Array[String]
       var put = new Put(rec._1.getBytes)

我是初学者,所以我怎么不能纠正这个错误,我有一个问题:
在何处创建表;在流媒体流程之外还是内部?
谢谢您

bvuwiixz

bvuwiixz1#

你的错误基本上是在线的 var put = new Put(rec._1.getBytes) 只能在Map(键为1,值为2)或元组上调用\u n。 rec 是通过将流中的字符串按空格字符拆分得到的字符串数组。如果你在第一个元素后面,你会把它写成 var put = new Put(rec(0).getBytes) . 同样地,在下一行中,你会把它写成 put.add("z1".getBytes(), "name".getBytes(), Bytes.toBytes(rec(1)))

相关问题