1

I am parsing tweets in java and want to store the data to a DB, however i am looking for a java data structure that can store all the retrieved tweets so that they can be later inserted into the DB tables.

The form i want to have is:

|---user---||---tweet---||---date---|

The only i can think of is making a class Row:

public class Row {

private String user;
private String tweet;
private Date created;

public Row(String user, String tweet, Date created) {
    this.user = user;
    this.tweet = tweet;
    this.created = created;
}

public String getUser() {
    return user;
}

public void setUser(String user) {
    this.user = user;
}

public String getTweet() {
    return tweet;
}

public void setTweet(String tweet) {
    this.tweet = tweet;
}

public Date getCreated() {
    return created;
}

public void setCreated(Date created) {
    this.created = created;
}

}

and instantiate a Row object to store every tweet with the set() methods and use the get() methods to retrieve the data.

Any tips greatly appreciated, switched to java again after some years in python and these tasks seem really not intuitive compared to beloved pandas :)

Shadow
  • 33,525
  • 10
  • 51
  • 64
balalaika
  • 904
  • 4
  • 10
  • 17
  • From what I've read, that seems to encapsulate the basic information you want - if you're looking for a review, it might be better suite to [CodeReview](https://codereview.stackexchange.com) – MadProgrammer Jul 03 '18 at 22:36
  • Sorry, but I didn't get the question. What are you asking? – Andreas Jul 03 '18 at 22:39
  • @MadProgrammer Not sure i understand what you mean with `that seems to encapsulate the basic information you want`. In any case, i am not having a working solution so that it is reviewed but i am requesting for coding & data structure tips to reach a working solution. – balalaika Jul 03 '18 at 22:39
  • @Andreas `i am looking for a java data structure that can store all the retrieved tweets so that they can be later inserted into the DB tables` not clear enough? – balalaika Jul 03 '18 at 22:41
  • Yes, and the data structure you've provided seems to encapsulate your specific requirements – MadProgrammer Jul 03 '18 at 22:41
  • 1
    Your `Row` class stores the values of a row. To make the data "tabular", you need a list of rows, i.e. a `List`. Is that what you're looking for? If so, see [The Java™ Tutorials - Collections](https://docs.oracle.com/javase/tutorial/collections/). – Andreas Jul 03 '18 at 22:41
  • List is a good one, as far as i know List provides deduplication but is it also easy to sort rows and other similar operations? Will update with my implementation to ask further assistance, thanks for the help so far. – balalaika Jul 03 '18 at 22:44
  • 1
    I don't think List provides "deduplication" whatever that is. I think you might be looking for a Map. Put your key in the key field, so that it is unique, and the rest of the data in the value field. – markspace Jul 03 '18 at 22:51
  • 1
    @markspace seems you understood quite well what i meant, still took the time to write a sentence about what you supposedly did not understand, loving the spirit! Thanks a lot for the tip though! – balalaika Jul 03 '18 at 22:54

2 Answers2

2

Use a list with tweet object, then you can add & query from the list to find tweets you need to insert into the DB. Or you can use a map with [key:value]

key=username/email,time Value= string of the tweet

JpersaudCodezit
  • 143
  • 2
  • 13
2

For future reference, this is my solution in combination with the Rowclass define in the original question:

    public static List<Row> searchTweets() throws TwitterException{

    Twitter twitter = GetTwitterInstance();

    Query query = new Query("something");
    QueryResult result = twitter.search(query);
    List<Status> statuses = result.getTweets();
    List<Row> table = new ArrayList<Row>();

    for(Status tweet: statuses){

        Row row = new Row();
        row.setUser(tweet.getUser().getScreenName());
        row.setTweet(tweet.getText());
        row.setCreated(tweet.getCreatedAt());
        table.add(row);
    }
    return table;
}
balalaika
  • 904
  • 4
  • 10
  • 17