|
| 1 | +/* Copyright (c) 2022 vesoft inc. All rights reserved. |
| 2 | + * |
| 3 | + * This source code is licensed under Apache 2.0 License. |
| 4 | + */ |
| 5 | + |
| 6 | +package com.vesoft.nebula.algorithm |
| 7 | + |
| 8 | +import com.vesoft.nebula.connector.connector.{NebulaDataFrameReader} |
| 9 | +import com.facebook.thrift.protocol.TCompactProtocol |
| 10 | +import com.vesoft.nebula.algorithm.config.{CcConfig, LPAConfig, LouvainConfig, PRConfig} |
| 11 | +import com.vesoft.nebula.algorithm.lib.{ |
| 12 | + ConnectedComponentsAlgo, |
| 13 | + LabelPropagationAlgo, |
| 14 | + LouvainAlgo, |
| 15 | + PageRankAlgo |
| 16 | +} |
| 17 | +import com.vesoft.nebula.connector.{NebulaConnectionConfig, ReadNebulaConfig} |
| 18 | +import org.apache.spark.SparkConf |
| 19 | +import org.apache.spark.sql.{DataFrame, SparkSession} |
| 20 | + |
| 21 | +object AlgoPerformanceTest { |
| 22 | + |
| 23 | + def main(args: Array[String]): Unit = { |
| 24 | + val sparkConf = new SparkConf() |
| 25 | + .set("spark.serializer", "org.apache.spark.serializer.KryoSerializer") |
| 26 | + .registerKryoClasses(Array[Class[_]](classOf[TCompactProtocol])) |
| 27 | + val spark = SparkSession |
| 28 | + .builder() |
| 29 | + .config(sparkConf) |
| 30 | + .getOrCreate() |
| 31 | + |
| 32 | + val df = readNebulaData(spark) |
| 33 | + lpa(spark, df) |
| 34 | + louvain(spark, df) |
| 35 | + pagerank(spark, df) |
| 36 | + wcc(spark, df) |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + def readNebulaData(spark: SparkSession): DataFrame = { |
| 41 | + val start = System.currentTimeMillis() |
| 42 | + val config = |
| 43 | + NebulaConnectionConfig |
| 44 | + .builder() |
| 45 | + .withMetaAddress("127.0.0.0.1:9559") |
| 46 | + .withTimeout(6000) |
| 47 | + .withConenctionRetry(2) |
| 48 | + .build() |
| 49 | + val nebulaReadEdgeConfig: ReadNebulaConfig = ReadNebulaConfig |
| 50 | + .builder() |
| 51 | + .withSpace("twitter") |
| 52 | + .withLabel("FOLLOW") |
| 53 | + .withNoColumn(true) |
| 54 | + .withLimit(20000) |
| 55 | + .withPartitionNum(120) |
| 56 | + .build() |
| 57 | + val df: DataFrame = |
| 58 | + spark.read.nebula(config, nebulaReadEdgeConfig).loadEdgesToDF() |
| 59 | + df.cache() |
| 60 | + df.count() |
| 61 | + println(s"read data cost time ${(System.currentTimeMillis() - start)}") |
| 62 | + df |
| 63 | + } |
| 64 | + |
| 65 | + def lpa(spark: SparkSession, df: DataFrame): Unit = { |
| 66 | + val start = System.currentTimeMillis() |
| 67 | + val lpaConfig = LPAConfig(10) |
| 68 | + val lpa = LabelPropagationAlgo.apply(spark, df, lpaConfig, false) |
| 69 | + lpa.write.csv("hdfs://127.0.0.1:9000/tmp/lpa") |
| 70 | + println(s"lpa compute and save cost ${System.currentTimeMillis() - start}") |
| 71 | + } |
| 72 | + |
| 73 | + def pagerank(spark: SparkSession, df: DataFrame): Unit = { |
| 74 | + val start = System.currentTimeMillis() |
| 75 | + val pageRankConfig = PRConfig(10, 0.85) |
| 76 | + val pr = PageRankAlgo.apply(spark, df, pageRankConfig, false) |
| 77 | + pr.write.csv("hdfs://127.0.0.1:9000/tmp/pagerank") |
| 78 | + println(s"pagerank compute and save cost ${System.currentTimeMillis() - start}") |
| 79 | + } |
| 80 | + |
| 81 | + def wcc(spark: SparkSession, df: DataFrame): Unit = { |
| 82 | + val start = System.currentTimeMillis() |
| 83 | + val ccConfig = CcConfig(20) |
| 84 | + val cc = ConnectedComponentsAlgo.apply(spark, df, ccConfig, false) |
| 85 | + cc.write.csv("hdfs://127.0.0.1:9000/tmp/wcc") |
| 86 | + println(s"wcc compute and save cost ${System.currentTimeMillis() - start}") |
| 87 | + } |
| 88 | + |
| 89 | + def louvain(spark: SparkSession, df: DataFrame): Unit = { |
| 90 | + val start = System.currentTimeMillis() |
| 91 | + val louvainConfig = LouvainConfig(10, 5, 0.5) |
| 92 | + val louvain = LouvainAlgo.apply(spark, df, louvainConfig, false) |
| 93 | + louvain.write.csv("hdfs://127.0.0.1:9000/tmp/louvain") |
| 94 | + println(s"louvain compute and save cost ${System.currentTimeMillis() - start}") |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments