Reputation: 289
I am using jena API and I am running a SELECT query which is like this-
SELECT ?stream ?numberOfStudents
where {
?stream inst:hasNumber ?numberOfStudents
}
This is returning a ResultSet where one column is 'stream' and the other one is 'numberOfStudents'. Now I am trying to convert this to a map using the following code-
public static getResultAsMap(ResultSet rs){
Map<String, Integer> myMap = new HashMap<String, Integer>();
int column1Pos = rs.findColumn("stream");
int column2Pos = rs.findColumn("numberOfStudents");
while (rs.next()) {
String column1 = rs.getString(column1Pos);
int column2 = rs.getInt(column2Pos);
myMap.put(column1, column2);
}
But this is getting an error saying the I cannot use the findColumn method. Is there any way of achieving my goal of getting a map from the resultset. If this looks absolutely wrong can some one suggest me a better approach to achieve the goal of getting a map from the resultset.
Upvotes: 0
Views: 870
Reputation: 26
This is an alternative way to convert a sparql ResultSet to a Map in your case.
1.Include the following imports:
import java.util.HashMap;
import java.util.Map;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
2.Use QuerySolution class for conversion:
public Map<String, Integer> getResultAsMap(ResultSet rs) {
Map<String, Integer> myMap = new HashMap<String, Integer>();
for (; rs.hasNext();) {
QuerySolution soln = rs.nextSolution();
String stream = soln.get("stream").toString();
String noOfStudentsStr = soln.get("numberOfStudents").toString();
int noOfStudents = Integer.parseInt(noOfStudentsStr);
myMap.put(stream, noOfStudents);
}
return myMap;
}
Upvotes: 1