3

我无法通过嵌入式 python 绑定(neo4j / python-embedded,版本 1.6)使用 neo4j 图形数据库使用 Python Micro-Webframework Flask 实现一个工作的最小示例。我所拥有的(基于这个 Stack-Overflow- thread)是:

import os
import jpype
from neo4j import GraphDatabase
from flask import Flask, g

# configuration
DATABASE = 'graphdatabase'
DEBUG = True
SECRET_KEY = 'blubber'
USERNAME = 'tobias'
PASSWORD = 'bla'
ADMIN = 'admin'

app = Flask(__name__)
app.config.from_object(__name__)

def connectDB(): 
    return GraphDatabase(app.config['DATABASE'])

def initDB():
    db = connectDB()
    with db.transaction:
        users = db.node()
        userIndex = db.node.indexes.create('users')
        user = db.node(name=app.config['ADMIN'])
        userIndex['name'][app.config['ADMIN']] = user
    db.shutdown()
    print "Database initialized."

@app.before_request
def before_request():
    jpype.attachThreadToJVM()
    g.db = connectDB()

@app.teardown_request
def teardown_request(exception):
    jpype.attachThreadToJVM()
    g.db.shutdown()

@app.route('/')
def index():
    with g.db.transaction:
          userIndex = g.db.node.indexes.get('users')
          user = userIndex['name'][app.config['ADMIN']].single
          username = user['name']
    return render_template('index.html', name=username)

if os.path.exists(app.config['DATABASE']) == False:
    initDB()

if __name__ == '__main__':
  app.run()

不幸的是,它抛出:

    File "/home/tobias/Esk/Dev/adhocracyLight/venv/lib/python2.7/site-packages/flask/app.py", line 1518, in __call__
return self.wsgi_app(environ, start_response)
File "/home/tobias/Esk/Dev/adhocracyLight/venv/lib/python2.7/site-packages/flask/app.py", line 1506, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/tobias/Esk/Dev/adhocracyLight/venv/lib/python2.7/site-packages/flask/app.py", line 1504, in wsgi_app
response = self.full_dispatch_request()
File "/home/tobias/Esk/Dev/adhocracyLight/venv/lib/python2.7/site-packages/flask/app.py", line 1264, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/tobias/Esk/Dev/adhocracyLight/venv/lib/python2.7/site-packages/flask/app.py", line 1262, in full_dispatch_request
rv = self.dispatch_request()
File "/home/tobias/Esk/Dev/adhocracyLight/venv/lib/python2.7/site-packages/flask/app.py", line 1248, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/tobias/Esk/Dev/adhocracyLight/adhocracyLight.py", line 73, in index
userIndex = g.db.node.indexes.get('users')
File "/home/tobias/Esk/Dev/adhocracyLight/venv/lib/python2.7/site-packages/neo4j/index.py", line 36, in get
return self._index.forNodes(name)
java.lang.RuntimeExceptionPyRaisable: java.lang.IllegalArgumentException: No index provider 'lucene' found. Maybe the intended provider (or one more of its dependencies) aren't on the classpath or it failed to load.

我猜这个问题是由于线程(g是线程本地的并且包含指向neo4j-Database的指针,也许这是一个坏主意?)。

4

1 回答 1

3

Neo4j Embedded 不打算在 Web 服务器环境中使用——您应该使用 Neo4j Server 和 Neo4j Server 客户端。

Bulbs ( http://bulbflow.com ) 是一个 Neo4j 服务器客户端,在设计时考虑了 Flask,实际上,bulbflow.com 使用 Neo4j Heroku Add On(目前是免费的)在 Heroku 上的 Flask 上运行。

您使用 Gremlin 或 Cypher 进行查询和交易请求。

有关如何构建应用程序的示例,请参阅 Lightbulb ( https://github.com/espeed/lightbulb ) 博客示例,尤其是:

注意bulbsconf.py 中的这一行:

bulbs_config.set_neo4j_heroku()

Lightbulb 旨在通过 Neo4j Heroku Add On 在 Heroku 上运行——如果您不在 Heroku 上运行,请忽略该行。

然后在您的 Flask 视图或您的 app.py 中,执行以下操作:

from bulbsconf import graph
于 2012-06-02T17:39:41.127 回答