Source code

Download source archive ↓

upstream/piflow-bundle/src/main/scala/cn/piflow/bundle/generic/visualization/Histogram.scala

1package cn.piflow.bundle.generic.visualization
2
3import cn.piflow.conf.bean.PropertyDescriptor
4import cn.piflow.conf.util.{ImageUtil, MapUtil}
5import cn.piflow.conf.{ConfigurableVisualizationStop, Port, StopGroup, VisualizationType}
6import cn.piflow.{JobContext, JobInputStream, JobOutputStream, ProcessContext}
7import org.apache.spark.sql.SparkSession
8
9class Histogram extends ConfigurableVisualizationStop{
10  override val authorEmail: String = "xjzhu@cnic.cn"
11  override val description: String = "Show data with histogram. " +
12    "The ordinate is represented by customizedProperties, " +
13    "the key of customizedProperty is the dimentsion, and the value of customizedProperty is the operation for the dimension, such as COUNT/SUM/AVG/MAX/MIN."
14  override val inportList: List[String] = List(Port.DefaultPort)
15  override val outportList: List[String] = List(Port.DefaultPort)
16
17
18  var abscissa:String =_
19
20  override var visualizationType: String = VisualizationType.Histogram
21  override val isCustomized: Boolean = true
22  override val customizedAllowValue: List[String] = List("COUNT","SUM","AVG","MAX","MIN")
23
24  override def setProperties(map: Map[String, Any]): Unit = {
25    abscissa=MapUtil.get(map,key="abscissa").asInstanceOf[String]
26    //dimension=MapUtil.get(map,key="dimension").asInstanceOf[String]
27  }
28
29  override def getPropertyDescriptor(): List[PropertyDescriptor] = {
30    var descriptor : List[PropertyDescriptor] = List()
31    val abscissa = new PropertyDescriptor()
32      .name("abscissa")
33      .displayName("Abscissa")
34      .description("The abscissa  of histogram")
35      .defaultValue("")
36      .example("year")
37      .required(true)
38    /*val dimension = new PropertyDescriptor()
39      .name("dimension")
40      .displayName("Dimension")
41      .description("The dimension of line chart, multi demensions are separated by commas")
42      .defaultValue("")
43      .required(true)*/
44    descriptor = abscissa :: descriptor
45    //descriptor = dimension :: descriptor
46    descriptor
47  }
48
49  override def getIcon(): Array[Byte] = {
50    ImageUtil.getImage("icon/visualization/histogram.png")
51  }
52
53  override def getGroup(): List[String] = {
54    List(StopGroup.Visualization)
55  }
56
57  override def initialize(ctx: ProcessContext): Unit = {}
58
59  override def perform(in: JobInputStream, out: JobOutputStream, pec: JobContext): Unit = {
60    val spark = pec.get[SparkSession]()
61    val sqlContext=spark.sqlContext
62    val dataFrame = in.read()
63    dataFrame.createOrReplaceTempView("Histoqram")
64
65    if(this.customizedProperties != null || this.customizedProperties.size != 0){
66
67      println("dimension is " + this.customizedProperties.keySet.mkString(",") + "!!!!!!!!!!!!!!!")
68
69      var dimensionActionArray = List[String]()
70      val it = this.customizedProperties.keySet.iterator
71      while (it.hasNext){
72        val dimention = it.next()
73        val action = MapUtil.get(this.customizedProperties,dimention).asInstanceOf[String]
74        val dimentionAction = action + "(" + dimention + ") as " + dimention + "_" + action
75        dimensionActionArray = dimensionActionArray :+ dimentionAction
76      }
77
78      val sqlText = "select " + abscissa + "," + dimensionActionArray.mkString(",") + " from Histoqram group by " + abscissa;
79      println("Histoqram Sql: " + sqlText)
80      val lineChartDF = spark.sql(sqlText)
81      out.write(lineChartDF.repartition(1))
82    }else{
83      out.write(dataFrame)
84    }
85  }
86}