Skip to content

Instantly share code, notes, and snippets.

@nlpjoe
Last active December 21, 2019 13:04
Show Gist options
  • Select an option

  • Save nlpjoe/90fdeea00f077f2e0d43b2d8c043836e to your computer and use it in GitHub Desktop.

Select an option

Save nlpjoe/90fdeea00f077f2e0d43b2d8c043836e to your computer and use it in GitHub Desktop.
[virtuoso utils] sql语言 #virtuoso

重要设置

ResultSetMaxRows默认10000,影响返回结果

查看导入状态

select * from DB.DBA.load_list;

导入N3数据

isql 1111 dba dba exec="DB.DBA.TTLP_MT (file_to_string_output ('xxx'), '', 'http://nlpcc2016.org');"

导入数据

ld_dir() 仅从指定目录加载,不包括任何子目录;
ld_dir_all() 从指定目录加载,包括任何和所有子目录;
isql-v 1111 dba dba exec="ld_dir('/path/to/files','*.n3','http://dbpedia.org';"
isql-v 1111 dba dba exec="rdf_loader_run();"

关闭virtuoso

isql-v 1111 dba dba -K

导入完清理load_list

delete from db.dba.load_list;

删除图

SPARQL CLEAR GRAPH <http://test2.com>;

查询graph的命令

select distinct ?g where {graph ?g{?s ?p ?o}}

查询图中facts个数

select distinct ?g COUNT(*) where {graph ?g{?s ?p ?o}}

查询图中单变量unique fact数

SELECT (COUNT(distinct ?p) AS ?count) WHERE { ?s ?p ?o }

?p频次排序

select ?p (count(?p) as ?count) 
where {
  ?s ?p ?o
} order by DESC(?count)

无条件查询

select ?s ?p ?o
from <http://test.com>
where {?s ?p ?o}

已知条件查询(三元组的第一个等于指定值的查询,也可以用filter过滤)

select ?s ?p ?o
from <http://test.com>
where {<http://lod4all.net/resource/corporate/jp/1420001000014/西目屋村役場前> ?p ?o}

前缀查询(需要的命名空间可以在SPARQL语句前预先定义)

PREFIX vcard: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select ?s ?o
from <http://test.com>
where {?s vcard:type ?o} limit 10

字符串匹配,(正则表达式)

select ?s ?o
from <test>
where {?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?o .
			FILTER regex(?s, "前")} limit 10

值匹配(添加过滤条件, filter类似sql里的like)

FILTER (?age >= 24) 

OPTIONAL 使得当某一项不存在时查询不至于失败。 如下查询获取 name 和 age(当age 存在时才获取,如果age 不存在只获取name)。

SELECT ?name ?age
where 
{ 
	?person vcard:FN ?name
    OPTIONAL {?person info:age ?age}
}

嵌套查询

prefix org: <http://www.w3.org/ns/org#>
prefix gn: <http://www.geonames.org/ontology#>
select ?s ?o ?t ?k ?r
from <test>
where {?s org:hasSite ?o .
			?o org:siteAddress ?k .
            ?k gn:countryCode ?r
} limit 10

insertinto 写入数据

PREFIX foaf:<http://xmlns.com/foaf/0.1/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
INSERT
INTO GRAPH <http://test.com>
{
<http://www.example.org/graph/johnsmith#me> 
rdf:Type foaf:Person ;
foaf:name "John Smith" ;
foaf:birthday <1985-01-01T00:00:00> ;
foaf:location "Bristol, UK"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment