简体   繁体   中英

Flink AsyncDataStream how to pass and use configuration parameters

I have this code in my Main() function:

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. And the code in the CustomAsyncConnector is just like every example you will find at its core:

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. 2.) How are the parameters supposed to be used to set up the connection to the client in the open() function?

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. 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). 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(). However I cannot seem to access them by doing something like ".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.

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.

Added this to the CustomAsyncConnector class:

private final CustomProps props;

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

And what I pass in the main() method:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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