-
Notifications
You must be signed in to change notification settings - Fork 15
/
graphWriteCosmosDB.scala
136 lines (96 loc) · 4.53 KB
/
graphWriteCosmosDB.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Databricks notebook source
// MAGIC %md # Writing GraphFrames to Azure Cosmos DB Gremlin API
// MAGIC This notebook is based on the `GraphFrames` example [specified here](https://graphframes.github.io/user-guide.html#tab_scala_0). It requires [graphframes](https://spark-packages.org/package/graphframes/graphframes) and [azure-cosmosdb-spark (uber jar)](https://github.com/Azure/azure-cosmosdb-spark#using-databricks-notebooks) libraries to be uploaded and attached to the cluster. **Python version** of this notebook can be [found here](https://github.com/syedhassaanahmed/databricks-notebooks/blob/master/graph_write_cosmosdb.py)
// COMMAND ----------
import org.apache.spark.sql.functions.lit
val v = sqlContext.createDataFrame(List(
("a", "Alice", 34),
("b", "Bob", 36),
("c", "Charlie", 30),
("d", "David", 29),
("e", "Esther", 32),
("f", "Fanny", 36),
("g", "Gabby", 60)
)).toDF("id", "name", "age")
.withColumn("entity", lit("person"))
// COMMAND ----------
val e = sqlContext.createDataFrame(List(
("a", "b", "friend"),
("b", "c", "follow"),
("c", "b", "follow"),
("f", "c", "follow"),
("e", "f", "follow"),
("e", "d", "friend"),
("d", "a", "friend"),
("a", "e", "friend")
)).toDF("src", "dst", "relationship")
// COMMAND ----------
import org.graphframes.GraphFrame
val g = GraphFrame(v, e)
// COMMAND ----------
display(g.vertices)
// COMMAND ----------
display(g.edges)
// COMMAND ----------
// MAGIC %md ## Convert Vertices and Edges to Cosmos DB internal format
// MAGIC Cosmos DB Gremlin API internally keeps a JSON document representation of Edges and Vertices [as explained here](https://github.com/LuisBosquez/azure-cosmos-db-graph-working-guides/blob/master/graph-backend-json.md). Also `id` in Cosmos DB is [part of the resource URI](https://github.com/Azure/azure-cosmosdb-dotnet/issues/35#issuecomment-121009258) and hence must be URL encoded.
// COMMAND ----------
import java.net.URLEncoder
import java.nio.charset.StandardCharsets
import org.apache.spark.sql.types.StringType
val urlEncode = (value: String) => {
URLEncoder.encode(value, StandardCharsets.UTF_8.toString).replaceAll("\\+", "%20")
}
val udfUrlEncode = udf(urlEncode, StringType)
// COMMAND ----------
import org.apache.spark.sql.DataFrame
import scala.collection.mutable.ListBuffer
def toCosmosDBVertices(dfVertices: DataFrame, labelColumn: String, partitionKey: String = "") : DataFrame = {
val dfResult = dfVertices.withColumn("id", udfUrlEncode($"id"))
var columns = ListBuffer("id", labelColumn)
if (!partitionKey.isEmpty()) {
columns += partitionKey
}
columns ++= dfResult.columns.filterNot(columns.contains(_))
.map(x => s"""nvl2($x, array(named_struct("id", uuid(), "_value", $x)), NULL) AS $x""")
dfResult.selectExpr(columns:_*).withColumnRenamed(labelColumn, "label")
}
// COMMAND ----------
val cosmosDbVertices = toCosmosDBVertices(g.vertices, "entity")
display(cosmosDbVertices)
// COMMAND ----------
import org.apache.spark.sql.functions.{concat_ws, col}
def toCosmosDBEdges(g: GraphFrame, labelColumn: String, partitionKey: String = "") : DataFrame = {
var dfEdges = g.edges
if (!partitionKey.isEmpty()) {
dfEdges = dfEdges.alias("e")
.join(g.vertices.alias("sv"), $"e.src" === $"sv.id")
.join(g.vertices.alias("dv"), $"e.dst" === $"dv.id")
.selectExpr("e.*", "sv." + partitionKey, "dv." + partitionKey + " AS _sinkPartition")
}
dfEdges = dfEdges
.withColumn("id", udfUrlEncode(concat_ws("_", $"src", col(labelColumn), $"dst")))
.withColumn("_isEdge", lit(true))
.withColumn("_vertexId", udfUrlEncode($"src"))
.withColumn("_sink", udfUrlEncode($"dst"))
.withColumnRenamed(labelColumn, "label")
.drop("src", "dst")
dfEdges
}
// COMMAND ----------
val cosmosDbEdges = toCosmosDBEdges(g, "relationship")
display(cosmosDbEdges)
// COMMAND ----------
// MAGIC %md ## Make sure to use the [Cosmos DB https endpoint](https://docs.microsoft.com/en-us/azure/cosmos-db/how-to-use-regional-gremlin#portal-endpoint-discovery) and **NOT** the `wss://` endpoint
// COMMAND ----------
import com.microsoft.azure.cosmosdb.spark.config.Config
import com.microsoft.azure.cosmosdb.spark.schema._
import org.apache.spark.sql.SaveMode
val cosmosDBConfig = Config(Map(
"Endpoint" -> "https://<COSMOSDB_ENDPOINT>.documents.azure.com:443/",
"Masterkey" -> "<COSMOSDB_PRIMARYKEY>",
"Database" -> "<DATABASE>",
"Collection" -> "<COLLECTION>"
))
cosmosDbVertices.write.mode(SaveMode.Overwrite).cosmosDB(cosmosDBConfig)
cosmosDbEdges.write.mode(SaveMode.Overwrite).cosmosDB(cosmosDBConfig)