你可以通过以下方式完成此操作RML https://rml.io and FnO https://fno.io.
首先,我们需要访问每一行,这可以通过 RML 来完成。
RML 允许您迭代 CSV 文件的每一行(ql:CSV
)与一个逻辑源 https://rml.io/specs/rml/#logical-source。
指定iterator https://rml.io/specs/rml/#iterator (rml:iterator
)
不需要,因为 RML 中的默认迭代器是基于行的迭代器。
这会产生以下 RDF(Turtle):
<#LogicalSource>
a rml:LogicalSource;
rml:source "data.csv";
rml:referenceFormulation ql:CSV.
实际上三元组是在一个的帮助下生成的三元地图 https://rml.io/specs/rml/#triples-map哪个
使用 LogicalSource 从每个 CSV 行检索数据:
<#MyTriplesMap>
a rr:TriplesMap;
rml:logicalSource <#LogicalSource>;
rr:subjectMap [
rr:template "http://example.org/{col1}";
];
rr:predicateObjectMap [
rr:predicate ex:aProperty;
rr:objectMap <#FunctionMap>;
];
rr:predicateObjectMap [
rr:predicate ex:anotherProperty;
rr:objectMap [
rml:reference "col3";
];
].
The col3
CSV 列用于创建以下三元组:
<http://example.org/A> <http://example.org/ns#anotherProperty> "John".
但是,CSV 列中的字符串col2
需要先拆分。
这可以通过 Fno(功能本体)和 RML 处理器来实现,
支持FnO功能的执行。这样的 RML 处理器可以是RML映射器 https://github.com/RMLio/rmlmapper-java,但其他处理器可以
也可以使用。
需要以下 RDF 来调用 FnO 函数来分割输入
以空格作为分隔符的字符串,并以我们的 LogicalSource 作为输入数据:
<#FunctionMap>
fnml:functionValue [
rml:logicalSource <#LogicalSource>; # our LogicalSource
rr:predicateObjectMap [
rr:predicate fno:executes;
rr:objectMap [
rr:constant grel:string_split # function to use
];
];
rr:predicateObjectMap [
rr:predicate grel:valueParameter;
rr:objectMap [
rml:reference "col2" # input string
];
];
rr:predicateObjectMap [
rr:predicate grel:p_string_sep;
rr:objectMap [
rr:constant " "; # space separator
];
];
].
RML 映射器支持的 FnO 函数可在此处获得:https://rml.io/docs/rmlmapper/default-functions/ https://rml.io/docs/rmlmapper/default-functions/您可以在该页面上找到函数名称及其参数。
映射规则
@base <http://example.org> .
@prefix rml: <http://semweb.mmlab.be/ns/rml#> .
@prefix rr: <http://www.w3.org/ns/r2rml#> .
@prefix ql: <http://semweb.mmlab.be/ns/ql#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix fnml: <http://semweb.mmlab.be/ns/fnml#> .
@prefix fno: <https://w3id.org/function/ontology#> .
@prefix grel: <http://users.ugent.be/~bjdmeest/function/grel.ttl#> .
@prefix ex: <http://example.org/ns#> .
<#LogicalSource>
a rml:LogicalSource;
rml:source "data.csv";
rml:referenceFormulation ql:CSV.
<#MyTriplesMap>
a rr:TriplesMap;
rml:logicalSource <#LogicalSource>;
rr:subjectMap [
rr:template "http://example.org/{col1}";
];
rr:predicateObjectMap [
rr:predicate ex:aProperty;
rr:objectMap <#FunctionMap>;
];
rr:predicateObjectMap [
rr:predicate ex:anotherProperty;
rr:objectMap [
rml:reference "col3";
];
].
<#FunctionMap>
fnml:functionValue [
rml:logicalSource <#LogicalSource>;
rr:predicateObjectMap [
rr:predicate fno:executes;
rr:objectMap [
rr:constant grel:string_split
];
];
rr:predicateObjectMap [
rr:predicate grel:valueParameter;
rr:objectMap [
rml:reference "col2"
];
];
rr:predicateObjectMap [
rr:predicate grel:p_string_sep;
rr:objectMap [
rr:constant " ";
];
];
].
Output
<http://example.org/A> <http://example.org/ns#aProperty> "B".
<http://example.org/A> <http://example.org/ns#aProperty> "C".
<http://example.org/A> <http://example.org/ns#aProperty> "D".
<http://example.org/A> <http://example.org/ns#anotherProperty> "John".
<http://example.org/M> <http://example.org/ns#aProperty> "X".
<http://example.org/M> <http://example.org/ns#aProperty> "Y".
<http://example.org/M> <http://example.org/ns#aProperty> "Z".
<http://example.org/M> <http://example.org/ns#anotherProperty> "Jack".
Note:我为 RML 及其技术做出贡献。