从twitter spout中检索用户的关注者列表

时间:2014-10-10 21:44:37

标签: java twitter apache-storm

我正在尝试检索用户的关注者列表,以便稍后我可以在某些螺栓中使用该列表,但无论我做什么,我都会一直收到以下错误:

16374 [Thread-32-twitterSpout] ERROR backtype.storm.util - Async loop died!
java.lang.NullPointerException: null
    at storm.spout.twitterSpout.nextTuple(twitterSpout.java:136) ~[classes/:na]
    at backtype.storm.daemon.executor$fn__5573$fn__5588$fn__5617.invoke(executor.clj:563) ~[storm-core-0.9.2-incubating.jar:0.9.2-incubating]
    at backtype.storm.util$async_loop$fn__457.invoke(util.clj:431) ~[storm-core-0.9.2-incubating.jar:0.9.2-incubating]
    at clojure.lang.AFn.run(AFn.java:24) [clojure-1.5.1.jar:na]
    at java.lang.Thread.run(Thread.java:662) [na:1.6.0_45]
16375 [Thread-32-twitterSpout] ERROR backtype.storm.daemon.executor - 
java.lang.NullPointerException: null
    at storm.spout.twitterSpout.nextTuple(twitterSpout.java:136) ~[classes/:na]
    at backtype.storm.daemon.executor$fn__5573$fn__5588$fn__5617.invoke(executor.clj:563) ~[storm-core-0.9.2-incubating.jar:0.9.2-incubating]
    at backtype.storm.util$async_loop$fn__457.invoke(util.clj:431) ~[storm-core-0.9.2-incubating.jar:0.9.2-incubating]
    at clojure.lang.AFn.run(AFn.java:24) [clojure-1.5.1.jar:na]
    at java.lang.Thread.run(Thread.java:662) [na:1.6.0_45]
16376 [Thread-34-twitterSpout] ERROR backtype.storm.util - Async loop died!
java.lang.NullPointerException: null
    at storm.spout.twitterSpout.nextTuple(twitterSpout.java:136) ~[classes/:na]
    at backtype.storm.daemon.executor$fn__5573$fn__5588$fn__5617.invoke(executor.clj:563) ~[storm-core-0.9.2-incubating.jar:0.9.2-incubating]
    at backtype.storm.util$async_loop$fn__457.invoke(util.clj:431) ~[storm-core-0.9.2-incubating.jar:0.9.2-incubating]
    at clojure.lang.AFn.run(AFn.java:24) [clojure-1.5.1.jar:na]
    at java.lang.Thread.run(Thread.java:662) [na:1.6.0_45]
16376 [Thread-34-twitterSpout] ERROR backtype.storm.daemon.executor - 
java.lang.NullPointerException: null
    at storm.spout.twitterSpout.nextTuple(twitterSpout.java:136) ~[classes/:na]
    at backtype.storm.daemon.executor$fn__5573$fn__5588$fn__5617.invoke(executor.clj:563) ~[storm-core-0.9.2-incubating.jar:0.9.2-incubating]
    at backtype.storm.util$async_loop$fn__457.invoke(util.clj:431) ~[storm-core-0.9.2-incubating.jar:0.9.2-incubating]
    at clojure.lang.AFn.run(AFn.java:24) [clojure-1.5.1.jar:na]
    at java.lang.Thread.run(Thread.java:662) [na:1.6.0_45]

以下是我上次尝试使用我的spout中的declareOutputFields,open和nextTuple函数时使用的代码:

  @Override
    public void declareOutputFields(OutputFieldsDeclarer declarer){
        declarer.declare(new Fields("status","followers"));
    }

    @Override
    public void open(Map map,TopologyContext context,SpoutOutputCollector collector){
        queue=new LinkedBlockingQueue<Status>(10000);
        this.collector=collector;

        ConfigurationBuilder builder=new ConfigurationBuilder();
        Configuration configuration=builder.setOAuthAccessToken(accessToken).setOAuthAccessTokenSecret(accessTokenSecret).setOAuthConsumerKey(consumerKey).setOAuthConsumerSecret(consumerSecret).build();

        StatusListener listener=new StatusListener() {
            @Override
            public void onStatus(Status status) {
                queue.offer(status);
            }

            @Override
            public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {

            }

            @Override
            public void onTrackLimitationNotice(int numberOfLimitedStatuses) {

            }

            @Override
            public void onScrubGeo(long userId, long upToStatusId) {

            }

            @Override
            public void onStallWarning(StallWarning warning) {

            }

            @Override
            public void onException(Exception ex) {
                      ex.printStackTrace();
            }
        };

        twitterStream=new TwitterStreamFactory(configuration).getInstance();
        twitterStream.addListener(listener);

        Twitter twitter=new TwitterFactory(configuration).getInstance();

        if(filterQuery!=null){
            twitterStream.filter(filterQuery);
        }else{
            twitterStream.sample();
        }

    }

    @Override
    public void nextTuple() {
        Status ret=queue.poll();
        long userID=ret.getUser().getId();
        IDs followersIDs;


        if(ret==null){
            backtype.storm.utils.Utils.sleep(10000);
        }else{

                try{
                    followersIDs=twitter.getFollowersIDs(userID);
                    collector.emit(new Values(ret,followersIDs));
                }catch (TwitterException tex){
                    tex.printStackTrace();
                }
        }
    } 

1 个答案:

答案 0 :(得分:0)

我认为你的问题是

Status ret=queue.poll();
long userID=ret.getUser().getId();

如果retnull,会发生什么?你会在尝试获取用户时获得NPE

你检查是否为null,但为时已晚...... 我建议:

  1. ret
  2. 之后检查null是否为poll
  3. ret之后添加poll跟踪,以便它可以帮助您调试