-
Notifications
You must be signed in to change notification settings - Fork 3
/
SparkRandomDatasetGeneratorPlugin.scala
222 lines (196 loc) · 10.3 KB
/
SparkRandomDatasetGeneratorPlugin.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* Copyright (c) 2015 Alpine Data Labs
* All rights reserved.
*
* BSD 3-Clause License
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.alpine.plugin.samples.ver1_0
import com.alpine.plugin.core._
import com.alpine.plugin.core.datasource.OperatorDataSourceManager
import com.alpine.plugin.core.dialog.{OperatorDialog, SparkParameter}
import com.alpine.plugin.core.io._
import com.alpine.plugin.core.spark.{AlpineSparkEnvironment, AutoTunerOptions, SparkIOTypedPluginJob, SparkRuntimeWithIOTypedJob}
import com.alpine.plugin.core.utils.{HdfsCompressionType, HdfsParameterUtils, HdfsStorageFormatType, SparkParameterUtils}
class SparkRandomDatasetGeneratorSignature extends OperatorSignature[
SparkRandomDatasetGeneratorGUINode,
SparkRandomDatasetGeneratorRuntime] {
override def getMetadata: OperatorMetadata = new OperatorMetadata(
name = "Sample - Random Data Generator",
category = "Plugin Sample - Spark",
author = Some("Alpine Data"),
version = 1,
helpURL = None,
icon = None,
toolTipText = Some("Enter text to show as a tooltip for your operator here. This will appear when a user hovers " +
"over the operator’s name in the workflow editor. The best tooltips concisely describe the function" +
" of the operator and are no more than fifty words.")
)
}
object SparkRandomDatasetGeneratorConstants {
val doubleColumnsParam = "numDoubleColumns"
val intColumnsParam = "numIntColumns"
val stringColumnsParam = "numStringColumns"
val numRowsParam = "numRows"
def defineOutputSchema(numDoubleCols: Int,
numIntCols: Int,
numStringCols: Int
): TabularSchema = {
TabularSchema(
(1 to numDoubleCols).map(i => ColumnDef("DoubleCol" + i.toString, ColumnType.Double)) ++
(1 to numIntCols).map(i => ColumnDef("IntCol" + i.toString, ColumnType.Int)) ++
(1 to numStringCols).map(i => ColumnDef("StringCol" + i.toString, ColumnType.String))
)
}
}
class SparkRandomDatasetGeneratorGUINode
extends OperatorGUINode[IONone, HdfsTabularDataset] {
override def onPlacement(operatorDialog: OperatorDialog,
operatorDataSourceManager: OperatorDataSourceManager,
operatorSchemaManager: OperatorSchemaManager): Unit = {
operatorDialog.addIntegerBox(
SparkRandomDatasetGeneratorConstants.doubleColumnsParam,
"Number of double columns to generate.",
0,
Int.MaxValue,
1
)
operatorDialog.addIntegerBox(
SparkRandomDatasetGeneratorConstants.intColumnsParam,
"Number of integer columns to generate.",
0,
Int.MaxValue,
1
)
operatorDialog.addIntegerBox(
SparkRandomDatasetGeneratorConstants.stringColumnsParam,
"Number of string columns to generate.",
0,
Int.MaxValue,
1
)
operatorDialog.addIntegerBox(
SparkRandomDatasetGeneratorConstants.numRowsParam,
"Number of rows to generate.",
1,
Int.MaxValue,
1000
)
HdfsParameterUtils.addHdfsStorageAndCompressionParameters(operatorDialog, HdfsStorageFormatType.CSV, HdfsCompressionType.NoCompression)
HdfsParameterUtils.addStandardHdfsOutputParameters(operatorDialog)
SparkParameterUtils.addStandardSparkOptions(operatorDialog, List[SparkParameter]())
operatorSchemaManager.setOutputSchema(
TabularSchema(
Seq(
ColumnDef("DoubleCol1", ColumnType.Double),
ColumnDef("IntCol1", ColumnType.Int),
ColumnDef("StringCol1", ColumnType.String)
)
)
)
}
override def onInputOrParameterChange(inputSchemas: Map[String, TabularSchema],
params: OperatorParameters,
operatorSchemaManager: OperatorSchemaManager): OperatorStatus = {
val numDoubleCols = params.getIntValue(SparkRandomDatasetGeneratorConstants.doubleColumnsParam)
val numIntCols = params.getIntValue(SparkRandomDatasetGeneratorConstants.intColumnsParam)
val numStringCols = params.getIntValue(SparkRandomDatasetGeneratorConstants.stringColumnsParam)
val totalNumCols = numDoubleCols + numIntCols + numStringCols
if (totalNumCols <= 0) {
OperatorStatus(isValid = false, msg = "The total number of columns has to be greater than 0.")
} else {
// We have to re-define the output schema based on the number of column
// parameters. Additionally, we have to take into account the user
// potentially selecting different output formats.
val outputSchema = SparkRandomDatasetGeneratorConstants.defineOutputSchema(numDoubleCols,
numIntCols, numStringCols)
operatorSchemaManager.setOutputSchema(outputSchema)
OperatorStatus(isValid = true)
}
}
}
class SparkRandomDatasetGeneratorRuntime
extends SparkRuntimeWithIOTypedJob[RandomDatasetGeneratorJob, IONone, HdfsTabularDataset] {
/**
* We use this method to change how the Spark AutoTuner Works.
* By default the auto tuner spins up enough workers to read the input data into memory. We estimate
* the size of the input data in memory as the file size times some scalar, which can be
* set in this method.
* (See the documentation for the 'AutoTunerOptions' class for more details.)
*
* However, since this operator has no input data, we need to override this value so that
* it will correspond with the computational resources we need.
* There are many possible ways of doing this, one simple way, is just to assume that the
* operator requires more resources as the number of rows increases. (Note that this value
* will be reported in the "view status" feed of the alpine UI as the "size of input data in memory").
*/
override def getAutoTuningOptions(parameters: OperatorParameters, input: IONone): AutoTunerOptions = {
val cachedSizeMultiplier = parameters.getIntValue("numRows")/8000.0 //we will increase the desired cached size base on the number of rows (6000 rows => inputCachedSizeMultiplier = 1.0)
AutoTunerOptions(driverMemoryFraction = 0.5, //this doesn't require much work from the driver so we set this value to less than one.
inputCachedSizeMultiplier = cachedSizeMultiplier)
}
}
class RandomDatasetGeneratorJob extends SparkIOTypedPluginJob[IONone, HdfsTabularDataset] {
override def onExecution(alpineSparkEnvironment: AlpineSparkEnvironment,
input: IONone,
params: OperatorParameters,
listener: OperatorListener): HdfsTabularDataset = {
val sparkUtils = alpineSparkEnvironment.getSparkUtils
val numDoubleCols = params.getIntValue("numDoubleColumns")
val numIntCols = params.getIntValue("numIntColumns")
val numStringCols = params.getIntValue("numStringColumns")
val numRows = params.getIntValue("numRows")
val sparkContext = alpineSparkEnvironment.sparkSession.sparkContext
val numExecutors = sparkContext.getExecutorMemoryStatus.size
val numRowsPerPartition = math.ceil(numRows.toDouble / numExecutors.toDouble).toInt
val randomDataRdd = sparkContext.parallelize(Seq[Int](), numExecutors).mapPartitionsWithIndex {
(partitionId, _) => {
val rng = new scala.util.Random(partitionId)
(1 to numRowsPerPartition).map(
_ =>
org.apache.spark.sql.Row.fromSeq(
(1 to numDoubleCols).map(_ => rng.nextDouble()) ++
(1 to numIntCols).map(_ => rng.nextInt()) ++
(1 to numStringCols).map(
_ =>
rng.nextPrintableChar().toString +
rng.nextPrintableChar().toString +
rng.nextPrintableChar().toString +
rng.nextPrintableChar().toString +
rng.nextPrintableChar().toString
)
)
).iterator
}
}
val outputPath = HdfsParameterUtils.getOutputPath(params)
val overwrite = HdfsParameterUtils.getOverwriteParameterValue(params)
val storageFormat = HdfsParameterUtils.getHdfsStorageFormatType(params)
val compressionType = HdfsParameterUtils.getHdfsCompressionType(params)
//use our utils object to get the AlpineOutputSchema which we can then convert to Spark SQL with the
//the SparkRuntimeUtils class.
val outputSchema = SparkRandomDatasetGeneratorConstants.defineOutputSchema(numDoubleCols,
numIntCols, numStringCols)
val outputDF =
alpineSparkEnvironment.sparkSession.createDataFrame(randomDataRdd, sparkUtils.convertTabularSchemaToSparkSQLSchema(outputSchema))
//use sparkUtils to save the DataFrame and return the HdfsTabularDataset object
sparkUtils.saveDataFrame(
outputPath,
outputDF,
storageFormat,
overwrite,
Map[String, AnyRef](),
TSVAttributes.defaultCSV, //the default is a comma delimited file. But you may specify different delimiters or escape characters with this parameter
compressionType
)
}
}