如何在flink流工作中读写HBase

时间:2016-10-26 12:50:59

标签: hadoop apache-flink flink-streaming

如果我们必须在流应用程序中读取和写入HBASE,我们怎么能这样做。我们通过open方法打开连接写入,如何打开连接进行读取。

object test {

    if (args.length != 11) {
      //print args
      System.exit(1)
    }

    val Array() = args
    println("Parameters Passed " + ...);

    val env = StreamExecutionEnvironment.getExecutionEnvironment


    val properties = new Properties()
    properties.setProperty("bootstrap.servers", metadataBrokerList)
    properties.setProperty("zookeeper.connect", zkQuorum)
    properties.setProperty("group.id", group)


    val messageStream = env.addSource(new FlinkKafkaConsumer08[String](topics, new SimpleStringSchema(), properties))

    messageStream.map { x => getheader(x) }





    def getheader(a: String) {

        //Get header and parse and split the headers
                if (metadata not available hit HBASE) { //Device Level send(Just JSON)

            //How to read from HBASE here .

                      } 
                      //If the resultset is not available in Map fetch from phoenix
                      else {
                          //fetch from cache
                      }
     }




    }
   messageStream.writeUsingOutputFormat(new HBaseOutputFormat());
   env.execute()

}

现在在方法getheader内,如果我想从if(metadata not available hit HBASE)里面的HBASE读取,我怎么能这样做。我不想在这里打开连接,我的想法是为一个线程维护一个连接并重用它,就像flink使用open()方法处理HBASE接收器或者如何使用foreachpartition一样。我试过this但我无法将StreamExecutionEnvironment传递给方法。我怎么能实现这一点,有人可以提供一个片段吗?

1 个答案:

答案 0 :(得分:2)

您希望从流用户功能读取/写入Apache HBase。您链接的HBaseReadExample正在执行不同的操作:它将HBase表读入DataSet(Flink的批处理抽象)。在用户函数中使用此代码意味着从Flink程序中启动Flink程序。

对于您的用例,您需要在用户功能中直接创建HBase客户端并与之交互。执行此操作的最佳方法是使用RichFlatMapFunction并在open()方法中创建与HBase的连接。

下一版本的Flink(1.2.0)将在用户功能中支持asynchronous I/O operations,这将显着提高应用程序的吞吐量。