pl/python调用aerospike

1.先安装pl/python模块python

yum install postgresql94-plpython.x86_64 
在postgres用户下执行createlang plpythonu

2.编写python代码 /usr/lib64/python2.6/plpy_as_cli_test.py
sql

import aerospike
import sys

config = {
  'hosts': [
      ( '192.168.0.30', 4000 )
  ],
  'policies': {
      'timeout': 1000 # milliseconds
  }
}

def get_as_data():
    global client
    if client == None:
        client = aerospike.client(config)
        client.connect()
    # Retrieve the record using the key.
    (key, meta, bins) = client.get(('test','demo','foo'))

3.编写pl/python存储过程
shell

CREATE FUNCTION py_get_as_data()
  RETURNS void
AS $$
  import plpy_as_cli_test
  plpy_as_cli_test.get_as_data()
$$ LANGUAGE plpythonu;

4.在psql中执行以下命令post

select py_get_as_data()

这样就完成了sql调用python代码完成aerospike交互的过程。spa

是否是很爽,sql能够经过pl/python中能够干任意想干的事情哦。
postgresql

相关文章
相关标签/搜索