I need to compare SPARQL to SQL(Oracle based).
So I started with Jena and Eclipse. I created my RDF file and queries in SPARQL but when I execute a query in command line it took 5 secondes to get the result and with Eclipse it took 15 secondes !
The difference between the two computation times is huge and I can't compare properly these databases...
Is anyone know why ?
I save my RDF file in RDF/XML mode (by default).
Thanks !
There's the code i use :
String q = "SELECT ?appID ?appLibelle ?droit ?login ?password ?prenom ?nom "
+ "WHERE { "
+ "?empdroitapp EMPDROITAPP:EMP_ID ?emp . "
+ "?empdroitapp EMPDROITAPP:APP_ID ?app . "
+ "?empdroitapp EMPDROITAPP:EMPDroitAPP_Droit ?droit . "
+ "OPTIONAL { ?empapp EMPAPP:EMP_ID ?emp . "
+ "?empapp EMPAPP:APP_ID ?app . "
+ "?empapp EMPAPP:EMPAPP_Login ?login . "
+ "?empapp EMPAPP:EMPAPP_Password ?password . } . "
+ "?emp EMP:EMP_ID ?empID . "
+ "?emp EMP:EMP_Prenom ?prenom . "
+ "?emp EMP:EMP_Nom ?nom . "
+ "?app APP:APP_ID ?appID . "
+ "?app APP:APP_Libelle ?appLibelle . "
+ "} ORDER BY ASC(?empID)";
String prefix = "PREFIX LDAP: <http://example.com/property#>";
String prefixEMP = "PREFIX EMP: <http://example.com/property/emp#>";
String prefixAPP = "PREFIX APP: <http://example.com/property/app#>";
String prefixEMPAPP = "PREFIX EMPAPP: <http://example.com/property/empapp#>";
String prefixEMPDROITAPP = "PREFIX EMPDROITAPP: <http://example.com/property/empdroitapp#>";
String queryString = prefix + NL + prefixEMP + NL + prefixAPP + NL
+ prefixEMPAPP + NL + prefixEMPDROITAPP + q;
Query query = QueryFactory.create(queryString);
//query.serialize(new IndentedWriter(System.out, false));
QueryExecution qexec = QueryExecutionFactory.create(query, m);
long chrono = java.lang.System.currentTimeMillis();
try {
// Assumption: it's a SELECT query.
ResultSet rs = qexec.execSelect();
//String str = "appID\tappLibelle\tdroit\tlogin\tpassword\tprenom\tnom\n";
// The order of results is undefined.
for (; rs.hasNext();) {
QuerySolution rb = rs.nextSolution();
// Get title - variable names do not include the '?' (or '$')
RDFNode appID = rb.get("appID");
RDFNode appLibelle = rb.get("appLibelle");
RDFNode droit = rb.get("droit");
RDFNode login = rb.get("login");
RDFNode password = rb.get("password");
RDFNode prenom = rb.get("prenom");
RDFNode nom = rb.get("nom");
// Check the type of the result value
if (appID.isResource()) {
Resource res = (Resource) appID;
System.out.print("\t" + res);
} else {
System.out.print("\t" + appID);
}
if (appLibelle != null) {
System.out.print("\t" + appLibelle);
} else System.out.print("\tnull");
if (droit != null) {
System.out.print("\t" + droit);
} else System.out.print("\tnull");
if (login != null) {
System.out.print("\t" + login);
} else System.out.print("\tnull");
if (password != null) {
System.out.print("\t" + password);
} else System.out.print("\tnull");
if (prenom != null) {
System.out.print("\t" + prenom);
} else System.out.print("\tnull");
if (nom != null) {
System.out.print("\t" + nom);
} else System.out.print("\tnull");
System.out.println();
//str += "\n";
}
//System.out.println(str);
} finally {
// QueryExecution objects should be closed to free any system
// resources
qexec.close();
}
long tmp = java.lang.System.currentTimeMillis();
System.out.println("Temps écoulé : " + (tmp - chrono) + "ms");
Related
This method is working perfectly as my requirement.But I want to query with DB only once.
I have 2 conditions to check with DB for querying and displaying List data in UI.Each time I am accessing DB and checking list size and performing action.
For one data list with same table I am querying 3 times.Which is not efficient way I think. And wrote sub queries for it.But could not succeed with it.
So any efficient way for writing this and optimizing my method..?
query1: "FROM Produce where produceId not in (:produceIdList) and itemName in (:itemNameList)" + " and farmerUuid not in(" + merchantUuid + ") and lastDateForBid>=CURDATE() order by lastDateForBid Asc";
Condition: If(result>=10) return data
else check for query2
query2: "FROM Produce where produceId not in (:produceIdList) and farmerUuid not in(" + merchantUuid + ")" + "and itemName in (:itemNameList) and categoryId in (:categoryList) and lastDateForBid>=CURDATE() order by lastDateForBid Asc";
Condition: If(result>=10) return data
else check for query3
query3:
"FROM Produce where produceId not in (:produceIdList) and farmerUuid not in(" + merchantUuid + ") and lastDateForBid>=CURDATE() order by lastDateForBid Asc ";
This is my method
mId = merchantUuid;
Long produceId = null;
String itemName = null;
Long categoryId = null;
List<Long> categoryList = new ArrayList<Long>();
List<Long> produceIdList = new ArrayList<Long>();
List<Produce> produceList = new ArrayList<Produce>();
List<String> itemNameList = new ArrayList<String>();
List<Bidding> biddingList = getBiddingForMerchant();
int count = biddingList.size();
if (count > 0) {
for (int i = 0; i < count; i++) {
produceId = biddingList.get(i).getProduce().getProduceId();
if(!produceIdList.contains(produceId)){
produceIdList.add(produceId);
}
itemName = biddingList.get(i).getProduce().getItemName();
if (!itemNameList.contains(itemName)) {
itemNameList.add(itemName);
}
categoryId = biddingList.get(i).getProduce().getCategory().getCategoryId();
if (!categoryList.contains(categoryId)) {
categoryList.add(categoryId);
}
}
String ProduceQuery1 = "FROM Produce where produceId not in (:produceIdList)"
+ " and itemName in (:itemNameList)" + " and farmerUuid not in(" + merchantUuid + ")"
+ " and lastDateForBid>=CURDATE() order by lastDateForBid Asc";
Query q1 = sessionFactory.getCurrentSession().createQuery(ProduceQuery1);
q1.setParameterList("itemNameList", itemNameList);
q1.setParameterList("produceIdList", produceIdList);
//q1.setMaxResults(10);
#SuppressWarnings("unchecked")
List<Produce> produceList1 = q1.list();
produceList.addAll(produceList1);
if (produceList.size() ==10) {
return produceList;
}
else if (produceList.size() < 10) {
String produceQuery2 = "FROM Produce where produceId not in (:produceIdList)"
+ " and farmerUuid not in(" + merchantUuid + ")" + "and itemName in (:itemNameList)"
+ " and categoryId in (:categoryList) "
+ " and lastDateForBid>=CURDATE() order by lastDateForBid Asc";
Query q2 = sessionFactory.getCurrentSession().createQuery(produceQuery2);
System.out.println("produceQuery::" + produceQuery2);
q2.setParameterList("produceIdList", produceIdList);
q2.setParameterList("itemNameList", itemNameList);
q2.setParameterList("categoryList", categoryList);
//q2.setMaxResults(10);
#SuppressWarnings("unchecked")
List<Produce> produceList2 = q2.list();
produceList.clear();
produceList.addAll(produceList2);
if (produceList.size() > 9) {
List<Produce> produceListNew = produceList.subList(0,10);
return produceListNew;
}
else {
String produceQuery3 = "FROM Produce where produceId not in (:produceIdList)"
+ " and farmerUuid not in(" + merchantUuid + ")"
+ " and lastDateForBid>=CURDATE() order by lastDateForBid Asc ";
Query q3 = sessionFactory.getCurrentSession().createQuery(produceQuery3);
q3.setParameterList("produceIdList", produceIdList);
//q3.setMaxResults(10);
#SuppressWarnings("unchecked")
List<Produce> produceList3 = q3.list();
produceList.clear();
produceList.addAll(produceList3);
if(produceList.size()>10){
produceList = produceList.subList(0,10);
}
return produceList;
}
}
}
Thank you..!!
protected void saveData() {
Map<String, String> allStationsParams = new HashMap<>();
List<String> stations = getAllStations();
stmt = Database.getUpdateableStatement();
today = (SysTime.currentTimeMillis() / DasStamp.TICKS_PER_DAY) *
DasStamp.TICKS_PER_DAY;
String changeTimestamp = DasStamp.asCompactString(today);
String keyName = "COM.MAPPINGTOOLTIP." + attributeValue;
for (int row = 0; row < this.getTableModel().getRowCount(); row++) {
String station = (String)this.getTableModel().getValueAt(row, 0);
putInStationParams(this, station, allStationsParams, row);
}
for (String station : stations) {
boolean sendToDB = false;
try (ResultSet rs = this.rsParameters) {
rs.beforeFirst();
while (rs.next()) {
if (rs.getString("station").equals(station)) {
sendToDB = true;
break;
}
}
if (sendToDB) {
if (!rs.getString("value_text").equals(allStationsParams.get(station)) || !allStationsParams.containsKey(station)) {
sendToDB = true;
} else {
sendToDB = false;
}
} else if (allStationsParams.containsKey(station)) {
sendToDB = true;
}
if (sendToDB) {
String sql = "REPLACE INTO dss_parameter (key_name, station, valid_from, value_text"
+ ", change_timestamp) VALUES ('"
+ keyName + "','" + station + "','" + DasStamp.asDateOnlyString(today) + "','"
+ Helper.nz(allStationsParams.get(station)) + "','"
+ changeTimestamp + "') ;";
if (null != stmt) {
stmt.execute(sql);
if (!isResultSetEmpty(rs) && !rs.isAfterLast()) {
AdminLogger.log("dss_parameter", Action.UPDATE,
"key_name='" + keyName + "' and station='" + station + "' and valid_from='" + DasStamp.asDateOnlyString(today) + "'",
"value_text='" + rs.getString("value_text") + "'",
"value_text='" + Helper.nz(allStationsParams.get(station)) + "', change_timestamp='" + changeTimestamp + "'");
} else {
AdminLogger.log("dss_parameter", Action.INSERT,
"key_name='" + keyName + "' and station='" + station + "' and valid_from='" + DasStamp.asDateOnlyString(today) + "'",
"", "value_text='" + Helper.nz(allStationsParams.get(station)) + "', change_timestamp='" + changeTimestamp + "'");
}
}
}
} catch (SQLException e) {
AppFrame.msgBox("Error on insert: " + e.getMessage());
Helper.printMessage(true, false, "Parameter save failed!!", e);
}
}
}
where rsParameters is class level and is fetched before. After first
iteration, rsParameters values is getting null.Is this a problem with try
with resource block? Please help
where rsParameters is class level and is fetched before. After first
iteration, rsParameters values is getting null.Is this a problem with try
with resource block? Please help
Your rsParameters parameter is of Type Resultset.
In first iteration, after try{} block is complete close() method of rsParameters:ResultSet is called.
This internally makes all the properties of resultSet NUll.
That is the reason for getting Null properties during second iteration.
SEE: http://grepcode.com/file/repo1.maven.org/maven2/mysql/mysql-connector-java/5.1.27/com/mysql/jdbc/ResultSetImpl.java#ResultSetImpl.realClose%28boolean%29
hello ive this query in this site working http://dbpedia.org/snorql/
PREFIX ontology: <http://dbpedia.org/ontology/>
SELECT DISTINCT ?author ?name ?wiki ?desc ?thumb WHERE {
?author a ontology:Book;
rdfs:label ?name;
ontology:wikiPageID ?wiki;
ontology:abstract ?desc.
OPTIONAL {?author <http://dbpedia.org/ontology/thumbnail> ?thumb }.
FILTER (lang(?name) = 'en')
FILTER (regex(?name, "lo") || regex(?desc, "lo"))
FILTER (lang(?desc) = 'en').
}ORDER BY ?name LIMIT 100
but in my jena queryFactory java class it only works if i remove Optional filter for thumbnail at this line :
OPTIONAL {?author <http://dbpedia.org/ontology/thumbnail> ?thumb }.
here is my jena java method that works :
private String authorQuery(String entity, String keyWord, String language) {
return addPrefix("rdfs: <http://www.w3.org/2000/01/rdf-schema#>") +
addPrefix("rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>") +
addPrefix("dbpprop: <http://dbpedia.org/property/>") +
addPrefix("ontology: <http://dbpedia.org/ontology/>") +
addQuery("SELECT DISTINCT ?author ?name ?wiki ?desc ?thumb WHERE {\n" +
"?author a ontology:" + entity + ";\n" +
"rdfs:label ?name;\n" +
"ontology:wikiPageID ?wiki;\n" +
"ontology:abstract ?desc;\n" +
"<http://dbpedia.org/ontology/thumbnail> ?thumb.\n" +
"FILTER (lang(?name) = '" + language + "') " +
"FILTER (regex(?name, \"" + keyWord + "\") || regex(?desc, \"" + keyWord + "\"))\n" +
" FILTER (lang(?desc) = '" + language + "')." +
"}\n" +
"ORDER BY ?name\n" +
"LIMIT 40000");
}
but as soon as i add optionnal keyword in this line :
"<http://dbpedia.org/ontology/thumbnail> ?thumb.\n" +
No result are returned, can someone tell me why? :(
PS : it work well without OPTIONAL FILTER
Edit : whole DbpediaQuery class
public class DbPediaQuery {
//array of authors
private DbPediaQuery() {
}
public static DbPediaQuery createDbPediaQuery() {
return new DbPediaQuery();
}
public LinkedList<Entity> queryAuthor(String entity, String keyWord, String language) {
LinkedList<Entity> temp = new LinkedList<>();
Query query = QueryFactory.create(authorQuery(entity, keyWord, language));
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);
try {
temp.addAll(collectAuthors(qexec.execSelect()));
} catch (Exception e) {
e.printStackTrace();
}
return temp;
}
private String authorQuery(String entity, String keyWord, String language) {
return addPrefix("rdfs: <http://www.w3.org/2000/01/rdf-schema#>") +
addPrefix("rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>") +
addPrefix("dbpprop: <http://dbpedia.org/property/>") +
addPrefix("ontology: <http://dbpedia.org/ontology/>") +
addQuery("SELECT DISTINCT ?author ?name ?wiki ?desc ?thumb WHERE {\n" +
"?author a ontology:" + entity + ";\n" +
"rdfs:label ?name;\n" +
"ontology:wikiPageID ?wiki;\n" +
"ontology:abstract ?desc;\n" +
"<http://dbpedia.org/ontology/thumbnail> ?thumb.\n" +
"FILTER (lang(?name) = '" + language + "') " +
"FILTER (regex(?name, \"" + keyWord + "\") || regex(?desc, \"" + keyWord + "\"))\n" +
" FILTER (lang(?desc) = '" + language + "')." +
"}\n" +
"ORDER BY ?name\n" +
"LIMIT 40000");
}
private LinkedList<Entity> collectAuthors(ResultSet results) {
LinkedList<Entity> temp = new LinkedList<>();
while (results.hasNext()) {
Entity a = new Entity();
QuerySolution row = results.next();
String fullName = row.get("name").toString().substring(0, row.get("name").toString().indexOf("#"));
String biography = row.get("desc").toString().substring(0, row.get("desc").toString().indexOf("#"));
a.setTitle(fullName);
a.setWikiID(Integer.parseInt(row.get("wiki").toString().substring(0, row.get("wiki").toString().indexOf("^"))));
if (!row.get("thumb").toString().isEmpty())
a.setPictureURL(row.get("thumb").toString());
else
a.setPictureURL("http://www.google.fr/imgres?imgurl=http%3A%2F%2Fwww.elated.com%2Fres%2FImage%2Farticles%2Fmanagement%2Fapache%2Fmaking-a-custom-error-page%2Fapache_404_default.gif&imgrefurl=https%3A%2F%2Fgithub.com%2Fjdorn%2Fphp-reports%2Fissues%2F43&h=241&w=400&tbnid=KQI5AbkkVp3-uM%3A&zoom=1&docid=6Bd7CTaQ291_UM&ei=5AU0VceoI87WPYOvgCg&tbm=isch&iact=rc&uact=3&dur=3255&page=1&start=0&ndsp=20&ved=0CDYQrQMwBw");
a.setBiography(biography);
temp.add(a);
System.out.println("FAAT" + a.getTitle());
}
return temp;
}
private String addPrefix(String prefix) { return "PREFIX " + prefix + "\n"; }
private String addQuery(String query) { return query; }
}
ok i figured it out after logging several things... First ive loged the resultset size, with the optional filter it gave me more results than if i put no optional filter... 2383 vs 2103(without optional).. then i noticied that if
a.setPictureURL(row.get("thumb2").toString()); is empty or null the loop dont continue... it stoped after 14 results and gave me no return for this method :
private LinkedList<Entity> collectAuthors(ResultSet results) {
LinkedList<Entity> temp = new LinkedList<>();
while (results.hasNext()) {
Entity a = new Entity();
QuerySolution row = results.next();
String fullName = row.get("name").toString().substring(0, row.get("name").toString().indexOf("#"));
String biography = row.get("desc").toString().substring(0, row.get("desc").toString().indexOf("#"));
a.setTitle(fullName);
a.setWikiID(Integer.parseInt(row.get("wiki").toString().substring(0, row.get("wiki").toString().indexOf("^"))));
if (!row.get("thumb").toString().isEmpty())
a.setPictureURL(row.get("thumb").toString());
a.setBiography(biography);
temp.add(a);
System.out.println("FAAT" + a.getTitle());
}
return temp;
}
so i just checked with if(row.getResource("thumb2")!=null) like that
if(row.getResource("thumb")!=null)
a.setPictureURL(row.get("thumb").toString());
and then the loop continued ---> problem solved
Thanks everyone for your hints and be carefull of empty reslutset columns with Jena
I am trying to make a query that returns a simple series of results using JDBC on a java class. The Query only needs 1 join for it to work yet, for some reason, it does not return any values. However, when this query is ran on the Oracle SQL Developer, the correct results are shown. below is the code i am currently using.
To Access Database
query = "select h.house_id, h.house_address, h.house_type, h.status, l.firstname, l.surname, h.price_per_month "
+ "from houses_tbl h join landlord_tbl l on l.landlord_id = h.landlord_id";
conn = connectToDatabase();
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
System.out.println(query);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
To Retrieve Data
response.setContentType("application/json");
fullJsonString = "{";
fullJsonString += "\"houses\":[";
ArrayList<HouseObj> allHouses = new ArrayList<HouseObj>();
try {
while (rs.next()) {
int houseID = rs.getInt(1);
Struct address = (Struct) rs.getObject(2);
Object[] taskAddress = address.getAttributes();
String houseAddressStreet = taskAddress[0].toString();
String houseAddressTown = taskAddress[1].toString();
String houseAddressCounty = taskAddress[2].toString();
String houseAddressCountry = taskAddress[3].toString();
String houseAddressPostcode = taskAddress[4].toString();
String houseFullAddress = houseAddressStreet + ", "
+ houseAddressTown + ", " + houseAddressCounty
+ ", " + houseAddressCountry + ", "
+ houseAddressPostcode;
String type = rs.getString(3);
String status = rs.getString(4);
String landlord = rs.getString(5)+" "+rs.getString(6);
int price = rs.getInt(7);
HouseObj newClient = new HouseObj(houseID,
houseFullAddress, type, status, landlord, price);
allHouses.add(newClient);
}
System.out.println("Number Of Houses : "+allHouses.size());
for (int i = 0; i < allHouses.size(); i++) {
if (i == allHouses.size() - 1) {
fullJsonString += "{\"id\":\""
+ allHouses.get(i).getHouseId() + "\","
+ "\"address\":\""
+ allHouses.get(i).getAddress() + "\","
+ "\"type\":\""
+ allHouses.get(i).getType() + "\","
+ "\"status\":\""
+ allHouses.get(i).getStatus() + "\","
+ "\"landlord\":\""
+ allHouses.get(i).getLandlord() + "\","
+ "\"price\":\""
+ allHouses.get(i).getPrice() + "\"}";
} else {
fullJsonString += "{\"id\":\""
+ allHouses.get(i).getHouseId() + "\","
+ "\"address\":\""
+ allHouses.get(i).getAddress() + "\","
+ "\"type\":\""
+ allHouses.get(i).getType() + "\","
+ "\"status\":\""
+ allHouses.get(i).getStatus() + "\","
+ "\"landlord\":\""
+ allHouses.get(i).getLandlord() + "\","
+ "\"price\":\""
+ allHouses.get(i).getPrice() + "\"},";
}
}
fullJsonString += "]}";
} //Catch Exception Below
Output
Number Of Houses : 0
{"houses":[]}
Any help to resolve this is greatly appreciated.
From your posted code, I don't see any printing of the resultset.
How do you know it's not returning anything?
Can you put a print statement before/after the while (rs.next) cycle (an if, a counter) to see if it' s actually empty?
If it is empty, try removing table alias from your query
EDIT:
Modify the query in "select house_id from houses_tbl", execute the query and exactly after
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
put
if (rs.next()){
System.out.println("Got house!");
}else{
System.out.println("No houses here!");
}
and momentarily comment out/bypass the printing code. This is to check baseline operativity of you env in that context. If this doesn't work, must be a database/driver issue, to me
System.out.println("The U value: " + u);
Statement stmt3 = null;
ResultSet srs3 = null;
List28 = new ArrayList<String>();
stmt3 = conn.createStatement();
String query = "SELECT [USERS_SYS_ID],[GROUPS_SYS_ID] as groupID FROM [USERS_GROUPS] WHERE [USERS_SYS_ID] = " + (u + 1);
srs3 = stmt3.executeQuery(query);
while (srs3.next()) {
List28.add(srs3.getString("groupID"));
}
System.out.println("Group ID: " + List28);
String z = "0";
z = null;
z = ("T_GROUP_" + List28.get(0));
driver.findElement(By.xpath(".//*[#id=z")).click();
System.out.println("Group ID: " + driver.findElement(By.xpath(".//*[#id=z]")));
Given that the value of u is 2 and List28 give a return 2.
Is it possible to run click on the xpath like this
driver.findElement(By.xpath(".//*[#id=z")).click();
Yes, it is possible using the String.format()
String xpathExpression = String.format(".//*[#id='%s']", z);
driver.findElement(By.xpath(xpathExpression)).click();
or simply
driver.findElement(By.xpath(".//*[#id='"+z+"']")).click();