comparison src/de/mpiwg/itgroup/nimanager/tools/StoreRDFToVirtuoso.java @ 0:1384a0d382fa

first input
author dwinter
date Thu, 30 Jun 2011 11:44:24 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1384a0d382fa
1 package de.mpiwg.itgroup.nimanager.tools;
2 // see http://docs.openlinksw.com/virtuoso/VirtuosoDriverJDBC.html
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.FileNotFoundException;
7 import java.io.FileReader;
8 import java.io.IOException;
9 import java.io.InputStreamReader;
10 import java.sql.Connection;
11 import java.sql.DriverManager;
12 import java.sql.ResultSet;
13 import java.sql.ResultSetMetaData;
14 import java.sql.SQLException;
15 import java.sql.Statement;
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
18
19 public class StoreRDFToVirtuoso {
20
21 private Statement smt;
22 private Connection con;
23
24
25 public StoreRDFToVirtuoso(String username, String password) throws SQLException{
26 String connectString="jdbc:virtuoso://localhost:1111/charset=UTF-8";
27 con = DriverManager.getConnection(connectString,username,password);
28
29 smt = con.createStatement();
30
31 }
32
33 public void run(String filename) throws IOException, SQLException{
34
35
36 BufferedReader nr = new BufferedReader( new InputStreamReader(new FileInputStream(filename), "UTF8"));
37 String line;
38 String currentRDF="";
39 int counter=0;
40 while ((line = nr.readLine()) != null) {
41 if (line.startsWith("<rdf:RDF")){ // start a new file
42 currentRDF=line;
43 } else if (line.startsWith("</rdf:RDF>")) {
44 currentRDF+=line;
45 try {
46 process(currentRDF);
47 } catch (Exception e) {
48 // TODO Auto-generated catch block
49 e.printStackTrace();
50 }
51 counter+=1;
52 //if (counter>0)
53 // break;
54 } else {
55 currentRDF+=line;
56 }
57
58
59 }
60 }
61
62
63
64 private void process(String currentRDF) throws SQLException {
65
66 if (alreadyExists(currentRDF)){
67 return;
68 }
69 System.out.println("--Process:");
70 currentRDF = currentRDF.replace("'", "''");
71 String cmd = String.format("DB.DBA.RDF_LOAD_RDFXML ('%s', '', 'file:///GND.rdf')", currentRDF);
72 System.out.println(cmd);
73 boolean more = smt.execute(cmd);
74 ResultSetMetaData data = smt.getResultSet().getMetaData();
75 while(more)
76 {
77
78 ResultSet rs = smt.getResultSet();
79 while(rs.next())
80 {
81 for(int i = 1;i <= data.getColumnCount();i++)
82 {
83 String s = rs.getString(i);
84 System.out.println(s);
85 }
86 }
87 more = smt.getMoreResults();
88 }
89
90 System.out.println(currentRDF);
91 System.out.println("--End");
92 con.commit();
93 }
94
95
96 private boolean alreadyExists(String currentRDF) throws SQLException {
97 //String cmdString="sparql select * from <file:///GND.rdf> where {<http://d-nb.info/gnd/100004776> ?x ?y}";
98 // find entries like <rdf:Description rdf:about="http://d-nb.info/gnd/100004776"> in currentRDF
99 Pattern p = Pattern.compile("<rdf:Description rdf:about=\"(.*?)\">", Pattern.MULTILINE | Pattern.DOTALL);
100 Matcher m = p.matcher(currentRDF);
101 m.find();
102 String gndID = m.group(1);
103 //String gndID="XSDGG";
104 String cmdString=String.format("sparql select count(*) from <file:///GND.rdf> where {<%s> ?x ?y}",gndID);
105 smt.execute(cmdString);
106 ResultSet rs = smt.getResultSet();
107 rs.next();
108 int count=rs.getInt(1);
109 if (count>0){
110 System.out.println("Already in:"+gndID);
111 return true;}
112 else
113 return false;
114 }
115
116 static public void main(String args[]) throws IOException, SQLException, ClassNotFoundException{
117 Class.forName("virtuoso.jdbc4.Driver");
118 if (args.length <2){
119 System.out.println("Usage: storeRDF username password");
120 }
121 StoreRDFToVirtuoso st = new StoreRDFToVirtuoso(args[0],args[1]);
122
123 st.run("/Users/dwinter/Documents/Projekte/ECHO-eSciDoc-MPDL/escidocMPIWG/NamedIdentityManager/data/PNDrdf_1.rdf.txt");
124
125 st.close();
126 }
127
128 private void close() throws SQLException {
129 con.close();
130
131 }
132 }