Post

Eclipse rdf4j Repository 생성 및 Schema 추가

1. Repository 생성

img001

img002

img003

2. 온톨로지 스키마 준비

3. 온톨로지 스키마 추가

1) rdf4j Add

img004

2) API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import org.eclipse.rdf4j.RDF4JException;
import org.eclipse.rdf4j.repository.Repository;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.repository.http.HTTPRepository;
import org.eclipse.rdf4j.rio.RDFFormat;

import java.io.File;
import java.io.IOException;

public class AddFile {

	public static void main(String[] args) {
		
		long startTime = System.currentTimeMillis();

		String rdf4jServer = "http://localhost:8080/rdf4j-server/";
		String repositoryID = "WineRepository";
		Repository repo = new HTTPRepository(rdf4jServer, repositoryID);

		RepositoryConnection con = null;

		File file = new File("D://Ontology/wine.rdf");
		String baseURI = "http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#";

		try {
			con = repo.getConnection();
			con.add(file, baseURI, RDFFormat.RDFXML);
		} catch (RDF4JException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			con.close();
			repo.shutDown();
		}
		
		long endTime = System.currentTimeMillis();
		System.out.println("### Run Time : " + (endTime - startTime) / 1000.0 + "sec");
		System.out.println("### Finish");
	}
}

img005

4. 결과 확인

1) rdf4j Query

img006

img007

2) API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import org.eclipse.rdf4j.repository.Repository;
import org.eclipse.rdf4j.repository.http.HTTPRepository;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.query.TupleQuery;
import org.eclipse.rdf4j.query.TupleQueryResult;
import org.eclipse.rdf4j.query.BindingSet;
import org.eclipse.rdf4j.query.QueryLanguage;

public class SelectQuery {

	public static void main(String[] args) {

		String rdf4jServer = "http://localhost:8080/rdf4j-server/";
		String repositoryID = "WineRepository";
		Repository repo = new HTTPRepository(rdf4jServer, repositoryID);

		try (RepositoryConnection con = repo.getConnection()) {
			String queryString = "SELECT ?s ?p ?o WHERE { ?s ?p ?o }";
			TupleQuery tupleQuery = con.prepareTupleQuery(QueryLanguage.SPARQL, queryString);

			try (TupleQueryResult result = tupleQuery.evaluate()) {

				while (result.hasNext()) {
					BindingSet bindingSet = result.next();

					// System.out.println(bindingSet);

					Value s = bindingSet.getValue("s");
					Value p = bindingSet.getValue("p");
					Value o = bindingSet.getValue("o");
					//
					System.out.println("=====");
					System.out.println(s);
					System.out.println(p);
					System.out.println(o);
					System.out.println("=====");
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				con.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			repo.shutDown();
		}
	}
}
This post is licensed under CC BY 4.0 by the author.