OutputStream os;
String[] names = new String[]{"tweet", "screenName", "replyTo", "createdAt", "text"};
File dir = new File("/tmp/sparqljdbc/nativestore");
Sail sail = new NativeStore(dir);
sail.initialize();
try {
Repository repo = new SailRepository(sail);
RepositoryConnection rc = repo.getConnection();
try {
rc.begin();
rc.clear();
rc.commit();
rc.begin();
rc.add(new File("/tmp/twitlogic-dump.nq"), "", RDFFormat.NQUADS);
rc.commit();
rc.begin();
////////////////////////////////////////////////////////////////
query = "PREFIX dc: <http://purl.org/dc/terms/>\n" +
"PREFIX sioc: <http://rdfs.org/sioc/ns#>\n" +
"SELECT DISTINCT ?tweet1 ?tweet2 ?time1 ?time2 WHERE {\n" +
" ?tweet1 dc:created ?time1 .\n" +
" ?tweet2 dc:created ?time2 .\n" +
" ?tweet2 sioc:reply_of ?tweet1 .\n" +
"}";
os = new FileOutputStream("/tmp/replies.csv");
try {
doQuery(query, rc, new PrintStream(os), new CSVOutputter() {
public void output(final BindingSet b,
final StringBuilder sb) {
sb.append(esc(((URI) b.getValue("tweet1")).getLocalName()));
sb.append(", ");
long l1 = ((Literal) b.getValue("time1")).calendarValue().toGregorianCalendar().getTime().getTime();
sb.append(esc("" + l1));
sb.append(", ");
sb.append(esc(((URI) b.getValue("tweet2")).getLocalName()));
sb.append(", ");
long l2 = ((Literal) b.getValue("time2")).calendarValue().toGregorianCalendar().getTime().getTime();
sb.append(esc("" + l2));
}
});
} finally {
os.close();
}
////////////////////////////////////////////////////////////////
query = "PREFIX dc: <http://purl.org/dc/terms/>\n" +
"PREFIX sioc: <http://rdfs.org/sioc/ns#>\n" +
"SELECT DISTINCT ?tweet ?account ?replyTo ?createdAt ?text WHERE {\n" +
" ?tweet dc:created ?createdAt .\n" +
" ?tweet sioc:content ?text .\n" +
" ?tweet sioc:has_creator ?account .\n" +
// " ?account sioc:id ?screenName .\n" +
" OPTIONAL { ?tweet sioc:reply_of ?replyTo . } .\n" +
"}";
os = new FileOutputStream("/tmp/tweets.csv");
try {
doQuery(query, rc, new PrintStream(os), new CSVOutputter() {
public void output(final BindingSet b,
final StringBuilder sb) {
sb.append(esc(((URI) b.getValue("tweet")).getLocalName()));
sb.append(", ");
sb.append(esc(((URI) b.getValue("account")).getLocalName()));
// sb.append(esc(((Literal) b.getValue("screenName")).getLabel()));
sb.append(", ");
Value v = b.getValue("replyTo");
if (null == v) {
sb.append("\"\"");
} else {
sb.append(esc(((URI) v).getLocalName()));
}
sb.append(", ");
sb.append(esc(((Literal) b.getValue("createdAt")).getLabel()));
sb.append(", ");
sb.append(esc(((Literal) b.getValue("text")).getLabel()));
}
});
} finally {
os.close();
}
////////////////////////////////////////////////////////////////
query = "PREFIX sioc: <http://rdfs.org/sioc/ns#>\n" +
"PREFIX sioct: <http://rdfs.org/sioc/types#>\n" +
"SELECT DISTINCT ?tweet ?topic WHERE {\n" +
" ?tweet a sioct:MicroblogPost .\n" +
" ?tweet sioc:topic ?topic .\n" +
"}";
os = new FileOutputStream("/tmp/tweet_topics.csv");
try {
doQuery(query, rc, new PrintStream(os), new CSVOutputter() {
public void output(final BindingSet b,
final StringBuilder sb) {
sb.append(esc(((URI) b.getValue("tweet")).getLocalName()));
sb.append(", ");
sb.append(esc(((URI) b.getValue("topic")).getLocalName()));
}
});
} finally {
os.close();
}
////////////////////////////////////////////////////////////////
query = "PREFIX sioc: <http://rdfs.org/sioc/ns#>\n" +
"PREFIX sioct: <http://rdfs.org/sioc/types#>\n" +
"SELECT DISTINCT ?tweet ?url WHERE {\n" +
" ?tweet a sioct:MicroblogPost .\n" +
" ?tweet sioc:links_to ?url .\n" +
"}";
os = new FileOutputStream("/tmp/tweet_links.csv");
try {
doQuery(query, rc, new PrintStream(os), new CSVOutputter() {
public void output(final BindingSet b,
final StringBuilder sb) {
sb.append(esc(((URI) b.getValue("tweet")).getLocalName()));
sb.append(", ");
sb.append(esc(((URI) b.getValue("url")).stringValue()));
}
});
} finally {
os.close();
}
////////////////////////////////////////////////////////////////
} finally {
rc.rollback();
rc.close();
}
} finally {
sail.shutDown();
}
}