用于存储表格数据的Java数据结构 [英] Java data structure to store tabular data
问题描述
我正在用 Java 解析推文并希望将数据存储到数据库中,但是我正在寻找一种可以存储所有检索到的推文的 Java 数据结构,以便稍后将它们插入到数据库表中.
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.
我想要的表格是:
|---用户---||---推文---||---日期---|
我唯一能想到的就是创建一个类Row
:
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;
}
}
并实例化一个 Row
对象以使用 set()
方法存储每条推文,并使用 get()
方法检索数据.
and instantiate a Row
object to store every tweet with the set()
methods and use the get()
methods to retrieve the data.
非常感谢任何提示,在使用 python 几年后再次切换到 java,与心爱的熊猫相比,这些任务似乎真的不直观:)
Any tips greatly appreciated, switched to java again after some years in python and these tasks seem really not intuitive compared to beloved pandas :)
推荐答案
为了将来参考,这是我结合原始问题中定义的 Row
类的解决方案:
For future reference, this is my solution in combination with the Row
class 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;
}
这篇关于用于存储表格数据的Java数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!