|
| 1 | +package org.graphframes.examples |
| 2 | + |
| 3 | +import org.apache.spark.SparkConf |
| 4 | +import org.apache.spark.sql.SparkSession |
| 5 | +import org.apache.spark.sql.functions.col |
| 6 | +import org.apache.spark.sql.types.LongType |
| 7 | +import org.apache.spark.sql.types.StructField |
| 8 | +import org.apache.spark.sql.types.StructType |
| 9 | +import org.apache.spark.storage.StorageLevel |
| 10 | +import org.graphframes.GraphFrame |
| 11 | + |
| 12 | +import java.nio.file.Files |
| 13 | +import java.nio.file.Path |
| 14 | +import java.util.Properties |
| 15 | + |
| 16 | +object ConnectedComponentsLDBC { |
| 17 | + def main(args: Array[String]): Unit = { |
| 18 | + val benchmarkGraphName = args.headOption.getOrElse("kgs") |
| 19 | + val resourcesPath = Path.of(args.lift(1).getOrElse("/tmp/ldbc_graphalitics_datesets")) |
| 20 | + val caseRoot: Path = resourcesPath.resolve(benchmarkGraphName) |
| 21 | + |
| 22 | + val sparkConf = new SparkConf() |
| 23 | + .setMaster("local[*]") |
| 24 | + .setAppName("GraphFramesBenchmarks") |
| 25 | + .set("spark.sql.shuffle.partitions", s"${Runtime.getRuntime.availableProcessors() * 2}") |
| 26 | + .set("spark.serializer", "org.apache.spark.serializer.KryoSerializer") |
| 27 | + |
| 28 | + val spark = SparkSession.builder().config(sparkConf).getOrCreate() |
| 29 | + val context = spark.sparkContext |
| 30 | + context.setLogLevel("ERROR") |
| 31 | + context.setCheckpointDir("/tmp/graphframes-checkpoints") |
| 32 | + |
| 33 | + LDBCUtils.downloadLDBCIfNotExists(resourcesPath, benchmarkGraphName) |
| 34 | + |
| 35 | + val edges = spark.read |
| 36 | + .format("csv") |
| 37 | + .option("header", "false") |
| 38 | + .option("delimiter", " ") |
| 39 | + .schema(StructType(Seq(StructField("src", LongType), StructField("dst", LongType)))) |
| 40 | + .load(caseRoot.resolve(s"$benchmarkGraphName.e").toString) |
| 41 | + .persist(StorageLevel.MEMORY_AND_DISK_SER) |
| 42 | + println() |
| 43 | + println(s"Read edges: ${edges.count()}") |
| 44 | + |
| 45 | + val vertices = spark.read |
| 46 | + .format("csv") |
| 47 | + .option("header", "false") |
| 48 | + .schema(StructType(Seq(StructField("id", LongType)))) |
| 49 | + .load(caseRoot.resolve(s"$benchmarkGraphName.v").toString) |
| 50 | + .persist(StorageLevel.MEMORY_AND_DISK_SER) |
| 51 | + println(s"Read vertices: ${vertices.count()}") |
| 52 | + |
| 53 | + val graph = GraphFrame(vertices, edges) |
| 54 | + val props = new Properties() |
| 55 | + val stream = Files.newInputStream(caseRoot.resolve(s"$benchmarkGraphName.properties")) |
| 56 | + props.load(stream) |
| 57 | + stream.close() |
| 58 | + |
| 59 | + val expectedPath = caseRoot.resolve(s"$benchmarkGraphName-WCC") |
| 60 | + |
| 61 | + val expectedComponents = spark.read |
| 62 | + .format("csv") |
| 63 | + .option("header", "false") |
| 64 | + .option("delimiter", " ") |
| 65 | + .schema(StructType(Seq(StructField("id", LongType), StructField("wcomp", LongType)))) |
| 66 | + .load(expectedPath.toString) |
| 67 | + .toDF("id", "wcomp") |
| 68 | + .persist(StorageLevel.MEMORY_AND_DISK_SER) |
| 69 | + |
| 70 | + println(s"Expected components: ${expectedComponents.count()}") |
| 71 | + |
| 72 | + val start = System.currentTimeMillis() |
| 73 | + val results = graph.connectedComponents |
| 74 | + .setAlgorithm("graphframes") |
| 75 | + .setBroadcastThreshold(-1) |
| 76 | + .setUseLocalCheckpoints(true) |
| 77 | + .run() |
| 78 | + |
| 79 | + println(s"Connected components: ${results.count()}") |
| 80 | + |
| 81 | + val combined = results.join(expectedComponents, Seq("id"), "left") |
| 82 | + combined.show(10) |
| 83 | + |
| 84 | + val notMatchedRows = combined.filter(col("wcomp") =!= col("component")) |
| 85 | + println(s"Not matched rows count: ${notMatchedRows.count()}") |
| 86 | + notMatchedRows.show(20) |
| 87 | + |
| 88 | + val end = System.currentTimeMillis() |
| 89 | + println(s"Total time in seconds: ${(end - start) / 1000.0}") |
| 90 | + |
| 91 | + spark.stop() |
| 92 | + } |
| 93 | +} |
0 commit comments