Skip to main content

StellarDB 的 Python 接口

项目描述

PyStellarDB

PyStellarDB 是一个 Python API,用于执行 Transwarp Exetended OpenCypher(TEoC) 和 Hive 查询。它还可以生成可以在 PySpark 中使用的 RDD 对象。它基于 PyHive(https://github.com/dropbox/PyHive)和 PySpark(https://github.com/apache/spark/

PySpark RDD

我们使用sc.parallelize(data)中的相同方法破解了一种生成 RDD 对象的方法。如果查询返回大量数据,可能会导致内存恐慌。

如果您确实需要大量数据,用户可以使用解决方法:

  1. 如果您正在查询图形,请参阅第 4.4.5 章的 StellarDB 手册将查询数据保存到临时表中。

  2. 如果要查询 SQL 表,请将查询结果保存到临时表中。

  3. 找到第1步或第2步生成的临时表的HDFS路径。

  4. 使用像sc.newAPIHadoopFile()这样的 API来生成 RDD。

用法

PLAIN 模式(未配置安全性)

from pystellardb import stellar_hive

conn = stellar_hive.StellarConnection(host="localhost", port=10000, graph_name='pokemon')
cur = conn.cursor()
cur.execute('config query.lang cypher')
cur.execute('use graph pokemon')
cur.execute('match p = (a)-[f]->(b) return a,f,b limit 1')

print cur.fetchall()

LDAP 模式

from pystellardb import stellar_hive

conn = stellar_hive.StellarConnection(host="localhost", port=10000, username='hive', password='123456', auth='LDAP', graph_name='pokemon')
cur = conn.cursor()
cur.execute('config query.lang cypher')
cur.execute('use graph pokemon')
cur.execute('match p = (a)-[f]->(b) return a,f,b limit 1')

print cur.fetchall()

Kerberos 模式

# Make sure you have the correct realms infomation about the KDC server in /etc/krb5.conf
# Make sure you have the correct keytab file in your environment
# Run kinit command:
# In Linux: kinit -kt FILE_PATH_OF_KEYTABL PRINCIPAL_NAME
# In Mac: kinit -t FILE_PATH_OF_KEYTABL -f PRINCIPAL_NAME

from pystellardb import stellar_hive

conn = stellar_hive.StellarConnection(host="localhost", port=10000, kerberos_service_name='hive', auth='KERBEROS', graph_name='pokemon')
cur = conn.cursor()
cur.execute('config query.lang cypher')
cur.execute('use graph pokemon')
cur.execute('match p = (a)-[f]->(b) return a,f,b limit 1')

print cur.fetchall()

执行 Hive 查询

from pystellardb import stellar_hive

# If `graph_name` parameter is None, it will execute a Hive query and return data just as PyHive does
conn = stellar_hive.StellarConnection(host="localhost", port=10000, database='default')
cur = conn.cursor()
cur.execute('SELECT * FROM default.abc limit 10')

执行 Graph Query 并更改为 PySpark RDD 对象

from pyspark import SparkContext
from pystellardb import stellar_hive

sc = SparkContext("local", "Demo App")

conn = stellar_hive.StellarConnection(host="localhost", port=10000, graph_name='pokemon')
cur = conn.cursor()
cur.execute('config query.lang cypher')
cur.execute('use graph pokemon')
cur.execute('match p = (a)-[f]->(b) return a,f,b limit 10')

rdd = cur.toRDD(sc)

def f(x): print(x)

rdd.map(lambda x: (x[0].toJSON(), x[1].toJSON(), x[2].toJSON())).foreach(f)

# Every line of this query is in format of Tuple(VertexObject, EdgeObject, VertexObject)
# Vertex and Edge object has a function of toJSON() which can print the object in JSON format

执行 Hive Query 并更改为 PySpark RDD 对象

from pyspark import SparkContext
from pystellardb import stellar_hive

sc = SparkContext("local", "Demo App")

conn = stellar_hive.StellarConnection(host="localhost", port=10000)
cur = conn.cursor()
cur.execute('select * from default_db.default_table limit 10')

rdd = cur.toRDD(sc)

def f(x): print(x)

rdd.foreach(f)

# Every line of this query is in format of Tuple(Column, Column, Column)

依赖项

必需的:

  • Python 2.7+ / 小于 Python 3.7

系统 SASL

不同的系统需要安装不同的包来启用 SASL 支持。下面是一些如何在不同发行版上安装软件包的示例。

Ubuntu:

apt-get install libsasl2-dev libsasl2-2 libsasl2-modules-gssapi-mit
apt-get install python-dev gcc              #Update python and gcc if needed

RHEL/CentOS:

yum install cyrus-sasl-md5 cyrus-sasl-plain cyrus-sasl-gssapi cyrus-sasl-devel
yum install gcc-c++ python-devel.x86_64     #Update python and gcc if needed

# If your Python environment is 3.X, then you may need to compile and reinstall Python
# if pip3 install fails with a message like 'Can't connect to HTTPS URL because the SSL module is not available'

# 1. Download a higher version of openssl, e.g: https://www.openssl.org/source/openssl-1.1.1k.tar.gz
# 2. Install openssl: ./config && make && make install
# 3. Link openssl: echo /usr/local/lib64/ > /etc/ld.so.conf.d/openssl-1.1.1.conf
# 4. Update dynamic lib: ldconfig -v
# 5. Download a Python source package
# 6. vim Modules/Setup, search '_socket socketmodule.c', uncomment
#    _socket socketmodule.c
#    SSL=/usr/local/ssl
#    _ssl _ssl.c \
#            -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
#            -L$(SSL)/lib -lssl -lcrypto
#
# 7. Install Python: ./configure && make && make install

视窗:

# There are 3 ways of installing sasl for python on windows
# 1. (recommended) Download a .whl version of sasl from https://www.lfd.uci.edu/~gohlke/pythonlibs/#sasl
# 2. (recommended) If using anaconda, use conda install sasl.
# 3. Install Microsoft Visual C++ 9.0/14.0 buildtools for python2.7/3.x, then pip install sasl(under test).

通知

如果你安装 pystellardb >= 0.9,那么它会在系统中安装一个 beeline 命令。如果不需要,请删除 /usr/local/bin/beeline。

要求

安装使用

  • pip install 'pystellardb[hive]'用于 Hive 界面。

PyHive 与

Windows Kerberos 配置

如果您从 Windows 平台使用 Kerberos 身份验证连接到数据库,则需要首先安装和配置 Kerberos for Windows。从http://web.mit.edu/kerberos/dist/获取

安装后,配置环境变量。确保您的 Kerberos 变量设置在 JDK 变量之前(如果您有 JDK),因为 JDK 也有 kinit 等。

在您的 KDC 上找到 /etc/krb5.conf,将其复制到 Windows 上的 krb5.ini 并进行一些修改。例如(KDC 上的 krb5.conf):

[logging]
default = FILE:/var/log/krb5libs.log
kdc = FILE:/var/log/krb5kdc.log
admin_server = FILE:/var/log/kadmind.log

[libdefaults]
default_realm = DEFAULT
dns_lookup_realm = false
dns_lookup_kdc = false
ticket_lifetime = 24h
renew_lifetime = 7d
forwardable = true
allow_weak_crypto = true
udp_preference_limit = 32700
default_ccache_name = FILE:/tmp/krb5cc_%{uid}

[realms]
DEFAULT = {
kdc = host1:1088
kdc = host2:1088
}

修改一下,删除[libdefaults]中的[logging]和default_ccache_name:

[libdefaults]
default_realm = DEFAULT
dns_lookup_realm = false
dns_lookup_kdc = false
ticket_lifetime = 24h
renew_lifetime = 7d
forwardable = true
allow_weak_crypto = true
udp_preference_limit = 32700

[realms]
DEFAULT = {
kdc = host1:1088
kdc = host2:1088
}

这是用于 Windows Kerberos 的 krb5.ini。把它放在这三个地方:

C:ProgramDataMITKerberos5krb5.ini

C:程序文件MITKerberoskrb5.ini

C:Windowskrb5.ini

最后配置hosts在:C:/Windows/System32/drivers/etc/hosts 添加上例中host1、host2的ip映射。例如

10.6.6.96     host1
10.6.6.97     host2

现在,您可以在命令行中运行 kinit 了!

测试

他在路上

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

PyStellarDB-0.11.0.tar.gz (28.3 kB 查看哈希

已上传 source

内置分布

PyStellarDB-0.11.0-py2.py3-none-any.whl (12.6 kB 查看哈希

已上传 py2 py3