消费Kafka数据存入HBase(Scala)

代码如下》》》 

package NB

import org.apache.flink.api.common.functions.RichMapFunction
import org.apache.flink.api.common.serialization.SimpleStringSchema
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer
import org.apache.hadoop.hbase.client.{ConnectionFactory, Put}
import org.apache.hadoop.hbase.util.Bytes
import org.apache.hadoop.hbase.{HBaseConfiguration, TableName}

import java.util.Properties

object text1 {
  def main(args: Array[String]): Unit = {
   val env = StreamExecutionEnvironment.getExecutionEnvironment

   val properties =  new Properties()
   properties.setProperty("bootstrap.servers","192.168.10.102:9092")

   val stream = env.addSource(new FlinkKafkaConsumer[String]("order",new SimpleStringSchema(),properties))

   val dataStream = stream
     .filter(data=>{
       if(data.substring(0,1) == "O"){
         true
       }else{
         false
       }
     })

    val outputStream = dataStream
      .map(data2=>{
        val arr = data2
          .replaceAll("'","")
          .replaceAll("O:","")
          .split(",")
        add(id = arr(0), status = arr(2), price = arr(3), time = arr(4))
      })
      .keyBy(_.id)

    val out= outputStream.map(new RichMapFunction[add,String] {
      override def map(in: add): String = {
        val id = in.id
        val status = in.status
        val price = in.price
        val time = in.time
        //hbase连接配置
        val conf = HBaseConfiguration.create()
        conf.set("hbase.zookeeper.quorum", "master,slave1,slave2")
        conf.set("hbase.zookeeper.property.clientPort", "2181")
        //创建hbase连接
        val connection = ConnectionFactory.createConnection(conf)
        //获取hbase表
        val table = connection.getTable(TableName.valueOf("hainan:WanNing")) //表名

        val put = new Put(Bytes.toBytes(id))//行键
        put.addColumn(
          Bytes.toBytes("name"), //列族
          Bytes.toBytes("id"), //列名
          Bytes.toBytes(id) //数值
        )
        put.addColumn(
          Bytes.toBytes("name"), //列族
          Bytes.toBytes("status"), //列名
          Bytes.toBytes(status) //数值
        )
        put.addColumn(
          Bytes.toBytes("name"), //列族
          Bytes.toBytes("price"), //列名
          Bytes.toBytes(price) //数值
        )
        put.addColumn(
          Bytes.toBytes("name"), //列族
          Bytes.toBytes("time"), //列名
          Bytes.toBytes(time) //数值
        )
        //将数据写入表中
        table.put(put)
        //关闭table
        table.close()
        connection.close()
        id
      }
    })

   out.print("aa")

   env.execute("")

  }
case class add(id:String,status:String,price:String,time:String)
}

验证结果》》》

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的示例代码,通过FlinkKafka数据存入HBase: ``` import org.apache.flink.api.common.functions.MapFunction import org.apache.flink.api.common.serialization.SimpleStringSchema import org.apache.flink.streaming.api.scala._ import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer import org.apache.flink.streaming.connectors.hbase.* import org.apache.hadoop.hbase.client.Put import org.apache.hadoop.hbase.util.Bytes object KafkaToHBase { def main(args: Array[String]): Unit = { val env = StreamExecutionEnvironment.getExecutionEnvironment val kafkaProps = new Properties() kafkaProps.setProperty("bootstrap.servers", "localhost:9092") kafkaProps.setProperty("group.id", "test") val kafkaConsumer = new FlinkKafkaConsumer[String]("topic", new SimpleStringSchema(), kafkaProps) val hbaseProps = new Properties() hbaseProps.setProperty("zookeeper.quorum", "localhost:2181") hbaseProps.setProperty("zookeeper.znode.parent", "/hbase-unsecure") hbaseProps.setProperty("write.buffer.max.size", "20971520") // 20 MB val hbaseOutputFormat = new HBaseOutputFormat(new org.apache.hadoop.hbase.client.ConnectionConfiguration(hbaseProps)) val stream = env .addSource(kafkaConsumer) .map(new MapFunction[String, Put] { val cfBytes = Bytes.toBytes("cf") override def map(value: String): Put = { val rowkey = "some row key" val put = new Put(Bytes.toBytes(rowkey)) put.addColumn(cfBytes, Bytes.toBytes("data"), Bytes.toBytes(value)) put } }) .output(hbaseOutputFormat) env.execute("Kafka to HBase") } } ``` 需要注意的点: 1. 在HBaseOutputFormat实例化时需要传入一个org.apache.hadoop.hbase.client.ConnectionConfiguration对象,用于与HBase进行连接。 2. 在map函数中将Kafka数据转化为HBase Put对象时需要指定一个rowkey。这个rowkey可以按照需要进行设计,例如可以设置成Kafka数据的某个字段。 3. 在map函数中将Kafka数据转化为HBase Put对象时需要指定column family和column qualifier以及对应的value。这里使用了一个名为“cf”的column family和一个名为“data”的column qualifier。如果需要根据业务需要进行更改。 4. HBaseOutputFormat默认是批量写入模式,需要在HBase配置文件中指定write ahead log的大小,以及每次写入的缓冲区大小等。如果需要进行实时写入,则需要将批量写入模式关闭。可以通过在HBase连接配置中设置"HBASE_CLIENT_OPERATION_TIMEOUT"来达到此目的。单位是毫秒,设置为0表示禁用批处理模式。 5. 在实际使用时需要根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值