Source code

Download source archive ↓

upstream/piflow-bundle/src/main/scala/cn/piflow/bundle/graphx/LabelPropagation.scala

1package cn.piflow.bundle.graphx
2
3import cn.piflow.{JobContext, JobInputStream, JobOutputStream, ProcessContext}
4import cn.piflow.conf.{ConfigurableStop, Port, StopGroup}
5import cn.piflow.conf.bean.PropertyDescriptor
6import cn.piflow.conf.util.{ImageUtil, MapUtil}
7import org.apache.spark.graphx._
8import org.apache.spark.graphx.lib.LabelPropagation
9import org.apache.spark.sql.SparkSession
10
11class LabelPropagation extends ConfigurableStop {
12
13  val authorEmail: String = "06whuxx@163.com"
14  val description: String = "Compute sub graphs"
15  var edgePortIn : String = "edgesIn"
16  var vertexPortIn : String = "vertexIn"
17  val inportList: List[String] = List(edgePortIn,vertexPortIn)
18
19  var edgePortOut : String = "edgesOut"
20  var vertexPortOut : String = "vertexOut"
21  val outportList: List[String] = List(edgePortOut,vertexPortOut)
22
23  var maxIter:String = _
24
25  def perform(in: JobInputStream, out: JobOutputStream, pec: JobContext): Unit = {
26    val spark = pec.get[SparkSession]()
27    val sc=spark.sparkContext
28    val edge=in.read(edgePortIn).asInstanceOf[EdgeRDD[Int]]
29    val vertex=in.read(vertexPortIn).asInstanceOf[VertexRDD[Int]]
30    val graph=Graph(vertex,edge)
31
32    var maxIterValue:Int=50
33    if(maxIter!=""){
34      maxIterValue=maxIter.toInt
35    }
36
37    val res=LabelPropagation.run(graph,maxIterValue)
38
39    import spark.sqlContext.implicits._
40    out.write(edgePortOut,res.edges.toDF())
41    out.write(vertexPortOut,res.vertices.toDF())
42  }
43
44  def initialize(ctx: ProcessContext): Unit = {
45
46  }
47
48  def setProperties(map : Map[String, Any]): Unit = {
49    maxIter = MapUtil.get(map,"maxIter").asInstanceOf[String]
50  }
51
52  override def getPropertyDescriptor(): List[PropertyDescriptor] = {
53    var descriptor : List[PropertyDescriptor] = List()
54    val maxIter = new PropertyDescriptor()
55      .name("maxIter")
56      .displayName("MAX_ITER")
57      .defaultValue("")
58      .allowableValues(Set(""))
59      .required(false)
60      .example("20")
61    descriptor = maxIter :: descriptor
62    descriptor
63  }
64
65  override def getIcon(): Array[Byte] = {
66    ImageUtil.getImage("icon/graphx/LabelPropagation.png")
67  }
68
69  override def getGroup(): List[String] = {
70    List(StopGroup.GraphX)
71  }
72
73}