Example usage for twitter4j Status getGeoLocation

List of usage examples for twitter4j Status getGeoLocation

Introduction

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

Prototype

GeoLocation getGeoLocation();

Source Link

Document

Returns The location that this tweet refers to if available.

Usage

From source file:info.maslowis.twitterripper.util.Util.java

License:Open Source License

/**
 * Returns a representation the status (tweet) as string
 *
 * @return a representation the status as string in format <em>Status{id=, text='', lang='', createdAt=, geoLocation=, isFavorited=, isRetweeted=, retweetCount=, isTruncated=}</em>
 *//*  w ww .j a  v a 2s .  c  o m*/
public static String toString(final Status status) {
    return "Status{id=" + status.getId() + ", text='" + status.getText() + "', lang='" + status.getLang()
            + "', createdAt=" + status.getCreatedAt() + ", geoLocation=" + status.getGeoLocation()
            + ", isFavorited=" + status.isFavorited() + ", isRetweeted=" + status.isRetweeted()
            + ", retweetCount=" + status.getRetweetCount() + ", isTruncated=" + status.isTruncated() + "}";
}

From source file:io.druid.examples.twitter.TwitterSpritzerFirehoseFactory.java

License:Apache License

@Override
public Firehose connect(InputRowParser parser) throws IOException {
    final ConnectionLifeCycleListener connectionLifeCycleListener = new ConnectionLifeCycleListener() {
        @Override/* w  ww.  j  a  v  a2  s.  com*/
        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) {
            ex.printStackTrace();
        }

        @Override
        public void onStallWarning(StallWarning warning) {
            System.out.println("Got stall warning:" + warning);
        }
    };

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

    return new Firehose() {

        private final Runnable doNothingRunnable = new Runnable() {
            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;
            }
        }

        @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...");
                        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(String.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 ? String.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() throws IOException {
            log.info("CLOSE twitterstream");
            twitterStream.shutdown(); // invokes twitterStream.cleanUp()
        }
    };
}

From source file:io.rakam.datasource.twitter.TweetProcessor.java

License:Apache License

@Override
public void onStatus(Status status) {
    Map<String, Object> map = new HashMap<>();

    GeoLocation geoLocation = status.getGeoLocation();
    if (geoLocation != null) {
        map.put("latitude", geoLocation.getLatitude());
        map.put("longitude", geoLocation.getLongitude());
    }//from  w ww  .j ava2s.  c o m

    map.put("_time", status.getCreatedAt().getTime());
    Place place = status.getPlace();
    if (place != null) {
        map.put("country_code", place.getCountryCode());
        map.put("place", place.getName());
        map.put("place_type", place.getPlaceType());
        map.put("place_id", place.getId());
    }

    User user = status.getUser();
    map.put("_user", user.getId());
    map.put("user_lang", user.getLang());
    map.put("user_created", user.getCreatedAt());
    map.put("user_followers", user.getFollowersCount());
    map.put("user_status_count", user.getStatusesCount());
    map.put("user_verified", user.isVerified());

    map.put("id", status.getId());
    map.put("is_reply", status.getInReplyToUserId() > -1);
    map.put("is_retweet", status.isRetweet());
    map.put("has_media", status.getMediaEntities().length > 0);
    map.put("urls",
            Arrays.stream(status.getURLEntities()).map(URLEntity::getText).collect(Collectors.toList()));
    map.put("hashtags", Arrays.stream(status.getHashtagEntities()).map(HashtagEntity::getText)
            .collect(Collectors.toList()));
    map.put("user_mentions", Arrays.stream(status.getUserMentionEntities()).map(UserMentionEntity::getText)
            .collect(Collectors.toList()));
    map.put("language", "und".equals(status.getLang()) ? null : status.getLang());
    map.put("is_positive", classifier.isPositive(status.getText()));

    Event event = new Event().properties(map).collection(collection);
    buffer.add(event);

    commitIfNecessary();
}

From source file:mapper.TweetDataMapper.java

