我无法通过嵌入式Python绑定(neo4j / python-embedded,版本1.6)使用neo4j graphdatabase使用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的指针,也许这是个坏主意?).
解决方法:
Neo4j Embedded不能在Web服务器环境中使用-您应该使用Neo4j Server和Neo4j Server客户端.
Bulbs(http://bulbflow.com)是一个Neo4j Server客户端,其设计时考虑了Flask,实际上bulbflow.com使用Neo4j Heroku Add On(目前免费)在Heroku的Flask上运行.
您将Gremlin或Cypher用于查询和事务请求.
有关如何构建应用程序的示例,请参见Lightbulb(https://github.com/espeed/lightbulb)博客示例,特别是:
> https://github.com/espeed/lightbulb/blob/master/lightbulb/model.py
> https://github.com/espeed/lightbulb/blob/master/lightbulb/bulbsconf.py
请注意bulbsconf.py中的这一行:
bulbs_config.set_neo4j_heroku()
灯泡旨在与Neo4j Heroku Add On一起在Heroku上运行-如果您不在Heroku上运行,请将该行省略.
然后在Flask视图或app.py中,执行以下操作:
from bulbsconf import graph