简体   繁体   English

过滤twitter4j流

[英]Filter twitter4j stream

I'm trying to filter my twitter4j stream with the following code : 我正在尝试使用以下代码过滤我的twitter4j流:

    TwitterStream twitterStream = getTwitterStreamInstance();

    // Listener
    twitterStream.addListener(listener);

    // Filter
    FilterQuery filtre = new FilterQuery();
    String[] keywordsArray = { "iphone", "samsung" };
    filtre.track(keywordsArray);
    twitterStream.filter(filtre);

    // Listening
    twitterStream.sample();

But the result is, for example : 但结果是,例如:

27/59 - "Taking a risk over something only means that you want it more than anything"
28/63 - The more attractive you are, the more awkward I am.
29/64 - the thing about pain is that it demands to be felt

And I don't recover the keywords I want to follow, where is the problem ? 我没有恢复我想要关注的关键字,问题出在哪里?

You don't need: 你不需要:

twitterStream.sample();

If you remove it, you should start seeing Tweets matching your filter query. 如果删除它,您应该开始看到与您的过滤器查询匹配的推文。

When you invoke filter your listener will receive filtered Tweets, however because you then invoke sample you are effectively replacing the filtered-stream your listener is receiving with the sample steam, which is a random selection Tweets. 当您调用filter您的侦听器将接收已过滤的推文,但是因为您随后调用了sample您实际上正在使用样本流替换侦听器正在接收的过滤流,这是一个随机选择推文。

So in other words call either sample or filter , but not both. 换句话说,调用samplefilter ,但不是两者。

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

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