如何在Google Compute Engine上设置neo4j?
我在想怎么把neo4j这个数据库用在谷歌云计算引擎上。有没有人试过这个?遇到过什么问题吗?
2 个回答
4
最简单又最安全的方法是使用 docker neo4j 镜像
这里有 docker 的官方文档,可以帮助你在谷歌云计算引擎上安装和部署
12
好的,下面是一些基本的设置步骤:
基本设置
- 安装并设置
gcloud
工具。 - 安装 py2neo 库。
- 创建你的 GCE 实例,可以通过这个链接来创建:(https://console.developers.google.com/project/PROJECT_APPID/compute/instancesAdd),选择一个镜像,比如 (debian-7-wheezy-v20141021, Debian GNU/Linux 7.7 (wheezy) amd64,创建于2014-10-21,或者其他任何镜像)。
- 通过 SSH 连接到你的实例,命令是
gcloud compute ssh INSTANCE_NAME --zone AVAILABLE_ZONES
,你可以在这里找到可用的区域 AVAILABLE_ZONES。 - 在 GCE 上下载并安装
neo4j
,你可能需要先安装 Java(可以参考这个 解决方案),还有 lsof(安装命令是apt-get install lsof
)。
GCE 配置
(可选)为 neo4j 添加 https 支持
将 neo4j 的 7474 端口加入白名单(更多信息请查看 网络和防火墙)。
从 github 添加安全的 用户名:密码。
使用命令
gcloud compute firewall-rules create neo4j --network default --allow tcp:7474
来设置。
开始玩耍
- 启动 neo4j 服务器,命令是
./bin/neo4j start
。 - 在浏览器中检查你的运行实例,地址是
http://IP_ADDRESS:7474/
。
一旦 py2neo
安装完成并且服务器启动,可以尝试一些 Python 代码来测试一下。
>> from py2neo.neo4j import GraphDatabaseService, CypherQuery
>> # Set up a link to the local graph database.
>> # When () left blank defaults to http://localhost:7474/db/data/
>> graph = GraphDatabaseService('http://IP_ADDRESS:7474/db/data/')
>> CypherQuery(graph, "CREATE (n {name:'Example'}) RETURN n;").execute()
上面的 Python 设置/代码,你也可以在 GAE 中使用。
参考资料
编辑:Appengine + Neo4j
from py2neo import neo4j
GRAPH_DB = neo4j.GraphDatabaseService(
'http://uname:psswd@localhost:7474/db/data/')
if IS_PROD:
GRAPH_DB = neo4j.GraphDatabaseService(
'http://uname:psswd@host:port/db/data/')
def _execute(query):
"""Execute all neo4j queries and return list of Record objects.
Returns:
Returns list of Record objects.
"""
try:
result = neo4j.CypherQuery(GRAPH_DB, query).execute()
# logging.info(result.data)
return result
except neo4j.CypherError as error:
logging.error(error.exception)
except DeadlineExceededError as dead:
logging.warn(dead)
except urlfetch_errors.InternalTransientError as tra_error:
logging.warn(tra_error)
except httplib.HTTPException as exp:
logging.warn(exp)
except neo4j.http.SocketError as soc:
logging.warn(soc)