/**
* Transform a {@link Status} into an {@link Tweet}.
*
* @param status Object to be transformed.
* @return {@link Tweet}./*from  ww  w.  j  av a2 s .c o m*/
*/
@Override
public Tweet transform(Status status) {
    if (status == null) {
        throw new IllegalArgumentException("Cannot transform a null value");
    }
    Tweet tweet = new Tweet();
    tweet.setCreateAt(status.getCreatedAt());
    tweet.setLang(status.getLang());
    if (status.getGeoLocation() != null) {
        tweet.setLat(status.getGeoLocation().getLatitude());
        tweet.setLon(status.getGeoLocation().getLongitude());
    }
    tweet.setReTweetCount(status.getRetweetCount());
    tweet.setText(status.getText());
    return tweet;

}

From source file:my.twittergui.TwitterUI.java

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    String text;//from w w  w.j  a  va 2s. c o  m
    text = jTextField1.getText();
    //jTextArea1.append(text+"\n");
    jTextArea1.append("Searching for: ");
    String[] strarray = text.split(" ");
    for (int i = 0; i < strarray.length; i++) {
        jTextArea1.append(strarray[i] + "\n");
    }

    File filed = null;
    filed = new File("C:\\Results");
    if (!filed.exists()) {
        filed.mkdir();
    }

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("").setOAuthConsumerSecret("").setOAuthAccessToken("")
            .setOAuthAccessTokenSecret("");

    StatusListener listener = new StatusListener() {

        public void onStatus(Status status) {

            try {

                int fileno = 0;
                String strI = Integer.toString(fileno);
                String fname = "C:\\results\\DataCol" + strI + ".csv";
                File file = new File(fname);
                FileWriter bw = new FileWriter(file, true);

                if (file.length() == 0) {
                    bw.write("\"Screen Name\", text, \"created at\", geolocation, Retweet? \r\n");
                    bw.flush();
                }
                while (file.length() > 10485760) {
                    fileno += 1;
                    strI = Integer.toString(fileno);
                    fname = "DataCol" + strI + ".txt";
                    file = new File(fname);
                    bw = new FileWriter(file, true);
                    bw.write("\"Screen Name\", \"text\", \"created_at\", \"geolocation\" \r\n");
                    bw.flush();

                }

                // if(!status.isRetweet()){
                bw.write("\r\n");
                bw.write("\"" + status.getUser().getScreenName() + "\",");
                String tweettxt = status.getText();
                tweettxt = tweettxt.replace("\n", "");
                tweettxt = tweettxt.replace(",", "");
                tweettxt = tweettxt.replace("\"", "");
                bw.write("\"" + tweettxt + "\",");

                bw.write("\"" + status.getCreatedAt() + "\",");
                if (status.getGeoLocation() != null) {
                    bw.write("\"" + status.getGeoLocation() + "\"");
                } else
                    bw.write("N/A,");
                if (status.isRetweet())
                    bw.write("Yes");
                else
                    bw.write("No");

                bw.flush();

                bw.close();
                //System.out.print("\n");
                String str = "@" + status.getUser().getScreenName() + "  " + status.getText() + " "
                        + status.getCreatedAt() + " ";
                if (status.getGeoLocation() != null)
                    str += status.getGeoLocation();
                //System.out.print(str);
                jTextArea1.append(str + "\n");
                // }

                bw.close();
            } catch (IOException e) {
                //System.out.print("EXCEPTION");
                e.printStackTrace();
            }

        }

        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
        }

        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
        }

        public void onStallWarning(StallWarning stall) {
        }

        public void onScrubGeo(long a, long b) {
        }

        public void onException(Exception ex) {
            ex.printStackTrace();
        }
    };
    TwitterStreamFactory tf = new TwitterStreamFactory(cb.build());
    TwitterStream twitterStream = tf.getInstance();
    twitterStream.addListener(listener);
    // Filter
    FilterQuery filtre = new FilterQuery();
    Scanner in = new Scanner(System.in);

    filtre.track(strarray);
    twitterStream.filter(filtre);

}

