Filterquery with streaming API

storm
java

#1

I am using the Twitter Streaming API with Apache Storm to get the tweets and want to filter the tweets for specific keywords. I had a look at some examples i found on the internet and using the same code to filter for the keywords.

I get lots of tweets, unfortunately, the filter doesn’t seem to work at all. Here is the code i am using:

TwitterStreamFactory fact = new TwitterStreamFactory(config.build());

twitterStream = fact.getInstance();

FilterQuery tweetFilterQuery = new FilterQuery();
tweetFilterQuery.track(new String[]{"bitcoin"});

twitterStream.addListener(new TweetListener());
twitterStream.filter(tweetFilterQuery);

twitterStream.sample();

The resulting tweets are then displayed on the console.

I don’t see, why this code does not work. Any hints?

Thanks


#2

No need to call twitterStream.sample() at the end - what ends up happening is you open a filter stream connection with twitterStream.filter() then immediately close it and open up a sample stream. https://github.com/yusuke/twitter4j/blob/master/twitter4j-examples/src/main/java/twitter4j/examples/stream/PrintFilterStream.java#L96

Rather than using just Twitter4J, https://github.com/twitter/hbc is a good library to use for the streaming API - it implements Twitter4J interfaces so you won’t have to change much if you’re using Twitter4J Status objects.

The setup that worked well for me was HBC dumping json into a Kafka queue that Storm can consume.


#3

Commenting out the sample worked. Thanks for your help!