Example usage for twitter4j Status getText

List of usage examples for twitter4j Status getText

Introduction

In this page you can find the example usage for twitter4j Status getText.

Prototype

String getText();

Source Link

Document

Returns the text of the status

Usage

From source file:org.apache.blur.demo.twitter.Whiteboard.java

License:Apache License

/**
 * @param args//  w  ww. j  a v a  2 s.  c o  m
 * @throws TwitterException
 */
public static void main(String[] args) throws TwitterException {
    Twitter twitter = new TwitterFactory(new ConfigurationBuilder().build()).getInstance();
    OAuth2Token token = twitter.getOAuth2Token();
    System.out.println(token.getTokenType());

    try {
        Query query = new Query("Apache");
        QueryResult result;
        do {
            result = twitter.search(query);
            List<Status> tweets = result.getTweets();
            for (Status tweet : tweets) {
                System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());

            }
        } while ((query = result.nextQuery()) != null);
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to search tweets: " + te.getMessage());
        System.exit(-1);
    }

}

From source file:org.apache.camel.component.twitter.util.TwitterConverter.java

License:Apache License

@Converter
public static String toString(Status status) throws ParseException {
    StringBuilder s = new StringBuilder();
    s.append(status.getCreatedAt()).append(" (").append(status.getUser().getScreenName()).append(") ");
    s.append(status.getText());
    return s.toString();
}

From source file:org.apache.druid.examples.twitter.TwitterSpritzerFirehoseFactory.java

License:Apache License