From source file:nl.b3p.viewer.stripes.TwitterActionBean.java

License:Open Source License

public Resolution create() throws JSONException {
    JSONObject json = new JSONObject();

    json.put("success", Boolean.FALSE);
    String error = null;// w  w w  . j a va2s . com

    try {
        // The factory instance is re-useable and thread safe.
        Twitter twitter = new TwitterFactory().getInstance();
        Query query = new Query(term);
        if (latestId != null) {
            Long longVal = Long.valueOf(latestId);
            query.setSinceId(longVal);
        }

        QueryResult result = twitter.search(query);
        JSONArray tweets = new JSONArray();
        for (Status tweet : result.getTweets()) {

            //System.out.println(tweet.getFromUser() + ":" + tweet.getText());
            JSONObject t = new JSONObject();
            t.put("id_str", String.valueOf(tweet.getId()));
            t.put("text", tweet.getText());
            t.put("user_from", tweet.getUser().getScreenName());
            t.put("img_url", tweet.getUser().getProfileImageURL());

            JSONObject geo = new JSONObject();
            if (tweet.getGeoLocation() != null) {
                geo.put("lat", tweet.getGeoLocation().getLatitude());
                geo.put("lon", tweet.getGeoLocation().getLongitude());
            }
            t.put("geo", geo);
            tweets.put(t);
        }
        json.put("tweets", tweets);
        if (tweets.length() > 0) {
            json.put("maxId", String.valueOf(result.getMaxId()));
        } else {
            json.put("maxId", String.valueOf(latestId));
        }
        json.put("success", Boolean.TRUE);
    } catch (Exception e) {

        error = e.toString();
        if (e.getCause() != null) {
            error += "; cause: " + e.getCause().toString();
        }
    }

    if (error != null) {
        json.put("error", error);
    }

    return new StreamingResolution("application/json", new StringReader(json.toString()));
}

From source file:nl.isaac.dotcms.twitter.pojo.CustomStatus.java

License:Creative Commons License

public CustomStatus(Status status) {
    this.createdAt = status.getCreatedAt();
    this.id = status.getId();
    this.id_str = String.valueOf(status.getId());
    this.text = status.getText();
    this.source = status.getSource();
    this.isTruncated = status.isTruncated();
    this.inReplyToStatusId = status.getInReplyToStatusId();
    this.inReplyToUserId = status.getInReplyToUserId();
    this.isFavorited = status.isFavorited();
    this.inReplyToScreenName = status.getInReplyToScreenName();
    this.geoLocation = status.getGeoLocation();
    this.place = status.getPlace();
    this.retweetCount = status.getRetweetCount();
    this.isPossiblySensitive = status.isPossiblySensitive();

    this.contributorsIDs = status.getContributors();

    this.retweetedStatus = status.getRetweetedStatus();
    this.userMentionEntities = status.getUserMentionEntities();
    this.urlEntities = status.getURLEntities();
    this.hashtagEntities = status.getHashtagEntities();
    this.mediaEntities = status.getMediaEntities();
    this.currentUserRetweetId = status.getCurrentUserRetweetId();

    this.isRetweet = status.isRetweet();
    this.isRetweetedByMe = status.isRetweetedByMe();

    this.rateLimitStatus = status.getRateLimitStatus();
    this.accessLevel = status.getAccessLevel();
    this.user = status.getUser();
}

From source file:nlptexthatespeechdetection.NLPTextHateSpeechDetection.java

/**
 * @param args the command line arguments
 *//*from   ww  w. j a  va 2  s.  co  m*/
