Search in sources :

Example 1 with HosebirdMessageProcessor

use of com.twitter.hbc.core.processor.HosebirdMessageProcessor in project flink by apache.

the class TwitterSource method run.

@Override
public void run(final SourceContext<String> ctx) throws Exception {
    LOG.info("Initializing Twitter Streaming API connection");
    StreamingEndpoint endpoint = initializer.createEndpoint();
    Authentication auth = new OAuth1(properties.getProperty(CONSUMER_KEY), properties.getProperty(CONSUMER_SECRET), properties.getProperty(TOKEN), properties.getProperty(TOKEN_SECRET));
    client = new ClientBuilder().name(properties.getProperty(CLIENT_NAME, "flink-twitter-source")).hosts(properties.getProperty(CLIENT_HOSTS, Constants.STREAM_HOST)).endpoint(endpoint).authentication(auth).processor(new HosebirdMessageProcessor() {

        public DelimitedStreamReader reader;

        @Override
        public void setup(InputStream input) {
            reader = new DelimitedStreamReader(input, Constants.DEFAULT_CHARSET, Integer.parseInt(properties.getProperty(CLIENT_BUFFER_SIZE, "50000")));
        }

        @Override
        public boolean process() throws IOException, InterruptedException {
            String line = reader.readLine();
            ctx.collect(line);
            return true;
        }
    }).build();
    client.connect();
    running = true;
    LOG.info("Twitter Streaming API connection established successfully");
    // just wait now
    while (running) {
        synchronized (waitLock) {
            waitLock.wait(100L);
        }
    }
}
Also used : OAuth1(com.twitter.hbc.httpclient.auth.OAuth1) Authentication(com.twitter.hbc.httpclient.auth.Authentication) InputStream(java.io.InputStream) StreamingEndpoint(com.twitter.hbc.core.endpoint.StreamingEndpoint) DelimitedStreamReader(com.twitter.hbc.common.DelimitedStreamReader) IOException(java.io.IOException) HosebirdMessageProcessor(com.twitter.hbc.core.processor.HosebirdMessageProcessor) ClientBuilder(com.twitter.hbc.ClientBuilder)

Aggregations

ClientBuilder (com.twitter.hbc.ClientBuilder)1 DelimitedStreamReader (com.twitter.hbc.common.DelimitedStreamReader)1 StreamingEndpoint (com.twitter.hbc.core.endpoint.StreamingEndpoint)1 HosebirdMessageProcessor (com.twitter.hbc.core.processor.HosebirdMessageProcessor)1 Authentication (com.twitter.hbc.httpclient.auth.Authentication)1 OAuth1 (com.twitter.hbc.httpclient.auth.OAuth1)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1