|
| 1 | +package arq.examples; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.apache.jena.atlas.iterator.Iter; |
| 6 | +import org.apache.jena.query.Dataset; |
| 7 | +import org.apache.jena.query.DatasetFactory; |
| 8 | +import org.apache.jena.rdf.model.Model; |
| 9 | +import org.apache.jena.rdf.model.RDFNode; |
| 10 | +import org.apache.jena.rdf.model.Resource; |
| 11 | +import org.apache.jena.rdf.model.Statement; |
| 12 | +import org.apache.jena.rdflink.RDFLinkHTTP; |
| 13 | +import org.apache.jena.rdflink.dataset.DatasetGraphOverRDFLink; |
| 14 | +import org.apache.jena.vocabulary.RDFS; |
| 15 | + |
| 16 | +/** |
| 17 | + * Example that uses the {@link Resource} API over an HTTP-backed DatasetGraph. |
| 18 | + * Note, that each access fires an HTTP request. Hence, this pattern is only |
| 19 | + * meaningful lightweight infrequent lookups. |
| 20 | + * In general, it is recommended to first create local in-memory snapshots of |
| 21 | + * a remote endpoint's data using e.g. CONSTRUCT queries first. |
| 22 | + */ |
| 23 | +public class ExampleDBpediaViaRemoteDataset2 { |
| 24 | + public static void main(String... args) { |
| 25 | + Dataset ds = DatasetFactory.wrap(new DatasetGraphOverRDFLink(() -> |
| 26 | + RDFLinkHTTP.newBuilder() |
| 27 | + .destination("https://dbpedia.org/sparql") |
| 28 | + .build())); |
| 29 | + |
| 30 | + Model model = ds.getDefaultModel(); |
| 31 | + Resource r = model.getResource("http://dbpedia.org/resource/Apache_Jena"); |
| 32 | + List<RDFNode> list = Iter.toList(r.listProperties(RDFS.label).mapWith(Statement::getObject)); |
| 33 | + int n = list.size(); |
| 34 | + System.out.println("Got " + n + " labels from DBpedia:"); |
| 35 | + for (int i = 0; i < n; ++i) { |
| 36 | + System.out.println((i + 1) + ": " + list.get(i)); |
| 37 | + } |
| 38 | + |
| 39 | + /* Output is expected to be similar to: |
| 40 | + Got 5 labels from DBpedia: |
| 41 | + 1: "Apache Jena"@en |
| 42 | + 2: "Jena (Framework)"@de |
| 43 | + 3: "Jena (framework)"@fr |
| 44 | + 4: "Jena (informatica)"@it |
| 45 | + 5: "아파치 제나"@ko |
| 46 | + */ |
| 47 | + } |
| 48 | +} |
0 commit comments