在 EMR 中将 Spark Dataframe 推送到 Janusgraph for Spark 运行

Push Spark Dataframe to Janusgraph for Spark running in EMR

我在 EC2 实例上有一个 Janusgraph 运行,对于它的后端存储,我在 EC2 上也有 Cassandra 集群。我想将聚合和过滤数据从 Amazon EMR 上的 python 代码 Apache Spark(pyspark) 运行 推送到 Janusgraph。

我搜索过的内容:

我已阅读使用 gremlin here 插入,但定义为针对单个顶点和边执行此操作。我希望它立即插入,可能就像将所有数据帧推送到 Janusgraph。

是否有任何有效的方法直接从 spark 批量插入而不将其转换为 CSV 或从 bash

执行命令的任何中间步骤

我花了大约两周的时间来寻找答案,并将其发布以帮助他人。

要在远程计算机上写入 Dataframe 运行ning,您可以使用 gremlin,但为了高效读取(如果您想添加边),您可能需要 SparkGraphComputer。因为我的用例主要只是插入。我先关注一下

如果想从头开始做图遍历和配置,跟着long answer/installations

简答(从 spark 插入 vertex/edge 并从远程查询 janus)

您需要在远程安装 gremlin (sudo pip install gremlinpython),您可以像这样插入边缘

1)基本的gremline导入和制作远程图形对象

    from gremlin_python.structure.graph import Graph
    from gremlin_python.process.graph_traversal import __
    from gremlin_python.process.strategies import *
    from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
    graph = Graph()
    myGraphTraversal = graph.traversal().withRemote(DriverRemoteConnection('ws://<Your IP of JANUS>:8182/gremlin','myGraphTraversal'))

2)对于顶点

    for row in df.rdd.collect():
         myGraphTraversal.addV('Country').property('name',row["name"]).next()

3)对于边

for row in df.rdd.collect():

        node_from = myGraphTraversal.V().has('country',"name",row["from_country_name"]).next
        wallet_to = myGraphTraversal.V().has('country',"name",row["to_country_name"]).next()
       myGraphTraversal.V(wallet_to).as_('t').V(wallet_from).addE("sends").to("t").property('value',row["value"]).toList()

从远程测试顶点数(导入和图形对象与以前类似)

    print(myGraphTraversal.V().count().next())    

=> 11800

长答案/配置:

这里我假设你的数据存储和 janus 在不同的实例中,但我已经给出了本地待办事项的提示,如果它们不是的话

在 janus-server-node 上,为 python gremlin 和 tinkerpop 安装 jar

cd janus*    
./bin/gremlin-server.sh -i org.apache.tinkerpop gremlin-python 3.4.0(or 3.2.9)

第一个 edit/create 配置文件 (janusgraph.properties) 用于连接到 gremlin。

sudo vim janusgraph.properties

写下这些配置(注意gremlin.graph和graph.graphname)

storage.backend = cql (whatever you bakend is)
storage.hostname = 192.xx.xx.XXX (DataStore/CASSANDRA NODE/NODE2 IP)
gremlin.graph=org.janusgraph.core.ConfiguredGraphFactory
graph.graphname=ConfigurationManagementGraph
index.search.backend=elasticsearch
index.search.hostname=127.0.0.1

备份默认的 Gremlin 服务器配置

cp conf/gremlin-server/gremlin-server.yaml conf/gremlin-server/gremlin-server.yaml.orig

将 ConfiguredGraphFactory 配置设为默认配置

cp conf/gremlin-server/gremlin-server-configuration.yaml conf/gremlin-server/gremlin-server.yaml

现在编辑conf/gremlin-server/gremlin-server.yaml

sudo vim conf/gremlin-server/gremlin-server.yaml

进行此更改(设置主机,从文件数组中删除任何内容 []

host: 0.0.0.0 
port: 8182 (8182 is default, and you should have this but for me I have 6182)
org.apache.tinkerpop.gremlin.jsr223.ScriptFileGremlinPlugin: {files: []}}}}

运行 gremlin 服务器

bin/gremlin-server.sh # it should say that it is up on node 8182

编辑 conf/remote.yaml 并定义你的 janusgraph ip(如果你从同一台机器访问 gremlin 控制台,你也可以让它与 127.0.0.1 一起使用)

sudo vim conf/remote.yaml

将主机端口更改为

hosts: [192.xx.xx.xx] # your Janus node IP
port: 8182

打开 grmelin 控制台

bin/gremlin.sh

连接到远程(本例中为本地)8182 端口

:remote connect tinkerpop.server conf/remote.yaml session

接下来,将所有命令转移到您的 janusgraph 运行ning at :8154

:remote console

创建图表,

gremlin> map = new HashMap();
gremlin> map.put("storage.backend", "cql");
gremlin> map.put("storage.hostname", "192.xx.xx.xx(IP of storage backend)");
gremlin> map.put("graph.graphname", "graph1");
gremlin> ConfiguredGraphFactory.createConfiguration(new MapConfiguration(map));
==>null

不关闭 gremlin 并关闭服务器(如您所愿或按照以下方式)

ps -ef | grep gremlin-python
sudo kill -9 <gremlin process id 1> <gremlin process id 2> <gremlin process id n>

编辑script/empty-sample.groovy将graph1作为遍历源

def globals = [:]
myGraph = ConfiguredGraphFactory.open("graph1")
globals = [myGraphTraversal : myGraph.traversal()]

再次编辑conf/gremlin-server/gremlin-server.yaml

sudo vim conf/gremlin-server/gremlin-server.yaml

进行此更改并将 groovy 添加到文件的脚本中以启用从远程访问遍历的权限

org.apache.tinkerpop.gremlin.jsr223.ScriptFileGremlinPlugin: {files: [scripts/empty-sample.groovy]}}

re-运行 gremlin 服务器

bin/gremlin-server.sh

现在,从您要连接到 janus 的远程电脑。

安装 gremlin python

sudo yum -y install python-pip
pip install gremlinpython

转到step 1 of short answer(gremlin 导入和图形对象)

远程测试顶点

print(myGraphTraversal.V().count().next())    

=> 11800

电子病历

把它放在你的 bootstrap 中,这样 gremlin 就可以在你的 spark 脚本步骤执行之前安装

sudo pip-3.6 install gremlinpython #pip install gremlinpython for python2