Я не очень опытен во многих вещах, которые я использую здесь.Это для школьного проекта, где мы должны создать программное обеспечение, которое использует семантически структурированные данные.
Я получаю метеорологические данные из API-интерфейса Frost (https://frost.met.no/) в формате json-ld. Я хочу прочитатьэто в модели Jena. Я немного запутался, поддерживает ли jena это или нет.
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
public class Main {
public static void main(String[] args) {
//authentication for the frost api. I have a feeling i shouldn't share this online
String auth = "######";
// I use unirest to make the request to the api
HttpResponse<JsonNode> response = null;
try {
response = Unirest.get("https://frost.met.no/sources/v0.jsonld?country=Norge").
basicAuth(auth, "").
asJson();
} catch (UnirestException e) {
e.printStackTrace();
}
// Get the data as string. Remove context tag as it gives an error:
// "org.apache.jena.riot.RiotException: loading remote context failed: https://frost.met.no/schema"
// I'm assuming this is a problem on the api side. If anyone has any insights feel free to share
String jsonString = response.getBody().toString();
jsonString = jsonString.replace("\"@context\":\"https://frost.met.no/schema\",", "");
// Print the json-ld string. Looks like it should.
System.out.println(jsonString);
//convert json-ld string into InputStream as is required by the read() function.
InputStream targetStream = new ByteArrayInputStream(jsonString.getBytes());
Model model = ModelFactory.createDefaultModel() ;
try {
model.read(targetStream, "", "JSON-LD") ;
} catch (final Exception e){
System.out.println(e.toString());
}
// Write model to console. This seems to output an empty model
model.write(System.out, "JSON-LD");
}
}
Ответ, который я получаю, выглядит примерно так:
{
"@context": "https://frost.met.no/schema",
"@type": "SourceResponse",
"apiVersion": "v0",
"license": "https://creativecommons.org/licenses/by/3.0/no/",
"createdAt": "2019-03-27T14:00:46Z",
"queryTime": 0.534,
"currentItemCount": 1685,
"itemsPerPage": 1685,
"offset": 0,
"totalItemCount": 1685,
"currentLink": "https://frost.met.no//auth//sources/v0.jsonld?country=Norge",
"data": [
{
"@type": "SensorSystem",
"id": "SN100",
"name": "PLASSEN",
"shortName": "Plassen",
"country": "Norge",
"countryCode": "NO",
"geometry": {
"@type": "Point",
"coordinates": [
12.5039,
61.1349
],
"nearest": false
},
Там намного больше, но егопросто больше данных о различных SensorSystems.
Я не получаю ошибок, но модель, которую он выводит, кажется пустой:
{
"@id" : "_:b0",
"@type" : "file:///C:/Users/bm_93/Desktop/Fag/INFO216/SemesterOppgave/SourceResponse"
}
Я делаю это правильно? Поддерживается ли это в jena?
Если нет, могу ли я что-нибудь сделать, чтобы перенести эти данные json в модель jena?