gremlin id列提取胶

2024-05-15 11:23:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将Neptune数据库顶点提取到CSV文件中,但在id列提取失败。下面是我试图在AWS GLUE控制台中运行的脚本

import boto3
import os
import sys
import site
import json
import pandas as pd
from setuptools.command import easy_install
from importlib import reload

s3 = boto3.client('s3')
dir_path = os.path.dirname(os.path.realpath(__file__))
#os.path.dirname(sys.modules['__main__'].__file__)

install_path = os.environ['GLUE_INSTALLATION']
easy_install.main( ["--install-dir", install_path, "gremlinpython"] )

reload(site)

from gremlin_python import statics
from gremlin_python.structure.graph import Graph
from gremlin_python.process.graph_traversal import __
from gremlin_python.process.strategies import *
from gremlin_python.process.traversal import T
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection

graph = Graph()

remoteConn = DriverRemoteConnection('wss://neptune-test-new-reader-1.c3nqs7vjaggx.eu-west-1.neptune.amazonaws.com:8182/gremlin','g')
g = graph.traversal().withRemote(remoteConn)

vertices_columns = ['id', 'label', 'region','country']
vertices = g.V().hasLabel('airport').limit(2).project('id','label','region','country').by('T.id').by('T.label').by('region').by('country').select(values).fold()
for v in vertices:
    print(v)

错误: 未定义名称“值”

使用for循环尝试了以下脚本

import boto3
import os
import sys
import site
import json
import pandas as pd
from setuptools.command import easy_install
from importlib import reload

s3 = boto3.client('s3')
dir_path = os.path.dirname(os.path.realpath(__file__))
#os.path.dirname(sys.modules['__main__'].__file__)

install_path = os.environ['GLUE_INSTALLATION']
easy_install.main( ["--install-dir", install_path, "gremlinpython"] )

reload(site)

from gremlin_python import statics
from gremlin_python.structure.graph import Graph
from gremlin_python.process.graph_traversal import __
from gremlin_python.process.strategies import *
from gremlin_python.process.traversal import T
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection

graph = Graph()

remoteConn = DriverRemoteConnection('wss://neptune-test-new-reader-1.c3nqs7vjaggx.eu-west-1.neptune.amazonaws.com:8182/gremlin','g')
g = graph.traversal().withRemote(remoteConn)

vertices_columns = ['id', 'label', 'region','country']

"""
vertices = g.V().hasLabel('airport').limit(2).project('id','label','region','country').by('T.id').by('T.label').by('region').by('country').select(values).fold()
for v in vertices:
    print(v)
"""
#vertices = []
vertices = g.V().limit(1).valueMap(True).toList()
for v in vertices:
    print(v)
    for col in vertices_columns:
        print(v[col])
    #print(vertices)

错误:

打印输出(v) {T.id:1:'1',T.label:1:'airport','country':'US','region':'US-AL'} 密钥错误:id


Tags: installpathfromimportidbyosprocess
1条回答
网友
1楼 · 发布于 2024-05-15 11:23:46

select(values)中使用的values关键字是对定义为Column一部分的枚举的引用。在代码中,您可以使用select(Column.values),并且可以使用以下内容包含定义:

 from gremlin_python.process.traversal import Column

下面是一个Python示例:

>>> g.V('3').project('id','label','code').by(T.id).by(T.label).by('code').toList()
[{'id': '3', 'label': 'airport', 'code': 'AUS'}]

>>> g.V('3').project('id','label','code').by(T.id).by(T.label).by('code').select(Column.values).toList()
[['3', 'airport', 'AUS']]

请注意T.idT.label而不是内引号

相关问题 更多 >