@Override
public Firehose connect(InputRowParser parser, File temporaryDirectory) {
    final ConnectionLifeCycleListener connectionLifeCycleListener = new ConnectionLifeCycleListener() {
        @Override//from   w  w w.  ja  v  a  2s.c om
        public void onConnect() {
            log.info("Connected_to_Twitter");
        }

        @Override
        public void onDisconnect() {
            log.info("Disconnect_from_Twitter");
        }

        /**
         * called before thread gets cleaned up
         */
        @Override
        public void onCleanUp() {
            log.info("Cleanup_twitter_stream");
        }
    }; // ConnectionLifeCycleListener

    final TwitterStream twitterStream;
    final StatusListener statusListener;
    final int QUEUE_SIZE = 2000;
    /** This queue is used to move twitter events from the twitter4j thread to the druid ingest thread.   */
    final BlockingQueue<Status> queue = new ArrayBlockingQueue<Status>(QUEUE_SIZE);
    final long startMsec = System.currentTimeMillis();

    //
    //   set up Twitter Spritzer
    //
    twitterStream = new TwitterStreamFactory().getInstance();
    twitterStream.addConnectionLifeCycleListener(connectionLifeCycleListener);
    statusListener = new StatusListener() { // This is what really gets called to deliver stuff from twitter4j
        @Override
        public void onStatus(Status status) {
            // time to stop?
            if (Thread.currentThread().isInterrupted()) {
                throw new RuntimeException("Interrupted, time to stop");
            }
            try {
                boolean success = queue.offer(status, 15L, TimeUnit.SECONDS);
                if (!success) {
                    log.warn("queue too slow!");
                }
            } catch (InterruptedException e) {
                throw new RuntimeException("InterruptedException", e);
            }
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            //log.info("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            // This notice will be sent each time a limited stream becomes unlimited.
            // If this number is high and or rapidly increasing, it is an indication that your predicate is too broad, and you should consider a predicate with higher selectivity.
            log.warn("Got track limitation notice:" + numberOfLimitedStatuses);
        }

        @Override
        public void onScrubGeo(long userId, long upToStatusId) {
            //log.info("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
        }

        @Override
        public void onException(Exception ex) {
            log.error(ex, "Got exception");
        }

        @Override
        public void onStallWarning(StallWarning warning) {
            log.warn("Got stall warning: %s", warning);
        }
    };

    twitterStream.addListener(statusListener);
    twitterStream.sample(); // creates a generic StatusStream
    log.info("returned from sample()");

    return new Firehose() {

        private final Runnable doNothingRunnable = new Runnable() {
            @Override
            public void run() {
            }
        };

        private long rowCount = 0L;
        private boolean waitIfmax = (getMaxEventCount() < 0L);
        private final Map<String, Object> theMap = new TreeMap<>();
        // DIY json parsing // private final ObjectMapper omapper = new ObjectMapper();

        private boolean maxTimeReached() {
            if (getMaxRunMinutes() <= 0) {
                return false;
            } else {
                return (System.currentTimeMillis() - startMsec) / 60000L >= getMaxRunMinutes();
            }
        }

        private boolean maxCountReached() {
            return getMaxEventCount() >= 0 && rowCount >= getMaxEventCount();
        }

        @Override
        public boolean hasMore() {
            if (maxCountReached() || maxTimeReached()) {
                return waitIfmax;
            } else {
                return true;
            }
        }

        @Nullable
        @Override
        public InputRow nextRow() {
            // Interrupted to stop?
            if (Thread.currentThread().isInterrupted()) {
                throw new RuntimeException("Interrupted, time to stop");
            }

            // all done?
            if (maxCountReached() || maxTimeReached()) {
                if (waitIfmax) {
                    // sleep a long time instead of terminating
                    try {
                        log.info("reached limit, sleeping a long time...");
                        Thread.sleep(2000000000L);
                    } catch (InterruptedException e) {
                        throw new RuntimeException("InterruptedException", e);
                    }
                } else {
                    // allow this event through, and the next hasMore() call will be false
                }
            }
            if (++rowCount % 1000 == 0) {
                log.info("nextRow() has returned %,d InputRows", rowCount);
            }

            Status status;
            try {
                status = queue.take();
            } catch (InterruptedException e) {
                throw new RuntimeException("InterruptedException", e);
            }

            theMap.clear();

            HashtagEntity[] hts = status.getHashtagEntities();
            String text = status.getText();
            theMap.put("text", (null == text) ? "" : text);
            theMap.put("htags", (hts.length > 0)
                    ? Lists.transform(Arrays.asList(hts), new Function<HashtagEntity, String>() {
                        @Nullable
                        @Override
                        public String apply(HashtagEntity input) {
                            return input.getText();
                        }
                    })
                    : ImmutableList.<String>of());

            long[] lcontrobutors = status.getContributors();
            List<String> contributors = new ArrayList<>();
            for (long contrib : lcontrobutors) {
                contributors.add(StringUtils.format("%d", contrib));
            }
            theMap.put("contributors", contributors);

            GeoLocation geoLocation = status.getGeoLocation();
            if (null != geoLocation) {
                double lat = status.getGeoLocation().getLatitude();
                double lon = status.getGeoLocation().getLongitude();
                theMap.put("lat", lat);
                theMap.put("lon", lon);
            } else {
                theMap.put("lat", null);
                theMap.put("lon", null);
            }

            if (status.getSource() != null) {
                Matcher m = sourcePattern.matcher(status.getSource());
                theMap.put("source", m.find() ? m.group(1) : status.getSource());
            }

            theMap.put("retweet", status.isRetweet());

            if (status.isRetweet()) {
                Status original = status.getRetweetedStatus();
                theMap.put("retweet_count", original.getRetweetCount());

                User originator = original.getUser();
                theMap.put("originator_screen_name", originator != null ? originator.getScreenName() : "");
                theMap.put("originator_follower_count",
                        originator != null ? originator.getFollowersCount() : "");
                theMap.put("originator_friends_count", originator != null ? originator.getFriendsCount() : "");
                theMap.put("originator_verified", originator != null ? originator.isVerified() : "");
            }

            User user = status.getUser();
            final boolean hasUser = (null != user);
            theMap.put("follower_count", hasUser ? user.getFollowersCount() : 0);
            theMap.put("friends_count", hasUser ? user.getFriendsCount() : 0);
            theMap.put("lang", hasUser ? user.getLang() : "");
            theMap.put("utc_offset", hasUser ? user.getUtcOffset() : -1); // resolution in seconds, -1 if not available?
            theMap.put("statuses_count", hasUser ? user.getStatusesCount() : 0);
            theMap.put("user_id", hasUser ? StringUtils.format("%d", user.getId()) : "");
            theMap.put("screen_name", hasUser ? user.getScreenName() : "");
            theMap.put("location", hasUser ? user.getLocation() : "");
            theMap.put("verified", hasUser ? user.isVerified() : "");

            theMap.put("ts", status.getCreatedAt().getTime());

            List<String> dimensions = Lists.newArrayList(theMap.keySet());

            return new MapBasedInputRow(status.getCreatedAt().getTime(), dimensions, theMap);
        }

        @Override
        public Runnable commit() {
            // ephemera in, ephemera out.
            return doNothingRunnable; // reuse the same object each time
        }

        @Override
        public void close() {
            log.info("CLOSE twitterstream");
            twitterStream.shutdown(); // invokes twitterStream.cleanUp()
        }
    };
}

