Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/main/scala/org/graphframes/GraphFrame.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ import java.util.Random
import scala.reflect.runtime.universe.TypeTag

import org.apache.spark.graphx.{Edge, Graph}
import org.apache.spark.sql.SQLHelpers._
import org.apache.spark.sql._
import org.apache.spark.sql.functions.{array, broadcast, col, count, explode, struct, udf}
import org.apache.spark.sql.functions.{array, broadcast, col, count, explode, struct, udf, monotonically_increasing_id}
import org.apache.spark.sql.types._
import org.apache.spark.storage.StorageLevel

Expand Down Expand Up @@ -466,8 +465,15 @@ class GraphFrame private(
indexedVertices.select(
col(ATTR + "." + ID).cast("long").as(LONG_ID), col(ATTR + "." + ID).as(ID), col(ATTR))
} else {
val indexedVertices = zipWithUniqueId(vertices)
indexedVertices.select(col("uniq_id").as(LONG_ID), col("row." + ID).as(ID), col("row").as(ATTR))
val withLongIds = vertices.select(ID)
.repartition(col(ID))
.distinct()
.sortWithinPartitions(ID)
.withColumn(LONG_ID, monotonically_increasing_id())
.persist(StorageLevel.MEMORY_AND_DISK)
vertices.select(col(ID), nestAsCol(vertices, ATTR))
.join(withLongIds, ID)
.select(LONG_ID, ID, ATTR)
}
}

Expand Down
15 changes: 0 additions & 15 deletions src/main/spark-1.x/org/apache/spark/sql/SQLHelpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,6 @@ object SQLHelpers {

def expr(e: String): Column = functions.expr(e)

/**
* Appends each record with a unique ID (uniq_id) and groups existing fields under column "row".
* This is a workaround for SPARK-9020 and SPARK-13473.
*/
def zipWithUniqueId(df: DataFrame): DataFrame = {
val sqlContext = df.sqlContext
val schema = df.schema
val rdd = df.rdd.zipWithUniqueId().map { case (row, id) =>
Row(row, id)
}
val outputSchema = StructType(Seq(
StructField("row", schema, false), StructField("uniq_id", LongType, false)))
sqlContext.createDataFrame(rdd, outputSchema)
}

def callUDF(f: Function1[_, _], returnType: DataType, arg1: Column): Column = {
org.apache.spark.sql.functions.callUDF(f, returnType, arg1)
}
Expand Down
15 changes: 0 additions & 15 deletions src/main/spark-2.x/org/apache/spark/sql/SQLHelpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,6 @@ object SQLHelpers {

def expr(e: String): Column = functions.expr(e)

/**
* Appends each record with a unique ID (uniq_id) and groups existing fields under column "row".
* This is a workaround for SPARK-9020 and SPARK-13473.
*/
def zipWithUniqueId(df: DataFrame): DataFrame = {
val sqlContext = df.sqlContext
val schema = df.schema
val rdd = df.rdd.zipWithUniqueId().map { case (row, id) =>
Row(row, id)
}
val outputSchema = StructType(Seq(
StructField("row", schema, false), StructField("uniq_id", LongType, false)))
sqlContext.createDataFrame(rdd, outputSchema)
}

def callUDF(f: Function1[_, _], returnType: DataType, arg1: Column): Column = {
val u = udf(f, returnType)
u(arg1)
Expand Down