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

相關文章
相關標籤/搜索