converters - 对于 Apache Jena 输入 : Conversion from CSV to RDF Format

标签 converters rdf jena rdfs apache-jena

我将使用 Apache Jena,它采用 RDF 作为输入格式。但我有 CSV 格式的数据。我研究了很多,但找不到转换的方法。有谁知道如何有效地做到这一点。

我已经使用过 xml123 等工具,但下载链接不起作用。

最佳答案

使用 jena-arq 和 jena-csv(均为 v3.0.1),以下方法对我有用:

public static void main(String ... strings) throws Exception {
    CSV2RDF.init();
    //load through manager:
    //Model m = RDFDataMgr.loadModel("test.csv") ;
    //classic way to load:
    Model m = ModelFactory.createDefaultModel();
    try (InputStream in = JenaCSVTest.class.getResourceAsStream("/test.csv")) {
        m.read(in, "http://example.com", "csv");
    }
    m.setNsPrefix("test", "http://example.com#");
    m.write(System.out, "ttl");
}

输入(test.csv):

Town,Population
Southton,123000
Northville,654000

输出(海龟中的rdf):

@prefix test:  <http://example.com#> .

[ test:Population  "123000"^^<http://www.w3.org/2001/XMLSchema#double> ;
  test:Town        "Southton" ;
  <http://w3c/future-csv-vocab/row>
          1
] .

[ test:Population  "654000"^^<http://www.w3.org/2001/XMLSchema#double> ;
  test:Town        "Northville" ;
  <http://w3c/future-csv-vocab/row>
          2
] .

参见官方文档 jena-csv

更新:

jena-3.10.0 开始,jena-csv 已停用。 最后的 jena-csv 版本是 3.9.0 。 相反,您可以使用任何其他 csv2rdf 转换器。 例如,tarql .

com.github.tarql:tarql 版本 v1.2 的快速演示示例(通过 jitpack.io 获得 - 似乎没有 maven-central 版本):

    Path file = Paths.get(JenaCSVTest.class.getResource("/test.csv").toURI());
    String base = "http://example.com#";
    Model m = ModelFactory.createDefaultModel().setNsPrefix("xsd", XSD.getURI()).setNsPrefix("test", base);
    Graph g = m.getGraph();
    CSVOptions op = new CSVOptions();
    op.setDefaultsForCSV();
    String query = "PREFIX test: <" + base + ">\n" +
            "PREFIX xsd: <" + XSD.getURI() + ">\n" +
            "CONSTRUCT {\n" +
            "  ?Row a test:Row;\n" +
            "    test:town ?town;\n" +
            "    test:population ?population;\n" +
            "} \n" +
            "WHERE {\n" +
            "  BIND (BNODE() AS ?Row)\n" +
            "  BIND (xsd:string(?Town) AS ?town)\n" +
            "  BIND (xsd:integer(?Population) AS ?population)\n" +
            "}";
    TarqlQuery q = new TarqlQuery(QueryFactory.create(query));
    InputStreamSource src = InputStreamSource.fromFilenameOrIRI(file.toUri().toString());
    TarqlQueryExecution qe = TarqlQueryExecutionFactory.create(q, src, op);
    qe.execTriples().forEachRemaining(g::add);
    m.write(System.out, "ttl");

此代码片段将生成以下 RDF:

@prefix test:  <http://example.com#> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .

[ a                test:Row ;
  test:population  123000 ;
  test:town        "Southton"
] .

[ a                test:Row ;
  test:population  654000 ;
  test:town        "Northville"
] .

关于converters - 对于 Apache Jena 输入 : Conversion from CSV to RDF Format,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44717689/

相关文章:

将十六进制数转换为十进制数

java - 在 Joda-Time 上将mediumDate() 转换为其他格式(带区域设置)

c++ - 使用Raptor RDF Parser Toolkit生成FOAF rdfxml文件

java - 如何提取rdf :about or rdf:ID properties from triples using SPARQL?

java - TextDatasetFactory 有问题

c# - 如果通过反射设置 View 模型属性,则 WPF 绑定(bind)不起作用

python - 在 python 中使用括号从表示二叉树的字符串创建数组

logic - 巴拉克不喜欢唐纳德喜欢的任何东西

java - 如何在 Jena TDB 数据集中保留命名空间前缀?

java - SPARQL 在 Java 中并发查询?