From source file:org.apache.flume.sink.solr.morphline.TwitterSource.java

License:Apache License

public void onStatus(Status status) {
    System.out.println(status.getUser().getName() + " : " + status.getText());
    // TODO: increment rawBytes?
    Record doc = extractRecord("", avroSchema, status);
    if (doc == null) {
        return; // skip
    }//from   w ww.  ja v  a2s  .co m
    docs.add(doc);
    if (docs.size() >= maxBatchSize || System.currentTimeMillis() >= batchEndTime) {
        batchEndTime = System.currentTimeMillis() + maxBatchDurationMillis;
        byte[] bytes;
        try {
            bytes = serializeToAvro(avroSchema, docs);
        } catch (IOException e) {
            LOGGER.error("Exception while serializing tweet", e);
            return; //skip
        }
        Event event = EventBuilder.withBody(bytes);
        getChannelProcessor().processEvent(event); // send event to downstream flume sink
        docs.clear();
    }
    docCount++;
    if ((docCount % REPORT_INTERVAL) == 0) {
        LOGGER.info(String.format("Processed %s docs", numFormatter.format(docCount)));
    }
    if ((docCount % STATS_INTERVAL) == 0) {
        logStats();
    }
}

From source file:org.apache.flume.sink.solr.morphline.TwitterSource.java

License:Apache License

private Record extractRecord(String idPrefix, Schema avroSchema, Status status) {
    User user = status.getUser();/*from   w  w  w .  j  a  va 2  s  .co  m*/
    Record doc = new Record(avroSchema);

    doc.put("id", idPrefix + status.getId());
    doc.put("created_at", formatterTo.format(status.getCreatedAt()));
    doc.put("retweet_count", status.getRetweetCount());
    doc.put("retweeted", status.isRetweet());
    doc.put("in_reply_to_user_id", status.getInReplyToUserId());
    doc.put("in_reply_to_status_id", status.getInReplyToStatusId());

    addString(doc, "source", status.getSource());
    addString(doc, "text", status.getText());

    MediaEntity[] mediaEntities = status.getMediaEntities();
    if (mediaEntities.length > 0) {
        addString(doc, "media_url_https", mediaEntities[0].getMediaURLHttps());
        addString(doc, "expanded_url", mediaEntities[0].getExpandedURL());
    }

    doc.put("user_friends_count", user.getFriendsCount());
    doc.put("user_statuses_count", user.getStatusesCount());
    doc.put("user_followers_count", user.getFollowersCount());
    addString(doc, "user_location", user.getLocation());
    addString(doc, "user_description", user.getDescription());
    addString(doc, "user_screen_name", user.getScreenName());
    addString(doc, "user_name", user.getName());
    return doc;
}

From source file:org.apache.solr.handler.dataimport.TwitterEntityProcessor.java

License:Apache License

