|
| 1 | += Examples |
| 2 | + |
| 3 | +This page contains some code snippets with examples on using the library. |
| 4 | + |
| 5 | +== Importing a TTL file |
| 6 | +This a basic example for importing a single TTL file. |
| 7 | +Insert your own database credentials for `AURA_DB_URI`, `AURA_DB_USERNAME`, `AURA_DB_PWD` to use this template. |
| 8 | + |
| 9 | +[source,python] |
| 10 | +---- |
| 11 | +from rdflib_neo4j import Neo4jStoreConfig |
| 12 | +from rdflib_neo4j import HANDLE_VOCAB_URI_STRATEGY |
| 13 | +
|
| 14 | +# Get your Aura Db free instance here: https://neo4j.com/cloud/aura-free/#test-drive-section |
| 15 | +AURA_DB_URI="your_db_uri" |
| 16 | +AURA_DB_USERNAME="neo4j" |
| 17 | +AURA_DB_PWD="your_db_pwd" |
| 18 | +
|
| 19 | +auth_data = {'uri': AURA_DB_URI, |
| 20 | + 'database': "neo4j", |
| 21 | + 'user': AURA_DB_USERNAME, |
| 22 | + 'pwd': AURA_DB_PWD} |
| 23 | +from rdflib import Namespace |
| 24 | +
|
| 25 | +# Define your prefixes |
| 26 | +prefixes = { |
| 27 | + 'neo4ind': Namespace('http://neo4j.org/ind#'), |
| 28 | + 'neo4voc': Namespace('http://neo4j.org/vocab/sw#'), |
| 29 | + 'nsmntx': Namespace('http://neo4j.org/vocab/NSMNTX#'), |
| 30 | + 'apoc': Namespace('http://neo4j.org/vocab/APOC#'), |
| 31 | + 'graphql': Namespace('http://neo4j.org/vocab/GraphQL#') |
| 32 | +} |
| 33 | +# Define your custom mappings |
| 34 | +config = Neo4jStoreConfig(auth_data=auth_data, |
| 35 | + custom_prefixes=prefixes, |
| 36 | + handle_vocab_uri_strategy=HANDLE_VOCAB_URI_STRATEGY.IGNORE, |
| 37 | + batching=True) |
| 38 | +from rdflib_neo4j import Neo4jStore |
| 39 | +from rdflib import Graph |
| 40 | +file_path = 'https://raw.githubusercontent.com/neo4j-labs/neosemantics/3.5/docs/rdf/nsmntx.ttl' |
| 41 | +
|
| 42 | +graph_store = Graph(store=Neo4jStore(config=config)) |
| 43 | +graph_store.parse(file_path,format="ttl") |
| 44 | +graph_store.close(True) |
| 45 | +---- |
| 46 | + |
| 47 | +== Advanced Examples |
| 48 | + |
| 49 | +=== Initialize Neo4jStore |
| 50 | + |
| 51 | +[source,python] |
| 52 | +---- |
| 53 | +from rdflib_neo4j import Neo4jStoreConfig, Neo4jStore, HANDLE_VOCAB_URI_STRATEGY |
| 54 | +from rdflib import Namespace, Graph, URIRef, RDF, SKOS, Literal |
| 55 | + |
| 56 | +
|
| 57 | +# Define your custom prefixes |
| 58 | +prefixes = { |
| 59 | + 'neo4ind': Namespace('http://neo4j.org/ind#'), |
| 60 | + 'neo4voc': Namespace('http://neo4j.org/vocab/sw#'), |
| 61 | +} |
| 62 | +
|
| 63 | +# Neo4j connection credentials |
| 64 | +auth_data = {'uri': 'your_neo4j_uri', |
| 65 | + 'database': 'neo4j', |
| 66 | + 'user': "neo4j", |
| 67 | + 'pwd': 'your_password'} |
| 68 | +
|
| 69 | +# Define your Neo4jStoreConfig |
| 70 | +config = Neo4jStoreConfig(auth_data=auth_data, |
| 71 | + custom_prefixes=prefixes, |
| 72 | + handle_vocab_uri_strategy=HANDLE_VOCAB_URI_STRATEGY.IGNORE, |
| 73 | + batching=False) |
| 74 | +
|
| 75 | +neo4j_store = Neo4jStore(config=config) |
| 76 | +graph_store = Graph(store=neo4j_store) |
| 77 | +
|
| 78 | +---- |
| 79 | + |
| 80 | +=== Import by Reference URL |
| 81 | + |
| 82 | +[source,python] |
| 83 | +---- |
| 84 | +file_path = 'https://raw.githubusercontent.com/neo4j-labs/neosemantics/3.5/docs/rdf/nsmntx.ttl' |
| 85 | +graph_store.parse(file_path,format="ttl") |
| 86 | +---- |
| 87 | + |
| 88 | +=== Write Individual Triples |
| 89 | + |
| 90 | +[source,python] |
| 91 | +---- |
| 92 | +aura = URIRef("http://neo4j.com/voc/tech#AuraDB") |
| 93 | +
|
| 94 | +graph_store.add((aura, RDF.type, SKOS.Concept)) |
| 95 | +graph_store.add((aura, SKOS.prefLabel, Literal("AuraDB"))) |
| 96 | +graph_store.add((aura, SKOS.broader, URIRef("http://neo4j.org/ind#neo4j355"))) |
| 97 | + |
| 98 | +---- |
| 99 | + |
| 100 | +=== SPARQL Query with Batching |
| 101 | + |
| 102 | +[source,python] |
| 103 | +---- |
| 104 | +import requests |
| 105 | +import urllib.parse |
| 106 | +
|
| 107 | +endpoint = "https://id.nlm.nih.gov/mesh/sparql" |
| 108 | +sparql = """ |
| 109 | +PREFIX rdfs: |
| 110 | +PREFIX meshv: |
| 111 | +PREFIX mesh: |
| 112 | +PREFIX rdf: |
| 113 | +
|
| 114 | +CONSTRUCT { ?s ?p ?o } |
| 115 | +FROM |
| 116 | +WHERE { |
| 117 | + { |
| 118 | + ?s ?p ?o |
| 119 | + filter(?s = mesh:D000086402 || ?o = mesh:D000086402) |
| 120 | + } |
| 121 | + union |
| 122 | + { |
| 123 | + mesh:D000086402 ?x ?s . |
| 124 | + ?s ?p ?o . |
| 125 | + filter(?x != rdf:type && (isLiteral(?o) || ?p = rdf:type)) |
| 126 | + } |
| 127 | + union |
| 128 | + { |
| 129 | + ?s ?x mesh:D000086402 . |
| 130 | + ?s ?p ?o . |
| 131 | + filter(isLiteral(?o|| ?p = rdf:type)) |
| 132 | + } |
| 133 | +} |
| 134 | +""" |
| 135 | +
|
| 136 | +# Define your Neo4jStoreConfig |
| 137 | +config = Neo4jStoreConfig(auth_data=auth_data, |
| 138 | + custom_prefixes=prefixes, |
| 139 | + handle_vocab_uri_strategy=HANDLE_VOCAB_URI_STRATEGY.IGNORE, |
| 140 | + batching=True) |
| 141 | +
|
| 142 | +neo4j_store = Neo4jStore(config=config) |
| 143 | +graph_store = Graph(store=neo4j_store) |
| 144 | +
|
| 145 | +query_response = requests.get(endpoint, params = {"query": sparql , "format" : "TURTLE"}) |
| 146 | +graph_store.parse(data=query_response.text,format='ttl') |
| 147 | +graph_store.close(commit_pending_transaction=True) |
| 148 | +
|
| 149 | +---- |
0 commit comments