首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从url走一步

从url走一步
EN

Stack Overflow用户
提问于 2020-03-31 18:02:31
回答 1查看 462关注 0票数 0

https://www.compose.com/articles/importing-graphs-into-janusgraph/展示了如何将数据导入到janus图中。

由于无法使用localhost在我的Mac计算机上工作,所以我尝试连接到远程Ubuntu机器,在那里我使用:

代码语言:javascript
复制
docker run -it -p 8182:8182 janusgraph/janusgraph

然后,我想使用加载数据,结果失败了。为了获得一个简单的可重复示例,我尝试了以下步骤:

代码语言:javascript
复制
server= ...
port=8182
graph = Graph()
janusgraphurl='ws://%s:%s/gremlin' % (server,port)
connection = DriverRemoteConnection(janusgraphurl, 'g')    
g = graph.traversal().withRemote(connection)
dataurl="https://github.com/krlawrence/graph/raw/master/sample-data/air-routes.graphml"
g.io(dataurl).read().iterate()

我得到了以下错误:

代码语言:javascript
复制
 File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/gremlin_python/driver/protocol.py", line 110, in data_received
    raise GremlinServerError(message["status"])
gremlin_python.driver.protocol.GremlinServerError: 500: https://github.com/krlawrence/graph/raw/master/sample-data/air-routes.graphml does not exist

虽然链接https://github.com/krlawrence/graph/raw/master/sample-data/air-routes.graphml似乎运行得很好。

使用python语言变体从url加载图形数据的正确方法是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-01 05:56:22

凯文·劳伦斯是对的。

砰的一声:

代码语言:javascript
复制
docker run -it janusgraph/janusgraph /bin/bash

我可以查一下可用的文件

代码语言:javascript
复制
root@8542ed1b8232:/opt/janusgraph# ls data
grateful-dead-janusgraph-schema.groovy  tinkerpop-crew-typed.json
grateful-dead-typed.json        tinkerpop-crew-v2d0-typed.json
grateful-dead-v2d0-typed.json       tinkerpop-crew-v2d0.json
grateful-dead-v2d0.json         tinkerpop-crew.json
grateful-dead.json          tinkerpop-crew.kryo
grateful-dead.kryo          tinkerpop-modern-typed.json
grateful-dead.txt           tinkerpop-modern-v2d0-typed.json
grateful-dead.xml           tinkerpop-modern-v2d0.json
script-input-grateful-dead.groovy   tinkerpop-modern.json
script-input-tinkerpop.groovy       tinkerpop-modern.kryo
tinkerpop-classic-typed.json        tinkerpop-modern.xml
tinkerpop-classic-v2d0-typed.json   tinkerpop-sink-typed.json
tinkerpop-classic-v2d0.json     tinkerpop-sink-v2d0-typed.json
tinkerpop-classic.json          tinkerpop-sink-v2d0.json
tinkerpop-classic.kryo          tinkerpop-sink.json
tinkerpop-classic.txt           tinkerpop-sink.kryo
tinkerpop-classic.xml

在进行测试时,我选择了tinkerpo-ken.xml:

代码语言:javascript
复制
    file="data/tinkerpop-modern.xml";
    g.io(file).read().iterate()
    vCount=g.V().count().next()
    print ("%s has %d vertices" % (file,vCount))
    assert vCount==6

这很管用。谢谢!

若要使“外部”数据可用于码头映像,可以使用--挂载选项:

代码语言:javascript
复制
docker run -it -p 8182:8182 --mount src=<path to graphdata>,target=/graphdata,type=bind janusgraph/janusgraph

以下helper类帮助共享文件:

RemoteGremlin

代码语言:javascript
复制
'''
Created on 2020-03-30

@author: wf
'''
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
from gremlin_python.structure.graph import Graph
from shutil import copyfile
import os

class RemoteGremlin(object):
    '''
    helper for remote gremlin connections
    '''

    def __init__(self, server, port=8182):
        '''
        construct me with the given server and port
        '''
        self.server=server
        self.port=port    

    def sharepoint(self,sharepoint,sharepath):
        '''
        set up the sharepoint
        '''
        self.sharepoint=sharepoint
        self.sharepath=sharepath


    def share(self,file):
        '''
        share the given file  and return the path as seen by the server
        '''
        fbase=os.path.basename(file)
        copyfile(file,self.sharepoint+fbase)
        return self.sharepath+fbase

    def open(self):
        '''
        open the remote connection
        '''
        self.graph = Graph()
        self.url='ws://%s:%s/gremlin' % (self.server,self.port)
        self.connection = DriverRemoteConnection(self.url, 'g')    
        # The connection should be closed on shut down to close open connections with connection.close()
        self.g = self.graph.traversal().withRemote(self.connection)

    def close(self):
        '''
        close the remote connection
        '''
        self.connection.close()

python单元测试:

代码语言:javascript
复制
'''
Created on 2020-03-28

@author: wf
'''
import unittest
from tp.gremlin import RemoteGremlin

class JanusGraphTest(unittest.TestCase):
    '''
    test access to a janus graph docker instance via the RemoteGremlin helper class
    '''

    def setUp(self):
        pass


    def tearDown(self):
        pass

    def test_loadGraph(self):
        # change to your server
        rg=RemoteGremlin("capri.bitplan.com")
        rg.open()
        # change to your shared path
        rg.sharepoint("/Volumes/bitplan/user/wf/graphdata/","/graphdata/")
        g=rg.g
        graphmlFile="air-routes-small.xml";
        shared=rg.share(graphmlFile)
        # drop the existing content of the graph
        g.V().drop().iterate()
        # read the content from the air routes example
        g.io(shared).read().iterate()
        vCount=g.V().count().next()
        print ("%s has %d vertices" % (shared,vCount))
        assert vCount==47


if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60957035

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档