DseGraphFrameを使用したグラフのインポート

グラフをDataStax Enterpriseにインポートするには、DseGraphFrameを使用します。

グラフをDataStax Enterpriseにインポートするには、DseGraphFrameを使用します。

始める前に

この機能は実験段階です。

グラフのスキーマは、グラフをインポートする前にGremlin ConsoleかDSE Studioに手動で作成する必要があります。インポートは、カスタムIDマッピングを使用した場合にのみ可能です。

手順

  1. Sparkシェルを起動します。
    $ dse spark
  2. DseGraphFrameを使用してグラフをJSONにエクスポートした場合は、Sparkシェルを使用してグラフをインポートします。
    val g = spark.dseGraph("gods_import")
    g.updateVertices(spark.read.json("/tmp/v.json"))
    g.updateEdges(spark.read.json("/tmp/e.json"))
    val g = spark.dseGraph("graph name")
    g.updateVertices(spark.read.json("path to exported vertices JSON"))
    g.updateEdges(spark.read.json("path to exported edges JSON"))
  3. カスタム・グラフを使用している場合:
    1. グラフのスキーマを確認し、DSE Graphスキーマの予想されるスキーマにグラフをマッピングする方法を書き留めます。
      この例では、GraphFrameプロジェクトfriendsグラフを使用します。
      scala> import org.graphframes._
      scala> val g: GraphFrame = examples.Graphs.friends
      scala> g.vertices.printSchema
      root
       |-- id: string (nullable = true)
       |-- name: string (nullable = true)
       |-- age: integer (nullable = false)
      
      scala> g.edges.printSchema
      root
       |-- src: string (nullable = true)
       |-- dst: string (nullable = true)
       |-- relationship: string (nullable = true)
    2. Gremlin ConsoleかDSE Studioにスキーマを作成します。
      system.graph('friends').create()
      :remote config alias g friends.g
          schema.propertyKey("age").Int().create()
          schema.propertyKey("name").Text().create()
          schema.propertyKey("id").Text().single().create()
          schema.vertexLabel('people').partitionKey("id").properties("name", "age").create();
          schema.edgeLabel("friend").create()
          schema.edgeLabel("follow").create()
    3. Sparkシェルに空のDseGraphFrameグラフを作成し、ターゲット・スキーマを確認します。
      scala>  val d = spark.dseGraph("friends")
      scala> d.V.printSchema
      root
       |-- id: string (nullable = false)
       |-- ~label: string (nullable = false)
       |-- _id: string (nullable = true)
       |-- name: string (nullable = true)
       |-- age: integer (nullable = true)
      
      scala> d.E.printSchema
      root
       |-- src: string (nullable = false)
       |-- dst: string (nullable = false)
       |-- ~label: string (nullable = true)
       |-- id: string (nullable = true)
    4. エッジと頂点をターゲット形式に変換します。
      scala> val v = g.vertices.select ($"id" as "_id", lit("people") as "~label", $"name", $"age")
      scala> val e = g.edges.select (d.idColumn(lit("people"), $"src") as "src", d.idColumn(lit("people"), $"dst") as "dst",  $"relationship" as "~label")
    5. 変換した頂点とエッジをターゲット・グラフに追加書き込みします。
      d.updateVertices (v)
      d.updateEdges (e)