public static void main(String[] args) throws TwitterException, NotDirectoryException, IOException {
    HateSpeechClassifier1 classifier = new HateSpeechClassifier1();
    AnnotatedDataFolder data = new AnnotatedDataFolder("data");
    boolean overSampling = false;
    classifier.train(data.getDateSortedLabeledData(overSampling));

    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    StatusListener listener = new StatusListener() {
        int numHateSpeech = 0;
        int numTweets = 0;

        @Override
        public void onStatus(Status status) {
            if (status.getLang().equals("in")) {
                numTweets++;
                if (classifier.isHateSpeech(status.getText(), 0.5)) {
                    System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText() + " * "
                            + status.getId() + " # " + status.getLang() + " $ "
                            + (status.getGeoLocation() == null ? "NULLGEO"
                                    : status.getGeoLocation().toString()));
                    System.out.println();
                    System.out.println("lang: " + status.getLang());
                    System.out.println("number of detected hate speech: " + numHateSpeech);
                    System.out.println("total number of streamed tweets: " + numTweets);
                    System.out.println();
                    System.out.println();
                    numHateSpeech++;
                }
            } else {
                System.out.println("ignoring non-Indonesian tweet");
            }
            //                if (status.getGeoLocation() != null) {
            //                    System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText() + " * " + status.getId() + " $ " + status.getGeoLocation().toString());
            //                }
            //                if (status.getLang().equals("id")) {
            //                    System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText() + " * " + status.getId() + " # " + status.getLang() + " $ " + (status.getGeoLocation() == null ? "NULLGEO" : status.getGeoLocation().toString()));
            //                }
        }

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

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
        }

        @Override
        public void onScrubGeo(long userId, long upToStatusId) {
            System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
        }

        @Override
        public void onStallWarning(StallWarning warning) {
            System.out.println("Got stall warning:" + warning);
        }

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

    twitterStream.addListener(listener);

    FilterQuery filterQuery = new FilterQuery();
    filterQuery.track(new String[] { "a", "i", "u", "e", "o" });
    filterQuery.language("in");
    twitterStream.filter(filterQuery);

    twitterStream.sample();
}

From source file:nyu.twitter.lg.FentchTwitter.java

License:Open Source License

public static void invoke() throws Exception {
    init();/* w w  w  . jav a 2  s . com*/
    // Create table if it does not exist yet
    if (Tables.doesTableExist(dynamoDB, tableName)) {
        System.out.println("Table " + tableName + " is already ACTIVE");
    } else {
        // Create a table with a primary hash key named 'name', which holds
        // a string
        CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName)
                .withKeySchema(new KeySchemaElement().withAttributeName("id").withKeyType(KeyType.HASH))
                .withAttributeDefinitions(new AttributeDefinition().withAttributeName("id")
                        .withAttributeType(ScalarAttributeType.S))
                .withProvisionedThroughput(
                        new ProvisionedThroughput().withReadCapacityUnits(1L).withWriteCapacityUnits(1L));
        TableDescription createdTableDescription = dynamoDB.createTable(createTableRequest)
                .getTableDescription();
        System.out.println("Created Table: " + createdTableDescription);
        // Wait for it to become active
        System.out.println("Waiting for " + tableName + " to become ACTIVE...");
        Tables.waitForTableToBecomeActive(dynamoDB, tableName);
    }

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("Emwo2pG").setOAuthConsumerSecret("RM9B7fske5T")
            .setOAuthAccessToken("19ubQOirq").setOAuthAccessTokenSecret("Lbg3C");

    final TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();

    StatusListener listener = new StatusListener() {

        @Override
        public void onStatus(Status status) {

            if (status.getGeoLocation() != null && status.getPlace() != null) {

                //               if (count == 0) {
                //                  count++;
                //               }
                //               
                latitude = status.getGeoLocation().getLatitude();
                longtitude = status.getGeoLocation().getLongitude();
                place = status.getPlace().getCountry() + "," + status.getPlace().getFullName();
                date = status.getCreatedAt().toString();
                id = Integer.toString(count);
                name = status.getUser().getScreenName();
                message = status.getText();
                System.out.println("---------------------------");
                System.out.println("ID:" + count);
                System.out.println("latitude:" + latitude);
                System.out.println("longtitude:" + longtitude);
                System.out.println("place:" + place);
                System.out.println("name:" + name);
                System.out.println("message:" + message);
                System.out.println("data:" + date);
                System.out.println("-------------8-------------");

                insertDB(id, count, name, longtitude, latitude, place, message, date);

                if (++count > 100) {
                    twitterStream.shutdown();
                    System.out.println("Information Collection Completed");
                }
                //      count = (count+1) % 101;
            }
        }

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

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            //            System.out.println("Got track limitation notice:"
            //                  + numberOfLimitedStatuses);
        }

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

        @Override
        public void onStallWarning(StallWarning warning) {
            //            System.out.println("Got stall warning:" + warning);
        }

        @Override
        public void onException(Exception ex) {
            ex.printStackTrace();
        }
    };
    twitterStream.addListener(listener);
    twitterStream.sample();
}