@Override
public Map<String, Object> nextRow() {

    Map<String, Object> row = new HashMap<>();

    if (twitter == null || query == null)
        return null;

    try {/*from   w ww . ja  v  a2  s .  c  o m*/
        if (results == null) {
            results = twitter.search(query);
            if (results == null || results.getCount() == 0)
                return null;
        }
        if (tweets == null)
            tweets = (ArrayList<Status>) results.getTweets();

        Status tweet = null;
        if (index < tweets.size()) {
            tweet = (Status) tweets.get(index++);
        } else {
            query = results.nextQuery();
            if (query != null) {
                results = twitter.search(query);
                if (results == null || results.getCount() == 0)
                    return null;
                tweets = (ArrayList<Status>) results.getTweets();
                index = 0;
                tweet = (Status) tweets.get(index++);
            }
        }
        if (tweet == null)
            return null;

        // id
        row.put(MESSAGE_ID, tweet.getId());

        // lang
        row.put(MESSAGE_LANG, tweet.getLang());

        // user
        User user = tweet.getUser();

        // name
        row.put(MESSAGE_USER, user.getName());

        // pseudo
        row.put(MESSAGE_PSEUDO, tweet.getUser().getScreenName());

        // text
        row.put(MESSAGE_TEXT, tweet.getText());

        // date
        Date date = tweet.getCreatedAt();
        row.put(MESSAGE_DATE, date.toString());

    } catch (TwitterException e) {
        e.printStackTrace();
        return null;
    }

    return row;
}

From source file:org.apache.spark.examples.streaming.JavaTwitterHashTagJoinSentiments.java

License:Apache License

public static void main(String[] args) {
    if (args.length < 4) {
        System.err.println("Usage: JavaTwitterHashTagJoinSentiments <consumer key> <consumer secret>"
                + " <access token> <access token secret> [<filters>]");
        System.exit(1);/*w ww.  ja  v a  2s  .  co m*/
    }

    StreamingExamples.setStreamingLogLevels();

    String consumerKey = args[0];
    String consumerSecret = args[1];
    String accessToken = args[2];
    String accessTokenSecret = args[3];
    String[] filters = Arrays.copyOfRange(args, 4, args.length);

    // Set the system properties so that Twitter4j library used by Twitter stream
    // can use them to generate OAuth credentials
    System.setProperty("twitter4j.oauth.consumerKey", consumerKey);
    System.setProperty("twitter4j.oauth.consumerSecret", consumerSecret);
    System.setProperty("twitter4j.oauth.accessToken", accessToken);
    System.setProperty("twitter4j.oauth.accessTokenSecret", accessTokenSecret);

    SparkConf sparkConf = new SparkConf().setAppName("JavaTwitterHashTagJoinSentiments");
    JavaStreamingContext jssc = new JavaStreamingContext(sparkConf, new Duration(2000));
    JavaReceiverInputDStream<Status> stream = TwitterUtils.createStream(jssc, filters);

    JavaDStream<String> words = stream.flatMap(new FlatMapFunction<Status, String>() {
        @Override
        public Iterable<String> call(Status s) {
            return Arrays.asList(s.getText().split(" "));
        }
    });

    JavaDStream<String> hashTags = words.filter(new Function<String, Boolean>() {
        @Override
        public Boolean call(String word) {
            return word.startsWith("#");
        }
    });

    // Read in the word-sentiment list and create a static RDD from it
    String wordSentimentFilePath = "data/streaming/AFINN-111.txt";
    final JavaPairRDD<String, Double> wordSentiments = jssc.sparkContext().textFile(wordSentimentFilePath)
            .mapToPair(new PairFunction<String, String, Double>() {
                @Override
                public Tuple2<String, Double> call(String line) {
                    String[] columns = line.split("\t");
                    return new Tuple2<>(columns[0], Double.parseDouble(columns[1]));
                }
            });

    JavaPairDStream<String, Integer> hashTagCount = hashTags
            .mapToPair(new PairFunction<String, String, Integer>() {
                @Override
                public Tuple2<String, Integer> call(String s) {
                    // leave out the # character
                    return new Tuple2<>(s.substring(1), 1);
                }
            });

    JavaPairDStream<String, Integer> hashTagTotals = hashTagCount
            .reduceByKeyAndWindow(new Function2<Integer, Integer, Integer>() {
                @Override
                public Integer call(Integer a, Integer b) {
                    return a + b;
                }
            }, new Duration(10000));

    // Determine the hash tags with the highest sentiment values by joining the streaming RDD
    // with the static RDD inside the transform() method and then multiplying
    // the frequency of the hash tag by its sentiment value
    JavaPairDStream<String, Tuple2<Double, Integer>> joinedTuples = hashTagTotals.transformToPair(
            new Function<JavaPairRDD<String, Integer>, JavaPairRDD<String, Tuple2<Double, Integer>>>() {
                @Override
                public JavaPairRDD<String, Tuple2<Double, Integer>> call(
                        JavaPairRDD<String, Integer> topicCount) {
                    return wordSentiments.join(topicCount);
                }
            });

    JavaPairDStream<String, Double> topicHappiness = joinedTuples
            .mapToPair(new PairFunction<Tuple2<String, Tuple2<Double, Integer>>, String, Double>() {
                @Override
                public Tuple2<String, Double> call(Tuple2<String, Tuple2<Double, Integer>> topicAndTuplePair) {
                    Tuple2<Double, Integer> happinessAndCount = topicAndTuplePair._2();
                    return new Tuple2<>(topicAndTuplePair._1(),
                            happinessAndCount._1() * happinessAndCount._2());
                }
            });

    JavaPairDStream<Double, String> happinessTopicPairs = topicHappiness
            .mapToPair(new PairFunction<Tuple2<String, Double>, Double, String>() {
                @Override
                public Tuple2<Double, String> call(Tuple2<String, Double> topicHappiness) {
                    return new Tuple2<>(topicHappiness._2(), topicHappiness._1());
                }
            });

    JavaPairDStream<Double, String> happiest10 = happinessTopicPairs
            .transformToPair(new Function<JavaPairRDD<Double, String>, JavaPairRDD<Double, String>>() {
                @Override
                public JavaPairRDD<Double, String> call(JavaPairRDD<Double, String> happinessAndTopics) {
                    return happinessAndTopics.sortByKey(false);
                }
            });

    // Print hash tags with the most positive sentiment values
    happiest10.foreachRDD(new VoidFunction<JavaPairRDD<Double, String>>() {
        @Override
        public void call(JavaPairRDD<Double, String> happinessTopicPairs) {
            List<Tuple2<Double, String>> topList = happinessTopicPairs.take(10);
            System.out.println(String.format("\nHappiest topics in last 10 seconds (%s total):",
                    happinessTopicPairs.count()));
            for (Tuple2<Double, String> pair : topList) {
                System.out.println(String.format("%s (%s happiness)", pair._2(), pair._1()));
            }
        }
    });

    jssc.start();
    jssc.awaitTermination();
}

