如果我有一个可以像这样查询的SQL表:
Select empname from mytable
Where empid=1;
等效的SPARQL查询是什么?
答案 0 :(得分:3)
正如这里提到的一些人以及之前类似的问题,您可能需要更多地了解RDF建模数据的性质。
对于Stack Overflow,你肯定需要养成显示输入数据的习惯,以及到目前为止所写的任何代码,即使它不能正常工作。在这种情况下,您需要在问题中包含错误消息。
这是一个自我包含的例子,我
这个例子是用我最强的语言R写的。 要明确:你不需要了解任何关于R的知识就可以擅长RDF,SPARQL和其他语义技术。如果我更擅长Java或Python,我会编写这个例子用其中一种语言。
在使用RDF查询之前,必须将关系(SQL)数据转换为RDF。在下面的代码中,我已经手动完成了。有关螺母和螺栓,请参阅消息的末尾。
library(rrdf)
library(sqldf)
mytable.table.string <- 'empid,empname
1,Arthur
2,Kevin
3,Joey'
mytable <- read.csv(textConnection(mytable.table.string))
print(mytable)
> empid empname
> 1 1 Arthur
> 2 2 Kevin
> 3 3 Joey
sqldf('select empname from mytable where empid = 1')
> empname
> 1 Arthur
# R2RML conversion goes here
# I did it by hand
table.rdf.string <- 'prefix mytable: <http://mytable/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
mytable:1 a mytable:employee .
mytable:1 mytable:empid "1" .
mytable:1 rdfs:label "Arthur" .
mytable:2 a mytable:employee .
mytable:2 mytable:empid "2" .
mytable:2 rdfs:label "Kevin" .
mytable:3 a mytable:employee .
mytable:3 mytable:empid "3" .
mytable:3 rdfs:label "Joey" . '
mytable.rdf <- fromString.rdf(table.rdf.string, format = "TURTLE")
query.as.sparql <- 'prefix mytable: <http://mytable/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ?empname
where
{?emp a mytable:employee .
?emp mytable:empid "1" .
?emp rdfs:label ?empname . }'
sparql.rdf(model = mytable.rdf, sparql = query.as.sparql)
> empname
> [1,] "Arthur"
要查看:上面的这个R代码演示了一个等同于OP的SQL查询的SAPRQL查询。 SQL查询通常针对SQL数据库运行,但我通过针对数据帧运行它来使其自包含。
SPARQL查询针对RDF数据运行,通常以triplestore数据库的形式运行。在我的示例中,包含RDF三元组的字符串将转换为内存中的RDF模型,并将SPARQL查询发送到该模型。
所以问题就变成了,如果你在SQL数据库中有数据,你如何使用SPARQL查询数据库的内容?截至2017年夏季,这是一个不断发展的话题。有许多商业和开源工具可以帮助解决这个问题,通常有以下两种方式之一:
在任何一种情况下,有人都需要创建从SQL数据到RDF格式的映射。这是必需的,因为SQL表本身没有明确的语义。行通常模拟某些类别的个体,列通常表示个人的属性或关系。 RDF三元组必须具有明确的主语 - 动词 - 对象语义。
以下是我建议使用Karma进行SQL-RDF映射的第一步,它具有漂亮的图形界面。使用其他R2RML映射器可以实现类似的结果,例如D2RQ(有效,但未维护3年),ontop或R2RML parser。
44403425.owl
在Karma中打开附加的本体(import
- &gt; from file
)mytable.csv
,用于表示
来自SQL数据库的转储。如果你想现场工作
数据库,您将负责确保正确的数据库
驱动程序已安装。在这种情况下,请使用import
- &gt; Database Table
代替。mytable-model.ttl
。点击下拉菜单
在Karma网页顶部附近的灰色条的左侧。
选择Apply R2RML model
- &gt; from file
并加载mytable_model.ttl
Publish
- &gt; RDF
。接受建议的设置。OpenRDF
。右键点击
该链接在新的浏览器窗口/标签页中打开它。karma_data
存储库。
您现在可以使用左侧的任意Explore
个链接,包括
(SPARQL)Query
链接最后,如果您想使用除Karma之外的R2RML映射器,则可以使用与mytable-model.ttl
类似(但不相同)的映射文件。 Karma使用Python的片段进行一些数据操作,这些片段嵌入在JSON中。 JSON成为km-dev:hasWorksheetHistory
三元组的巨大对象。我不相信所有R2RML解析器都能识别Python / JSON。
<强> 44403425.owl 强>
<?xml version="1.0"?>
<rdf:RDF xmlns="https://stackoverflow.com/questions/44403425.owl/"
xml:base="https://stackoverflow.com/questions/44403425.owl/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:mytable="https://stackoverflow.com/questions/44403425.owl/"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<owl:Ontology rdf:about="https://stackoverflow.com/questions/44403425.owl"/>
<owl:DatatypeProperty rdf:about="https://stackoverflow.com/questions/44403425.owl/empid">
<rdfs:domain rdf:resource="https://stackoverflow.com/questions/44403425.owl/employee"/>
<rdfs:range rdf:resource="http://www.w3.org/2000/01/rdf-schema#Literal"/>
<rdfs:label>employee identifier</rdfs:label>
</owl:DatatypeProperty>
<owl:Class rdf:about="https://stackoverflow.com/questions/44403425.owl/employee">
<owl:equivalentClass>
<owl:Restriction>
<owl:onProperty rdf:resource="https://stackoverflow.com/questions/44403425.owl/empid"/>
<owl:cardinality rdf:datatype="http://www.w3.org/2001/XMLSchema#nonNegativeInteger">1</owl:cardinality>
</owl:Restriction>
</owl:equivalentClass>
<rdfs:label>employee class</rdfs:label>
</owl:Class>
</rdf:RDF>
<强> mytable.csv 强>
"empid","empname"
1,"Arthur"
2,"Kevin"
3,"Joey"
<强> MYTABLE-model.ttl 强>
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix pato: <http://purl.obolibrary.org/obo/pato#> .
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix obo: <http://purl.obolibrary.org/obo/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix oboInOwl: <http://www.geneontology.org/formats/oboInOwl#> .
@prefix protege: <http://protege.stanford.edu/plugins/owl/protege#> .
@prefix turbo: <http://turbo.org/> .
@prefix subsets: <http://purl.obolibrary.org/obo/ro/subsets#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rr: <http://www.w3.org/ns/r2rml#> .
@prefix km-dev: <http://isi.edu/integration/karma/dev#> .
_:node1bi2073ftx1 a km-dev:R2RMLMapping ;
km-dev:sourceName "mytable.csv" ;
km-dev:modelPublicationTime "1496863444477"^^xsd:long ;
km-dev:modelVersion "1.7" ;
km-dev:hasInputColumns "[[{\"columnName\":\"empname\"}],[{\"columnName\":\"empid\"}]]" ;
km-dev:hasOutputColumns "[[{\"columnName\":\"empname\"}],[{\"columnName\":\"empid\"}],[{\"columnName\":\"empid_val\"}]]" ;
km-dev:hasModelLabel "mytable" ;
km-dev:hasBaseURI "https://stackoverflow.com/questions/44403425.owl/" ;
km-dev:hasWorksheetHistory """[
{
\"commandName\": \"SubmitPythonTransformationCommand\",
\"model\": \"new\",
\"inputParameters\": [
{
\"name\": \"hNodeId\",
\"type\": \"hNodeId\",
\"value\": [{\"columnName\": \"empid\"}]
},
{
\"name\": \"worksheetId\",
\"type\": \"worksheetId\",
\"value\": \"W\"
},
{
\"name\": \"selectionName\",
\"type\": \"other\",
\"value\": \"DEFAULT_TEST\"
},
{
\"name\": \"newColumnName\",
\"type\": \"other\",
\"value\": \"empid_val\"
},
{
\"name\": \"transformationCode\",
\"type\": \"other\",
\"value\": \"return getValue(\\\"empid\\\")\"
},
{
\"name\": \"errorDefaultValue\",
\"type\": \"other\",
\"value\": \"\"
},
{
\"name\": \"isJSONOutput\",
\"type\": \"other\",
\"value\": \"false\"
},
{
\"name\": \"inputColumns\",
\"type\": \"hNodeIdList\",
\"value\": \"[{\\\"value\\\":[{\\\"columnName\\\":\\\"empid\\\"}]}]\"
},
{
\"name\": \"outputColumns\",
\"type\": \"hNodeIdList\",
\"value\": \"[{\\\"value\\\":[{\\\"columnName\\\":\\\"empid_val\\\"}]}]\"
}
],
\"tags\": [\"Transformation\"]
},
{
\"commandName\": \"SetWorksheetPropertiesCommand\",
\"model\": \"new\",
\"inputParameters\": [
{
\"name\": \"worksheetId\",
\"type\": \"worksheetId\",
\"value\": \"W\"
},
{
\"name\": \"selectionName\",
\"type\": \"other\",
\"value\": \"DEFAULT_TEST\"
},
{
\"name\": \"properties\",
\"type\": \"other\",
\"value\": {
\"graphLabel\": \"\",
\"hasBaseURI\": false,
\"prefix\": \"mytable\",
\"hasPrefix\": true,
\"hasServiceProperties\": false
}
},
{
\"name\": \"inputColumns\",
\"type\": \"hNodeIdList\",
\"value\": \"[]\"
},
{
\"name\": \"outputColumns\",
\"type\": \"hNodeIdList\",
\"value\": \"[]\"
}
],
\"tags\": [\"Transformation\"]
},
{
\"commandName\": \"SetWorksheetPropertiesCommand\",
\"model\": \"new\",
\"inputParameters\": [
{
\"name\": \"worksheetId\",
\"type\": \"worksheetId\",
\"value\": \"W\"
},
{
\"name\": \"selectionName\",
\"type\": \"other\",
\"value\": \"DEFAULT_TEST\"
},
{
\"name\": \"properties\",
\"type\": \"other\",
\"value\": {
\"graphLabel\": \"\",
\"hasBaseURI\": true,
\"baseURI\": \"https://stackoverflow.com/questions/44403425.owl/\",
\"hasPrefix\": false,
\"hasServiceProperties\": false
}
},
{
\"name\": \"inputColumns\",
\"type\": \"hNodeIdList\",
\"value\": \"[]\"
},
{
\"name\": \"outputColumns\",
\"type\": \"hNodeIdList\",
\"value\": \"[]\"
}
],
\"tags\": [\"Transformation\"]
},
{
\"commandName\": \"SetWorksheetPropertiesCommand\",
\"model\": \"new\",
\"inputParameters\": [
{
\"name\": \"worksheetId\",
\"type\": \"worksheetId\",
\"value\": \"W\"
},
{
\"name\": \"selectionName\",
\"type\": \"other\",
\"value\": \"DEFAULT_TEST\"
},
{
\"name\": \"properties\",
\"type\": \"other\",
\"value\": {
\"graphLabel\": \"mytable\",
\"hasBaseURI\": false,
\"hasPrefix\": false,
\"hasServiceProperties\": false
}
},
{
\"name\": \"inputColumns\",
\"type\": \"hNodeIdList\",
\"value\": \"[]\"
},
{
\"name\": \"outputColumns\",
\"type\": \"hNodeIdList\",
\"value\": \"[]\"
}
],
\"tags\": [\"Transformation\"]
},
{
\"commandName\": \"SetMetaPropertyCommand\",
\"model\": \"new\",
\"inputParameters\": [
{
\"name\": \"hNodeId\",
\"type\": \"hNodeId\",
\"value\": [{\"columnName\": \"empid\"}]
},
{
\"name\": \"worksheetId\",
\"type\": \"worksheetId\",
\"value\": \"W\"
},
{
\"name\": \"selectionName\",
\"type\": \"other\",
\"value\": \"DEFAULT_TEST\"
},
{
\"name\": \"metaPropertyName\",
\"type\": \"other\",
\"value\": \"isUriOfClass\"
},
{
\"name\": \"metaPropertyUri\",
\"type\": \"other\",
\"value\": \"https://stackoverflow.com/questions/44403425.owl/employee\"
},
{
\"name\": \"metaPropertyId\",
\"type\": \"other\",
\"value\": \"https://stackoverflow.com/questions/44403425.owl/employee1\"
},
{
\"name\": \"SemanticTypesArray\",
\"type\": \"other\",
\"value\": [{
\"DomainUri\": \"https://stackoverflow.com/questions/44403425.owl/employee\",
\"DomainId\": \"https://stackoverflow.com/questions/44403425.owl/employee1\",
\"isPrimary\": true,
\"isProvenance\": false,
\"FullType\": \"http://isi.edu/integration/karma/dev#classLink\",
\"DomainLabel\": \"https://stackoverflow.com/questions/44403425.owl/employee/employee1 (add)\"
}]
},
{
\"name\": \"trainAndShowUpdates\",
\"type\": \"other\",
\"value\": true
},
{
\"name\": \"rdfLiteralType\",
\"type\": \"other\",
\"value\": \"\"
},
{
\"name\": \"language\",
\"type\": \"other\",
\"value\": \"\"
},
{
\"name\": \"inputColumns\",
\"type\": \"hNodeIdList\",
\"value\": \"[{\\\"value\\\":[{\\\"columnName\\\":\\\"empid\\\"}]}]\"
},
{
\"name\": \"outputColumns\",
\"type\": \"hNodeIdList\",
\"value\": \"[{\\\"value\\\":[{\\\"columnName\\\":\\\"empid\\\"}]}]\"
}
],
\"tags\": [\"SemanticType\"]
},
{
\"commandName\": \"SetSemanticTypeCommand\",
\"model\": \"new\",
\"inputParameters\": [
{
\"name\": \"hNodeId\",
\"type\": \"hNodeId\",
\"value\": [{\"columnName\": \"empid_val\"}]
},
{
\"name\": \"worksheetId\",
\"type\": \"worksheetId\",
\"value\": \"W\"
},
{
\"name\": \"selectionName\",
\"type\": \"other\",
\"value\": \"DEFAULT_TEST\"
},
{
\"name\": \"SemanticTypesArray\",
\"type\": \"other\",
\"value\": [{
\"DomainUri\": \"https://stackoverflow.com/questions/44403425.owl/employee\",
\"DomainId\": \"https://stackoverflow.com/questions/44403425.owl/employee1\",
\"isPrimary\": true,
\"isProvenance\": false,
\"FullType\": \"https://stackoverflow.com/questions/44403425.owl/empid\",
\"DomainLabel\": \"https://stackoverflow.com/questions/44403425.owl/employee/employee1\"
}]
},
{
\"name\": \"trainAndShowUpdates\",
\"type\": \"other\",
\"value\": true
},
{
\"name\": \"rdfLiteralType\",
\"type\": \"other\",
\"value\": \"\"
},
{
\"name\": \"language\",
\"type\": \"other\",
\"value\": \"\"
},
{
\"name\": \"inputColumns\",
\"type\": \"hNodeIdList\",
\"value\": \"[{\\\"value\\\":[{\\\"columnName\\\":\\\"empid_val\\\"}]}]\"
},
{
\"name\": \"outputColumns\",
\"type\": \"hNodeIdList\",
\"value\": \"[{\\\"value\\\":[{\\\"columnName\\\":\\\"empid_val\\\"}]}]\"
}
],
\"tags\": [\"SemanticType\"]
},
{
\"commandName\": \"SetSemanticTypeCommand\",
\"model\": \"new\",
\"inputParameters\": [
{
\"name\": \"hNodeId\",
\"type\": \"hNodeId\",
\"value\": [{\"columnName\": \"empname\"}]
},
{
\"name\": \"worksheetId\",
\"type\": \"worksheetId\",
\"value\": \"W\"
},
{
\"name\": \"selectionName\",
\"type\": \"other\",
\"value\": \"DEFAULT_TEST\"
},
{
\"name\": \"SemanticTypesArray\",
\"type\": \"other\",
\"value\": [{
\"DomainUri\": \"https://stackoverflow.com/questions/44403425.owl/employee\",
\"DomainId\": \"https://stackoverflow.com/questions/44403425.owl/employee1\",
\"isPrimary\": true,
\"isProvenance\": false,
\"FullType\": \"http://www.w3.org/2000/01/rdf-schema#label\",
\"DomainLabel\": \"https://stackoverflow.com/questions/44403425.owl/employee/employee1\"
}]
},
{
\"name\": \"trainAndShowUpdates\",
\"type\": \"other\",
\"value\": true
},
{
\"name\": \"rdfLiteralType\",
\"type\": \"other\",
\"value\": \"\"
},
{
\"name\": \"language\",
\"type\": \"other\",
\"value\": \"\"
},
{
\"name\": \"inputColumns\",
\"type\": \"hNodeIdList\",
\"value\": \"[{\\\"value\\\":[{\\\"columnName\\\":\\\"empname\\\"}]}]\"
},
{
\"name\": \"outputColumns\",
\"type\": \"hNodeIdList\",
\"value\": \"[{\\\"value\\\":[{\\\"columnName\\\":\\\"empname\\\"}]}]\"
}
],
\"tags\": [\"SemanticType\"]
}
]""" .
km-dev:TriplesMap_d397eb02-1069-4e93-a544-0f2c4d1f8970 a rr:TriplesMap .
_:node1bi2073ftx1 km-dev:hasTriplesMap km-dev:TriplesMap_d397eb02-1069-4e93-a544-0f2c4d1f8970 .
km-dev:TriplesMap_d397eb02-1069-4e93-a544-0f2c4d1f8970 km-dev:isPartOfMapping _:node1bi2073ftx1 .
_:node1bi2073ftx2 rr:tableName "mytable.csv" ;
a rr:LogicalTable ;
km-dev:isPartOfMapping _:node1bi2073ftx1 .
_:node1bi2073ftx1 km-dev:hasLogicalTable _:node1bi2073ftx2 .
km-dev:TriplesMap_d397eb02-1069-4e93-a544-0f2c4d1f8970 rr:logicalTable _:node1bi2073ftx2 ;
rr:subjectMap _:node1bi2073ftx3 .
_:node1bi2073ftx1 km-dev:hasSubjectMap _:node1bi2073ftx3 .
_:node1bi2073ftx3 km-dev:isPartOfMapping _:node1bi2073ftx1 ;
a rr:SubjectMap ;
km-dev:alignmentNodeId "https://stackoverflow.com/questions/44403425.owl/employee1" ;
rr:class <https://stackoverflow.com/questions/44403425.owl/employee> ;
rr:template "{empid}" ;
a km-dev:steinerTreeRootNode .
km-dev:PredicateObjectMap_5a63b356-1bbb-4b1f-b64a-514c19f83d28 rr:predicate <https://stackoverflow.com/questions/44403425.owl/empid> .
_:node1bi2073ftx4 rr:column "empid" ;
a rr:ObjectMap ;
km-dev:isPartOfMapping _:node1bi2073ftx1 .
_:node1bi2073ftx1 km-dev:hasObjectMap _:node1bi2073ftx4 .
km-dev:PredicateObjectMap_5a63b356-1bbb-4b1f-b64a-514c19f83d28 rr:objectMap _:node1bi2073ftx4 .
km-dev:TriplesMap_d397eb02-1069-4e93-a544-0f2c4d1f8970 rr:predicateObjectMap km-dev:PredicateObjectMap_5a63b356-1bbb-4b1f-b64a-514c19f83d28 .
km-dev:PredicateObjectMap_5a63b356-1bbb-4b1f-b64a-514c19f83d28 a rr:PredicateObjectMap ;
km-dev:isPartOfMapping _:node1bi2073ftx1 .
_:node1bi2073ftx1 km-dev:hasPredicateObjectMap km-dev:PredicateObjectMap_5a63b356-1bbb-4b1f-b64a-514c19f83d28 .
km-dev:PredicateObjectMap_028e84af-9ed2-406c-81d0-9add2ed2ca2b rr:predicate rdfs:label .
_:node1bi2073ftx5 rr:column "empname" ;
a rr:ObjectMap ;
km-dev:isPartOfMapping _:node1bi2073ftx1 .
_:node1bi2073ftx1 km-dev:hasObjectMap _:node1bi2073ftx5 .
km-dev:PredicateObjectMap_028e84af-9ed2-406c-81d0-9add2ed2ca2b rr:objectMap _:node1bi2073ftx5 .
km-dev:TriplesMap_d397eb02-1069-4e93-a544-0f2c4d1f8970 rr:predicateObjectMap km-dev:PredicateObjectMap_028e84af-9ed2-406c-81d0-9add2ed2ca2b .
km-dev:PredicateObjectMap_028e84af-9ed2-406c-81d0-9add2ed2ca2b a rr:PredicateObjectMap ;
km-dev:isPartOfMapping _:node1bi2073ftx1 .
_:node1bi2073ftx1 km-dev:hasPredicateObjectMap km-dev:PredicateObjectMap_028e84af-9ed2-406c-81d0-9add2ed2ca2b .