SPARQL查询语句详解(不懂的可以楼下评论,看到秒回)

SPARQL

  • 简单家庭关联网络(知识图谱)

  • 用rdf描述这个网络

    <rdf:RDF
    	xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
    	xmlns:info='http://zy.example.com/info#'
    	xmlns:rel='http://zy.example.com/rel#'>
    
    	<rdf:Description rdf:about="http://zy.example.com/person/Tom">
    		<info:name>Tom</info:name>
    		<info:job>worker</info:job>
    		<info:age rdf:datatype='http://www.w3.org/2001/XMLSchema#integer'>56
            </info:age>
    		<rel:fatherof rdf:resource="http://zy.example.com/person/Jim"/>
    	</rdf:Description>
    
    	<rdf:Description rdf:about="http://zy.example.com/person/Jim">
    		<info:name>Jim</info:name>
    		<info:job>programmer</info:job>
    		<info:age rdf:datatype='http://www.w3.org/2001/XMLSchema#integer'>28
            </info:age>
    		<rel:fatherof rdf:resource="http://zy.example.com/person/Cherry"/>
    	</rdf:Description>
    
    	<rdf:Description rdf:about="http://zy.example.com/person/Cherry">
    		<info:name>Cherry</info:name>
    		<info:age rdf:datatype='http://www.w3.org/2001/XMLSchema#integer'>8
            </info:age>
    	</rdf:Description>
    </rdf:RDF>
    
  • 简单查询

    查询语句

    prefix info: <http://zy.example.com/info#>
    prefix rel: <http://zy.example.com/rel#>
    
    select ?person ?age
    where{
    	?person info:age ?age
    }
    

    查询结果

    -----------------------------------------------
    | person                                | age |
    ===============================================
    | <http://zy.example.com/person/Cherry> | 8   |
    | <http://zy.example.com/person/Tom>    | 56  |
    | <http://zy.example.com/person/Jim>    | 28  |
    -----------------------------------------------
    
  • 过滤查询(FILTER, OPTIONAL, ORDERBY)

    查询语句

    prefix info: <http://zy.example.com/info#>
    prefix rel: <http://zy.example.com/rel#>
    
    select ?name ?age ?job
    where{
    	?person info:name ?name.
    	?person info:age ?age.
    	filter(?age <= 30).
    	optional{?person info:job ?job}.
    }
    orderby(?age)
    

    查询结果

    ---------------------------------
    | name     | age | job          |
    =================================
    | "Cherry" | 8   |              |
    | "Jim"    | 28  | "programmer" |
    ---------------------------------
    
  • 聚合函数(COUNT, SUM, MIN, MAX, AVG)

    查询语句

    prefix info: <http://zy.example.com/info#>
    prefix rel: <http://zy.example.com/rel#>
    
    select (AVG(?age) as ?AvgAge)
    where{
    	?person info:age ?age
    }
    

    查询结果

    -------------------------------
    | AvgAge                      |
    ===============================
    | 30.666666666666666666666666 |
    -------------------------------
    
  • 联合查询(UNION)

    查询语句

    prefix info: <http://zy.example.com/info#>
    prefix rel: <http://zy.example.com/rel#>
    
    select ?name ?age ?job
    where{
    	{
    		?person info:name ?name.
    		optional{?person info:job ?job}.
    		?person info:age ?age.
    		filter(?age >= 30)
    	}
    	union
    	{
    		?person info:name ?name.
    		optional{?person info:job ?job}.
    		?person info:age ?age.
    		filter(?age <=28)
    	}
    }
    

    查询结果

    ---------------------------------
    | name     | age | job          |
    =================================
    | "Tom"    | 56  | "worker"     |
    | "Jim"    | 28  | "programmer" |
    | "Cherry" | 8   |              |
    ---------------------------------
    
  • 子查询

    查询语句

    prefix info: <http://zy.example.com/info#>
    prefix rel: <http://zy.example.com/rel#>
    
    select ?grandperson ?person
    where{
    	?grandperson rel:fatherof ?sonperson.
    	{
    		select ?sonperson ?person
    		where{
    			?sonperson rel:fatherof ?person.
    		}
    	}
    }
    
    

    查询结果

    ------------------------------------------------------------------------------
    | grandperson                        | person                                |
    ==============================================================================
    | <http://zy.example.com/person/Tom> | <http://zy.example.com/person/Cherry> |
    ------------------------------------------------------------------------------
    
    
相关推荐
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页