Skip to main content

A connect graphdb package

Project description

graphdb

介绍

python 连接 graphdb 数据库的包

功能

一、数据管理
    1. 通过执行SPARQL语句就可查询和更新Graph数据库
    2. 下载数据并保存
    3. 上传数据
    4. 删除所有的节点数据

二、数据库管理
    1. 获取现有的repository列表
    2. 删除现有的repository
    3. 创建一个repository
    4. 重启现有的repository
    5. 获取现有的repository size

安装教程

pip install pygraphdb

快速上手

1. 导入包

import pygraphdb

2. 数据库 config

host = '0.0.0.0'
port = '7200'
db_name = 'db_name'
user = 'admin'
password = 'password'

3. sparql 语句(例)

sparql = "PREFIX disease_type_iri:<http://schema.omaha.org.cn/property/> " \
     "select distinct ?x ?y where { ?x disease_type_iri:Alternatename ?y .}"
     
4. 连接数据库

db = pygraphdb.connect(host=host, port=port, user=user, password=password, db=db_name)

5. 获取 cursor

cur = db.cursor()

6. 执行sparql语句,得到结果

result = cur.execute(sparql)

7. 打印结果

print(result)

8. 关闭 cursor

cur.close()

9. 关闭数据库

db.close()

使用with连接

1.第一种方式

with pygraph.connect(host, port, user, password, db) as db2:
    #获取 cursor
    cur2 = db2.cursor()
    #执行sparql语句,得到结果
    result2 = cur2.execute(sparql)
    cur2.close()
    print(result2)

2.第二种方式

db3 = pygraph.connect(host, port, db, user, password, db)
with db3.cursor() as cur3:
    result3 = cur3.execute(sparql)
    print(result3)
db3.close()

数据管理

import pygraphdb

host = '0.0.0.0'
port = '7200'
db_name = 'db_name'
user = 'admin'
password = 'password'

db = pygraphdb.connect(host, port, user, password, db_name)

cur = db.cursor()

"""
执行查询语句
"""
r = cur.execute('select * where {?s ?p ?o .} limit 100 ')
print(r)


"""
上传数据
"""
r2 = cur.upload_data('./example.owl')
print(r2)


"""
下载数据
"""
r3 = cur.download_data('./download_result.owl')
print(r3)


"""
删除全部节点数据
"""
r4 = cur.delete_all_data()
print(r4)

cur.close()

db.close()

数据库管理

import pygraphdb

host = '0.0.0.0'
port = '7200'
user = 'admin'
password = 'password'

db = pygraphdb.connect(host, port, user, password)

manager = db.manage_repository()

"""
创建一个repository
"""
db_name = 'example'
r = manager.create_repository_graphdb_free(db_name)
print(r)

"""
删除一个repository
"""
r2 = manager.delete_repository(db_name)
print(r2)


"""
查询所有的repository列表
"""
r3 = manager.get_repository_list()
print(r3)


"""
重启repository
"""
r4 = manager.restart_repository(db_name)
print(r4)


"""
获取数据库大小
"""
r5 = manager.get_repository_size(db_name)
print(r5)

manager.close()

db.close()

参与贡献

刘洪波

参考资料

grapddb 使用教程: https://graphdb.ontotext.com/documentation/free/index.html

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pygraphdb-2.0.3.tar.gz (11.8 kB view details)

Uploaded Source

File details

Details for the file pygraphdb-2.0.3.tar.gz.

File metadata

  • Download URL: pygraphdb-2.0.3.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.7

File hashes

Hashes for pygraphdb-2.0.3.tar.gz
Algorithm Hash digest
SHA256 244c967e10d6c5fbdef0e6e3dc421411e7f022440773707f8ef71ab5625d517a
MD5 5749f087c261066df93d0063d1888cbb
BLAKE2b-256 f55a2471b4808f353bf7d3e140b7f10a905798e88dce3f5bf5a978b870c5019b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page