Docker에서 Provate 서브넷에 구성된 Neptune에 연결

최근 도쿄 지역을 찾은 넥튠의 개발 효율을 높이기 위해 Docker 내부에서 연결을 시도해 보고 싶습니다.

컨디션

  • docker
  • docker-compose
  • python3.6
  • gremlin-python
  • 대략적인 절차

  • docker image
  • 준비
  • 포트 전달 설정
  • public의 EC2 준비(생략)
  • docker-compose.사용자 정의
  • extra_hosts 추가
  • 호스트의 .ssh/config에서 포트 전달 설정
  • 부팅 시 이미지에 적절한 위치에 호스트.ssh/를 설치
  • ssh/config에 포트 전송 쓰기

    Host neptune
        HostName 踏み台EC2のPublicIP
        User ec2-user
        IdentityFile ~/.ssh/EC2の鍵ファイル.pem
        GatewayPorts   yes
        LocalForward 8182 接続したいNeptuneクラスターエンドポイント:8182
    

    Docker file 준비


    어쨌든 python 3.6 의 공식 이미지를 사용한다.
    FROM python:3.6
    
    RUN apt-get update -y && apt-get upgrade -y
    RUN apt-get install autossh -y
    
    RUN mkdir /root/.ssh
    

    docker-compose.준비


    부족한 점은 보충해 주세요.
    내가 한 일
  • 컨테이너에 호스트.ssh/config를 장착/root/.sshdummy
  • 컨테이너 내에서 EC2의 키 파일을 가져옵니다.ssh
  • 로 복사
  • 키 파일에 대한 권한 설정
  • 포워딩 포트
  • /bin/bash 시작
  • 또한, extrahosts에서 Neptune 집단의 단점을 로컬 호스트와 연결하는 이유는 이번python에서 사용한 gremlin-pythonhttps://localhost 로 연결되면 오류가 발생할 수 있기 때문이다.
    version: '3'
    services:
      gremlin:
        command: >
          /bin/bash -c "cp /root/.sshdummy/config /root/.ssh/ && 
          cp /root/.sshdummy/EC2の鍵ファイル.pem /root/.ssh/ && 
          chmod 600 /root/.ssh/EC2の鍵ファイル.pem &&
          autossh neptune -f -N &&
          /bin/bash"
        volumes:
          - ~/.ssh:/root/.sshdummy
          - 
        extra_hosts:
          - "Neptuneクラスターエンドポイント:127.0.0.1"
    

    먼저 접속 확인

    docker-compose run gremlin
    
    그리고 들어가서 아래를 두드려 보세요.
    curl https://Neptuneクラスターのエンドポイント:8182
    
    > {"requestId":"28b45dc4-842c-c129-2d4d-******","detailedMessage":"no gremlin script supplied","code":"MissingParameterException"}
    
    다녀왔습니다.연결 가능합니다.

    gremlin-python으로 볼게요.


    매크로 패키지를 설치합니다.
    pip install gremlinpython
    
    저는 현재 역'네타아'노선 검색 서비스를 제공하는 회사에서 일하고 있기 때문에 역 데이터와 노선 데이터를 네타넷에 불러와 간단한 노선 검색을 하고 싶습니다.
    가까스로 차트 DB를 사용해 N 정점~N 정점의 경로를 검색했다.
    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.driver.driver_remote_connection import DriverRemoteConnection
    from gremlin_python.structure.graph import VertexProperty
    import time
    
    graph = Graph()
    g = graph.traversal().withRemote(DriverRemoteConnection('wss://Neptuneクラスターのエンドポイント:8182/gremlin','g'))
    
    # 出発地点
    origin = g.V().has('station', '県コードキー', '県コード').has('駅種別キー', 駅種別).sample(10).toList()
    
    # 到着地点
    destination = g.V().has('station', '県コードキー', '県コード').has('駅種別キー', 駅種別).sample(10).toList()
    
    # 頂点IDの配列化
    origin_ids = [x.id for x in origin]
    destination_ids = [x.id for x in destination]
    
    start_time = time.time()
    result = g.V().hasId(*origin_ids).repeat(__.outE().hasLabel('特定の辺のラベルのみ').inV().simplePath()).until(__.hasId(*destination_ids)).path().limit(100).map(__.unfold().hasLabel('station').values('name').fold())
    
    for path in result.toList():
        print('->'.join(path))
    
    print(time.time() - start_time)
    
    결실
    大崎広小路->五反田->戸越->中延
    大崎広小路->戸越銀座->荏原中延->旗の台->荏原町->中延
    青物横丁->新馬場->北品川->品川->大崎->五反田->大崎広小路
    大崎広小路->五反田->大崎->大井町->下神明->戸越公園->中延
    
    〜〜〜〜 省略 〜〜〜〜〜
    
    代田橋->笹塚->新宿->四ツ谷->御茶ノ水->秋葉原->浅草橋->両国->錦糸町->押上
    大崎広小路->五反田->大崎->品川->新橋->東銀座->銀座->東京->上野->日暮里->新三河島
    青物横丁->新馬場->北品川->品川->新橋->東銀座->銀座->東京->上野->日暮里->新三河島
    代田橋->笹塚->新宿->池袋->板橋->十条(東京都)->赤羽->尾久->上野->日暮里->新三河島0.43123531341552734(←処理時間
    
    다 됐습니다.

    주의할 점


  • Neptune에서 사용하는 Gremlin은 완전히 호환된다고 할 수 없습니다.
  • 공식 문서의 실현의 차이점
  • 를 자세히 살펴보겠습니다.
  • Gremlin 무스카시(푸념)
  • 좋은 웹페이지 즐겨찾기