From source file:org.apache.spark.examples.streaming.twitter.JavaTwitterHashTagJoinSentiments.java

License:Apache License

public static void main(String[] args) {
    if (args.length < 4) {
        System.err.println("Usage: JavaTwitterHashTagJoinSentiments <consumer key>"
                + " <consumer secret> <access token> <access token secret> [<filters>]");
        System.exit(1);/*  w w w .  ja va2  s. c  om*/
    }

    //StreamingExamples.setStreamingLogLevels();
    // Set logging level if log4j not configured (override by adding log4j.properties to classpath)
    if (!Logger.getRootLogger().getAllAppenders().hasMoreElements()) {
        Logger.getRootLogger().setLevel(Level.WARN);
    }

    String consumerKey = args[0];
    String consumerSecret = args[1];
    String accessToken = args[2];
    String accessTokenSecret = args[3];
    String[] filters = Arrays.copyOfRange(args, 4, args.length);

    // Set the system properties so that Twitter4j library used by Twitter stream
    // can use them to generate OAuth credentials
    System.setProperty("twitter4j.oauth.consumerKey", consumerKey);
    System.setProperty("twitter4j.oauth.consumerSecret", consumerSecret);
    System.setProperty("twitter4j.oauth.accessToken", accessToken);
    System.setProperty("twitter4j.oauth.accessTokenSecret", accessTokenSecret);

    SparkConf sparkConf = new SparkConf().setAppName("JavaTwitterHashTagJoinSentiments");

    // check Spark configuration for master URL, set it to local if not configured
    if (!sparkConf.contains("spark.master")) {
        sparkConf.setMaster("local[2]");
    }

    JavaStreamingContext jssc = new JavaStreamingContext(sparkConf, new Duration(2000));
    JavaReceiverInputDStream<Status> stream = TwitterUtils.createStream(jssc, filters);

    JavaDStream<String> words = stream.flatMap(new FlatMapFunction<Status, String>() {
        @Override
        public Iterator<String> call(Status s) {
            return Arrays.asList(s.getText().split(" ")).iterator();
        }
    });

    JavaDStream<String> hashTags = words.filter(new Function<String, Boolean>() {
        @Override
        public Boolean call(String word) {
            return word.startsWith("#");
        }
    });

    // Read in the word-sentiment list and create a static RDD from it
    String wordSentimentFilePath = "streaming-twitter/examples/data/AFINN-111.txt";
    final JavaPairRDD<String, Double> wordSentiments = jssc.sparkContext().textFile(wordSentimentFilePath)
            .mapToPair(new PairFunction<String, String, Double>() {
                @Override
                public Tuple2<String, Double> call(String line) {
                    String[] columns = line.split("\t");
                    return new Tuple2<>(columns[0], Double.parseDouble(columns[1]));
                }
            });

    JavaPairDStream<String, Integer> hashTagCount = hashTags
            .mapToPair(new PairFunction<String, String, Integer>() {
                @Override
                public Tuple2<String, Integer> call(String s) {
                    // leave out the # character
                    return new Tuple2<>(s.substring(1), 1);
                }
            });

    JavaPairDStream<String, Integer> hashTagTotals = hashTagCount
            .reduceByKeyAndWindow(new Function2<Integer, Integer, Integer>() {
                @Override
                public Integer call(Integer a, Integer b) {
                    return a + b;
                }
            }, new Duration(10000));

    // Determine the hash tags with the highest sentiment values by joining the streaming RDD
    // with the static RDD inside the transform() method and then multiplying
    // the frequency of the hash tag by its sentiment value
    JavaPairDStream<String, Tuple2<Double, Integer>> joinedTuples = hashTagTotals.transformToPair(
            new Function<JavaPairRDD<String, Integer>, JavaPairRDD<String, Tuple2<Double, Integer>>>() {
                @Override
                public JavaPairRDD<String, Tuple2<Double, Integer>> call(
                        JavaPairRDD<String, Integer> topicCount) {
                    return wordSentiments.join(topicCount);
                }
            });

    JavaPairDStream<String, Double> topicHappiness = joinedTuples
            .mapToPair(new PairFunction<Tuple2<String, Tuple2<Double, Integer>>, String, Double>() {
                @Override
                public Tuple2<String, Double> call(Tuple2<String, Tuple2<Double, Integer>> topicAndTuplePair) {
                    Tuple2<Double, Integer> happinessAndCount = topicAndTuplePair._2();
                    return new Tuple2<>(topicAndTuplePair._1(),
                            happinessAndCount._1() * happinessAndCount._2());
                }
            });

    JavaPairDStream<Double, String> happinessTopicPairs = topicHappiness
            .mapToPair(new PairFunction<Tuple2<String, Double>, Double, String>() {
                @Override
                public Tuple2<Double, String> call(Tuple2<String, Double> topicHappiness) {
                    return new Tuple2<>(topicHappiness._2(), topicHappiness._1());
                }
            });

    JavaPairDStream<Double, String> happiest10 = happinessTopicPairs
            .transformToPair(new Function<JavaPairRDD<Double, String>, JavaPairRDD<Double, String>>() {
                @Override
                public JavaPairRDD<Double, String> call(JavaPairRDD<Double, String> happinessAndTopics) {
                    return happinessAndTopics.sortByKey(false);
                }
            });

    // Print hash tags with the most positive sentiment values
    happiest10.foreachRDD(new VoidFunction<JavaPairRDD<Double, String>>() {
        @Override
        public void call(JavaPairRDD<Double, String> happinessTopicPairs) {
            List<Tuple2<Double, String>> topList = happinessTopicPairs.take(10);
            System.out.println(String.format("\nHappiest topics in last 10 seconds (%s total):",
                    happinessTopicPairs.count()));
            for (Tuple2<Double, String> pair : topList) {
                System.out.println(String.format("%s (%s happiness)", pair._2(), pair._1()));
            }
        }
    });

    jssc.start();
    try {
        jssc.awaitTermination();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:org.apache.storm.starter.bolt.PrinterBolt.java

License:Apache License

@Override
public void execute(Tuple tuple, BasicOutputCollector collector) {

    File file = new File("Tweet_List.txt");

    Status t = (Status) tuple.getValueByField("tweet");

    try {/*from ww w.j a v a  2 s.c o m*/
        file.createNewFile();
        FileWriter writer = new FileWriter(file, true);
        writer.write(t.getText() + "\n");
        writer.flush();
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    //System.out.println(tuple);
    //System.out.println(count++ + " " + t.getText());
}

From source file:org.apache.storm.starter.bolt.ValueComparator.java

License:Apache License

@Override
public void execute(Tuple tuple, BasicOutputCollector collector) {
    if (tuple.getSourceComponent().compareTo("numGen") == 0) {
        numFlag = 1;//from  w  w w .j a v a 2 s  .  c o  m
        tweetFlag = 0;
        numFriends = tuple.getValue(0);
    } else if (tuple.getSourceComponent().compareTo("hasGen") == 0) {
        hashTag0 = tuple.getValue(0);
        hashTag1 = tuple.getValue(1);
        hashTag2 = tuple.getValue(2);
        hashTag3 = tuple.getValue(3);
        hashTag4 = tuple.getValue(4);
        hashTag5 = tuple.getValue(5);
        hashTag6 = tuple.getValue(6);
        hashTag7 = tuple.getValue(7);
        hashTag8 = tuple.getValue(8);
        hashTag9 = tuple.getValue(9);
        hashFlag = 1;
        tweetFlag = 0;
    } else if ((((tweetFlag == 0) && ((numFlag == 1) && (hashFlag == 1)))
            || ((tweetFlag == 1) && ((numFlag == 0) && (hashFlag == 0))))
            && (tuple.getSourceComponent().compareTo("twitter") == 0)) {

        if ((tweetFlag == 0) && ((numFlag == 1) && (hashFlag == 1))) {
            System.out.println("tweetsize" + collectedtweets.size());
            if (collectedtweets.size() > 0) {
                try {
                    FileWriter fw = new FileWriter("CollectedTweets" + fileno + ".txt", true);
                    fw.write(collectedtweets + "\n");
                    fileno++;
                    fw.close();
                } catch (IOException e) {
                    System.err.println(e);
                }

                collectedtweets.clear();
            }
            if (wordCountsMap.size() > 0) {
                Map sortedMap = sortByValue(wordCountsMap);
                try {
                    Set keyset = sortedMap.keySet();
                    Object[] array = keyset.toArray();
                    FileWriter fw = new FileWriter("TopWords" + fileno1 + ".txt", true);
                    for (int i = 0; i < array.length / 2; i++) {
                        fw.write(array[i] + "\n");
                        fileno1++;
                    }
                    fw.close();

                } catch (IOException e) {
                    System.err.println(e);
                }

                wordCountsMap.clear();
            }
        }
        tweetFlag = 1;
        numFlag = 0;
        hashFlag = 0;

        Status twitterStream = (Status) tuple.getValue(0);
        int friendscount = twitterStream.getUser().getFriendsCount();

        String tweet = twitterStream.getText();

        if (friendscount < (int) numFriends) {

            if ((tweet.contains((String) hashTag9)) || (tweet.contains((String) hashTag8))
                    || (tweet.contains((String) hashTag7)) || (tweet.contains((String) hashTag6))
                    || (tweet.contains((String) hashTag5)) || (tweet.contains((String) hashTag4))
                    || (tweet.contains((String) hashTag0)) || (tweet.contains((String) hashTag1))
                    || (tweet.contains((String) hashTag2)) || (tweet.contains((String) hashTag3))) {

                collectedtweets.add(tweet);
                for (String word : tweet.split("\\s", 0)) {
                    if (!FreeWordList.contains(word)) {
                        Integer count = wordCountsMap.get(word);
                        if (count == null)
                            count = 0;
                        count++;
                        wordCountsMap.put(word, count);
                    }
                }
            }
        }
    }
}