From source file:org.apache.asterix.external.parser.TweetParser.java

License:Apache License

@Override
public void parse(IRawRecord<? extends Status> record, DataOutput out) throws HyracksDataException {
    Status tweet = record.get();
    User user = tweet.getUser();/*from   ww w  .  j a v a  2s.com*/
    // Tweet user data
    ((AMutableString) mutableUserFields[userFieldNameMap.get(Tweet.SCREEN_NAME)])
            .setValue(JObjectUtil.getNormalizedString(user.getScreenName()));
    ((AMutableString) mutableUserFields[userFieldNameMap.get(Tweet.LANGUAGE)])
            .setValue(JObjectUtil.getNormalizedString(user.getLang()));
    ((AMutableInt32) mutableUserFields[userFieldNameMap.get(Tweet.FRIENDS_COUNT)])
            .setValue(user.getFriendsCount());
    ((AMutableInt32) mutableUserFields[userFieldNameMap.get(Tweet.STATUS_COUNT)])
            .setValue(user.getStatusesCount());
    ((AMutableString) mutableUserFields[userFieldNameMap.get(Tweet.NAME)])
            .setValue(JObjectUtil.getNormalizedString(user.getName()));
    ((AMutableInt32) mutableUserFields[userFieldNameMap.get(Tweet.FOLLOWERS_COUNT)])
            .setValue(user.getFollowersCount());

    // Tweet data
    ((AMutableString) mutableTweetFields[tweetFieldNameMap.get(Tweet.ID)])
            .setValue(String.valueOf(tweet.getId()));

    int userPos = tweetFieldNameMap.get(Tweet.USER);
    for (int i = 0; i < mutableUserFields.length; i++) {
        ((AMutableRecord) mutableTweetFields[userPos]).setValueAtPos(i, mutableUserFields[i]);
    }
    if (tweet.getGeoLocation() != null) {
        ((AMutableDouble) mutableTweetFields[tweetFieldNameMap.get(Tweet.LATITUDE)])
                .setValue(tweet.getGeoLocation().getLatitude());
        ((AMutableDouble) mutableTweetFields[tweetFieldNameMap.get(Tweet.LONGITUDE)])
                .setValue(tweet.getGeoLocation().getLongitude());
    } else {
        ((AMutableDouble) mutableTweetFields[tweetFieldNameMap.get(Tweet.LATITUDE)]).setValue(0);
        ((AMutableDouble) mutableTweetFields[tweetFieldNameMap.get(Tweet.LONGITUDE)]).setValue(0);
    }
    ((AMutableString) mutableTweetFields[tweetFieldNameMap.get(Tweet.CREATED_AT)])
            .setValue(JObjectUtil.getNormalizedString(tweet.getCreatedAt().toString()));
    ((AMutableString) mutableTweetFields[tweetFieldNameMap.get(Tweet.MESSAGE)])
            .setValue(JObjectUtil.getNormalizedString(tweet.getText()));

    for (int i = 0; i < mutableTweetFields.length; i++) {
        mutableRecord.setValueAtPos(i, mutableTweetFields[i]);
    }
    recordBuilder.reset(mutableRecord.getType());
    recordBuilder.init();
    IDataParser.writeRecord(mutableRecord, out, recordBuilder);
}