Search in sources :

Example 1 with TwitterSource

use of org.apache.flink.streaming.connectors.twitter.TwitterSource in project flink by apache.

the class TwitterExample method main.

// *************************************************************************
// PROGRAM
// *************************************************************************
public static void main(String[] args) throws Exception {
    // Checking input parameters
    final ParameterTool params = ParameterTool.fromArgs(args);
    System.out.println("Usage: TwitterExample [--output <path>] " + "[--twitter-source.consumerKey <key> --twitter-source.consumerSecret <secret> --twitter-source.token <token> --twitter-source.tokenSecret <tokenSecret>]");
    // set up the execution environment
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    // make parameters available in the web interface
    env.getConfig().setGlobalJobParameters(params);
    env.setParallelism(params.getInt("parallelism", 1));
    // get input data
    DataStream<String> streamSource;
    if (params.has(TwitterSource.CONSUMER_KEY) && params.has(TwitterSource.CONSUMER_SECRET) && params.has(TwitterSource.TOKEN) && params.has(TwitterSource.TOKEN_SECRET)) {
        streamSource = env.addSource(new TwitterSource(params.getProperties()));
    } else {
        System.out.println("Executing TwitterStream example with default props.");
        System.out.println("Use --twitter-source.consumerKey <key> --twitter-source.consumerSecret <secret> " + "--twitter-source.token <token> --twitter-source.tokenSecret <tokenSecret> specify the authentication info.");
        // get default test text data
        streamSource = env.fromElements(TwitterExampleData.TEXTS);
    }
    DataStream<Tuple2<String, Integer>> tweets = streamSource.flatMap(new SelectEnglishAndTokenizeFlatMap()).keyBy(0).sum(1);
    // emit result
    if (params.has("output")) {
        tweets.writeAsText(params.get("output"));
    } else {
        System.out.println("Printing result to stdout. Use --output to specify output path.");
        tweets.print();
    }
    // execute program
    env.execute("Twitter Streaming Example");
}
Also used : ParameterTool(org.apache.flink.api.java.utils.ParameterTool) TwitterSource(org.apache.flink.streaming.connectors.twitter.TwitterSource) Tuple2(org.apache.flink.api.java.tuple.Tuple2) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)

Aggregations

Tuple2 (org.apache.flink.api.java.tuple.Tuple2)1 ParameterTool (org.apache.flink.api.java.utils.ParameterTool)1 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)1 TwitterSource (org.apache.flink.streaming.connectors.twitter.TwitterSource)1