简体   繁体   English

Flink AsyncDataStream如何传递和使用配置参数

[英]Flink AsyncDataStream how to pass and use configuration parameters

I have this code in my Main() function: 我的Main()函数中有以下代码:

DataStream<OutputObject> asyncResultStream = AsyncDataStream.orderedWait(
            listOfData,
            new CustomAsyncConnector(),
            5,
            TimeUnit.SECONDS,
            10).setParallelism(3).startNewChain().uid("customUid");

Which is the simple format for using AsyncDataStreams in 1.2. 这是在1.2中使用AsyncDataStreams的简单格式。 And the code in the CustomAsyncConnector is just like every example you will find at its core: 而且,CustomAsyncConnector中的代码就像您可以在其核心找到的每个示例一样:

public class CustomAsyncConnector extends RichAsyncFunction<CustomObject, ResultObject> {

private transient Session client;

@Override
public void open(Configuration parameters) throws Exception {
    client = Cluster.builder().addContactPoint("<CustomUrl>")
            .withPort(1234)
            .build()
            .connect("<thisKeyspace>");
}
@Override
public void close() throws Exception {
    client.close();
}
@Override
public void asyncInvoke(final CustomObject ctsa, final AsyncCollector<ResultObject> asyncCollector) throws Exception {

    //Custom code here...

}

} }

Now here are my questions: 1.) What is the proper way to pass "parameters" to the open() function in CustomAsyncConnector() from where it is called in my Main() function. 现在是我的问题:1.)将“参数”传递到CustomAsyncConnector()中的open()函数的正确方法是从Main()函数中调用的地方。 2.) How are the parameters supposed to be used to set up the connection to the client in the open() function? 2.)应该如何在open()函数中使用参数来建立与客户端的连接?

My guess on the first question is to create a new CustomAsyncConnector() object instance in main and then call the open() function directly and pass the parameters object to it and then put that instance in the AsysDataStream's code. 我对第一个问题的猜测是在main中创建一个新的CustomAsyncConnector()对象实例,然后直接调用open()函数并将参数对象传递给它,然后将该实例放入AsysDataStream的代码中。 However I am not sure if this is the best way or, and more importantly, the proper way to set the fields in a Configuration type object (again, assuming that doing "configParameters.setString("contactPointUrl", "127.0.0.1")" is right, but am not sure). 但是,我不确定这是最好的方法还是更重要的是在Configuration类型对象中设置字段的正确方法(同样,假设执行“ configParameters.setString(” contactPointUrl“,” 127.0.0.1“) “是正确的,但不确定)。 And this leads to my second, and honestly most important, question. 这引出了我的第二个,也是最重要的最重要的问题。

So regarding my second question, the parameters I want to pass to the open() function are the contactPointUrl, the portNumber, and the keyspace to be put in .connect(). 因此,关于第二个问题,我想传递给open()函数的参数是contactPointUrl,portNumber和要放入.connect()的键空间。 However I cannot seem to access them by doing something like ".addContactPoint(parameters.getString("contactPointUrl"))". 但是,我似乎无法通过执行“ .addContactPoint(parameters.getString(“ contactPointUrl”))”之类的内容来访问它们。 I also tried seeing if or where I should do Cluster.builder().getConfiguration(parameters) but I am shooting in the dark where that even belongs or if at all and if the parameter names have to be something specific and so on. 我还尝试查看是否或应该在什么地方执行Cluster.builder()。getConfiguration(parameters),但是我在黑暗中拍摄甚至应该属于的地方,或者根本不知道,以及参数名称是否必须是特定的,等等。

So I hope I didn't word that too poorly, but any and all help would be greatly appreciated. 因此,我希望我不要说得太糟,但是所有帮助将不胜感激。

Thanks in advance! 提前致谢!

Here is what ended up working. 这是最终工作的结果。 Still not sure how to pass the configuration parameters to the .open() method, but oh well. 仍然不确定如何将配置参数传递给.open()方法,但是很好。

Added this to the CustomAsyncConnector class: 将此添加到CustomAsyncConnector类:

private final CustomProps props;

public CustomAsyncConnector(CustomProps props) {
    super();
    this.props = props;
}

And what I pass in the main() method: 而我在main()方法中传递的是:

AsyncDataStream
                .unorderedWait(
                        dataToProcess,
                        new CustomAsyncConnector(props),
                        5,
                        TimeUnit.SECONDS,
                        10);

And utilized the props in the .open() method like how I wanted to use the parameters. 并像我想使用参数一样使用.open()方法中的道具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM