upstream/piflow-bundle/src/main/scala/cn/piflow/bundle/generic/excel/ExcelWrite.scala
1package cn.piflow.bundle.generic.excel
2
3import cn.piflow.conf.bean.PropertyDescriptor
4import cn.piflow.conf.util.{ImageUtil, MapUtil}
5import cn.piflow.conf.{ConfigurableStop, Port, StopGroup}
6import cn.piflow.{JobContext, JobInputStream, JobOutputStream, ProcessContext}
7
8class ExcelWrite extends ConfigurableStop{
9 val authorEmail: String = "ygang@cnic.cn"
10 val description: String = "Write a DataFrame to an Excel file"
11 val inportList: List[String] = List(Port.DefaultPort)
12 val outportList: List[String] = List(Port.DefaultPort)
13
14 var filePath: String = _
15 var header: String = _
16 var dataAddress: String = _
17 var saveMode: String = _
18
19 override def perform(in: JobInputStream, out: JobOutputStream, pec: JobContext): Unit = {
20 val df = in.read()
21 df.write
22 .format("com.crealytics.spark.excel")
23 .option("dataAddress",dataAddress)
24 // .option("timestampFormat", timestampFormat)
25 .option("header", header)
26 .mode(saveMode)
27 .save(filePath)
28 }
29
30 override def setProperties(map: Map[String, Any]): Unit = {
31 filePath = MapUtil.get(map,"filePath").asInstanceOf[String]
32 header = MapUtil.get(map,"header").asInstanceOf[String]
33 dataAddress = MapUtil.get(map,"dataAddress").asInstanceOf[String]
34 saveMode = MapUtil.get(map,"saveMode").asInstanceOf[String]
35 }
36
37 override def getPropertyDescriptor(): List[PropertyDescriptor] = {
38 var descriptor : List[PropertyDescriptor] = List()
39
40 val filePath = new PropertyDescriptor()
41 .name("filePath")
42 .displayName("FilePath")
43 .description("The path of excel file")
44 .defaultValue("")
45 .required(true)
46 .example("/test/test.xlsx")
47 descriptor = filePath :: descriptor
48
49 val header = new PropertyDescriptor()
50 .name("header")
51 .displayName("Header")
52 .description("Whether the excel file has a header")
53 .defaultValue("true")
54 .allowableValues(Set("true","false"))
55 .required(true)
56 .example("true")
57 descriptor = header :: descriptor
58
59 val dataAddress = new PropertyDescriptor()
60 .name("dataAddress")
61 .displayName("DataAddress")
62 .description("The location of data to read or write can be specified with the dataAddress option, Currently the following address styles are supported:" +
63 "A1: Start cell of the data. Reading will return all rows below and all columns to the right. Writing will start here and use as many columns and rows as required.\n" +
64 "'My Sheet'!A1: Same as above, but with a specific sheet.\n" +
65 "A1:F35: Cell range of data. Reading will return only rows and columns in the specified range. Writing will start in the first cell (B3 in this example) " +
66 "and use only the specified columns and rows. If there are more rows or columns in the DataFrame to write, they will be truncated. Make sure this is what you want.\n" +
67 "'My Sheet'!A1:F35: Same as above, but with a specific sheet.")
68 .defaultValue("A1")
69 .required(true)
70 .example("A1")
71 descriptor = dataAddress :: descriptor
72
73 val saveMode = new PropertyDescriptor()
74 .name("saveMode")
75 .displayName("saveMode")
76 .description("default: overwrite")
77 .defaultValue("overwrite")
78 .allowableValues(Set("append","overwrite"))
79 .required(true)
80 .example("overwrite")
81 descriptor = saveMode :: descriptor
82
83 descriptor
84 }
85
86 override def getIcon(): Array[Byte] = {
87 ImageUtil.getImage("icon/excel/excelParse.png",this.getClass.getName)
88 }
89
90 override def getGroup(): List[String] = {
91 List(StopGroup.ExcelGroup)
92 }
93
94 override def initialize(ctx: ProcessContext): Unit = {
95
96 }
97
98}