-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSparkUtils.scala
540 lines (479 loc) · 17.5 KB
/
SparkUtils.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*
* Copyright 2022 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package za.co.absa.pramen.core.utils
import com.fasterxml.jackson.databind.ObjectMapper
import org.apache.hadoop.fs.{Path, PathFilter}
import org.apache.spark.sql.catalyst.ScalaReflection
import org.apache.spark.sql.expressions.UserDefinedFunction
import org.apache.spark.sql.functions._
import org.apache.spark.sql.types._
import org.apache.spark.sql.{DataFrame, SparkSession}
import org.slf4j.LoggerFactory
import za.co.absa.pramen.api.FieldChange
import za.co.absa.pramen.api.jobdef.TransformExpression
import za.co.absa.pramen.core.expr.DateExprEvaluator
import java.io.ByteArrayOutputStream
import java.time.format.DateTimeFormatter
import java.time.{Instant, LocalDate}
import scala.collection.mutable.ArrayBuffer
import scala.reflect.runtime.universe._
import scala.util.{Failure, Success, Try}
object SparkUtils {
private val log = LoggerFactory.getLogger(this.getClass)
val MAX_LENGTH_METADATA_KEY = "maxLength"
val COMMENT_METADATA_KEY = "comment"
/** Get Spark StructType from a case class. */
def getStructType[T: TypeTag]: StructType = ScalaReflection.schemaFor[T].dataType.asInstanceOf[StructType]
def dataFrameToJson(df: DataFrame): String = {
prettyJSON(df.toJSON
.collect()
.mkString("[", ",", "]"))
}
// This is taken from
// https://github.com/AbsaOSS/cobrix/blob/master/spark-cobol/src/main/scala/za/co/absa/cobrix/spark/cobol/utils/SparkUtils.scala */
def convertDataFrameToPrettyJSON(df: DataFrame, takeN: Int = 0): String = {
val collected = if (takeN <= 0) {
df.toJSON.collect().mkString("\n")
} else {
df.toJSON.take(takeN).mkString("\n")
}
val json = "[" + "}\n".r.replaceAllIn(collected, "},\n") + "]"
prettyJSON(json)
}
def prettyJSON(jsonIn: String): String = {
val mapper = new ObjectMapper()
val jsonUnindented = mapper.readValue(jsonIn, classOf[Any])
val indented = mapper.writerWithDefaultPrettyPrinter.writeValueAsString(jsonUnindented)
indented.replace("\r\n", "\n")
}
/**
* Renames space characters in column names.
*
* This can be potentially improved by adding support for other special characters
*
* @param df A dataframe to sanitize columns of
* @param characters A set of characters considered special
*/
def sanitizeDfColumns(df: DataFrame, characters: String): DataFrame = {
def replaceSpecialChars(s: String): String = {
s.map(c => if (characters.contains(c)) '_' else c)
}
def removeTablePrefix(s: String): String = {
if (s.contains('.')) {
s.split('.').drop(1).mkString(".")
} else {
s
}
}
val fieldsToSelect = df.schema.fields.map(field => {
val srcName = field.name
val trgName = replaceSpecialChars(removeTablePrefix(srcName))
if (srcName != trgName) {
log.info(s"Renamed column: $srcName -> $trgName")
col(s"`$srcName`").as(trgName)
} else {
col(srcName)
}
})
df.select(fieldsToSelect: _*)
}
/**
* Converts JSON-encoded Spark schema to the schema type `StructType`.
*/
def schemaFromJson(json: String): Option[StructType] = {
Try {
DataType.fromJson(json).asInstanceOf[StructType]
} match {
case Success(schema) => Option(schema)
case Failure(e) =>
log.error(s"Failed to parse schema from JSON: $json", e)
None
}
}
/**
* Compares 2 schemas.
*/
def compareSchemas(schema1: StructType, schema2: StructType): List[FieldChange] = {
def dataTypeToString(dt: DataType, metadata: Metadata): String = {
val maxLength = getLengthFromMetadata(metadata).getOrElse(0)
dt match {
case _: StructType | _: ArrayType => dt.simpleString
case _: StringType if maxLength > 0 => s"varchar($maxLength)"
case _ => dt.typeName
}
}
val fields1 = schema1.fields.map(f => (f.name, f)).toMap
val fields2 = schema2.fields.map(f => (f.name, f)).toMap
val newColumns: Array[FieldChange] = schema2.fields
.filter(f => !fields1.contains(f.name))
.map(f => FieldChange.NewField(f.name, dataTypeToString(f.dataType, f.metadata)))
val deletedColumns: Array[FieldChange] = schema1.fields
.filter(f => !fields2.contains(f.name))
.map(f => FieldChange.DeletedField(f.name, dataTypeToString(f.dataType, f.metadata)))
val changedType: Array[FieldChange] = schema1.fields
.filter(f => fields2.contains(f.name))
.flatMap(f1 => {
val dt1 = dataTypeToString(f1.dataType, f1.metadata)
val f2 = fields2(f1.name)
val dt2 = dataTypeToString(f2.dataType, f2.metadata)
if (dt1 == dt2) {
Seq.empty[FieldChange]
} else {
Seq(FieldChange.ChangedType(f1.name, dt1, dt2))
}
})
(newColumns ++ deletedColumns ++ changedType).toList
}
/**
* Applies transformations as custom Spark expression to the specified dataframe.
*
* @param df The input dataframe
* @param transformations The list of transformations
* @return The dataframe with all transformations applied
*/
def applyTransformations(df: DataFrame, transformations: Seq[TransformExpression]): DataFrame = {
transformations.foldLeft(df)((accDf, tf) => {
(tf.expression, tf.comment) match {
case (Some(expression), _) if expression.isEmpty || expression.trim.equalsIgnoreCase("drop") =>
log.info(s"Dropping: ${tf.column}")
accDf.drop(tf.column)
case (Some(expression), Some(comment)) =>
log.info(s"Applying: ${tf.column} <- $expression ($comment)")
val metadata = new MetadataBuilder()
metadata.putString("comment", comment)
accDf.withColumn(tf.column, expr(expression).as(tf.column, metadata.build()))
case (Some(expression), None) =>
log.info(s"Applying: ${tf.column} <- $expression")
accDf.withColumn(tf.column, expr(expression))
case (None, Some(comment)) =>
log.debug(s"Adding comment '$comment' to ${tf.column}")
val metadata = new MetadataBuilder()
metadata.putString("comment", comment)
accDf.withColumn(tf.column, col(tf.column).as(tf.column, metadata.build()))
case (None, None) =>
log.info(s"Dropping: ${tf.column}")
accDf.drop(tf.column)
}
})
}
def applyFilters(df: DataFrame, filters: Seq[String], infoDate: LocalDate, dateFrom: LocalDate, dateTo: LocalDate): DataFrame = {
filters.foldLeft(df)((df, filter) => {
val exprEvaluator = new DateExprEvaluator()
exprEvaluator.setValue("dateFrom", dateFrom)
exprEvaluator.setValue("dateTo", dateTo)
exprEvaluator.setValue("date", infoDate)
val withInfoDate = filter.replaceAll("@infoDate", s"date'${infoDate.toString}'")
val actualFilter = StringUtils.replaceFormattedDateExpression(withInfoDate, exprEvaluator)
log.info(s"Applying filter: $actualFilter")
df.filter(expr(actualFilter))
})
}
/**
* Transforms the schema to the format compatible with Hive-like catalogs.
* - Removes non-nullable flag since it is not compatible with catalogs
* - Switches from string to varchar(n) when the maximum field length is known
*
* @param schema The input schema.
* @return The transformed schema.
*/
def transformSchemaForCatalog(schema: StructType): StructType = {
def transformField(field: StructField): StructField = {
field.dataType match {
case struct: StructType => StructField(field.name, transformStruct(struct), nullable = true, field.metadata)
case arr: ArrayType => StructField(field.name, transformArray(arr, field), nullable = true, field.metadata)
case dataType: DataType => StructField(field.name, transformPrimitive(dataType, field), nullable = true, field.metadata)
}
}
def transformPrimitive(dataType: DataType, field: StructField): DataType = {
dataType match {
case _: StringType =>
getLengthFromMetadata(field.metadata) match {
case Some(n) => VarcharType(n)
case None => StringType
}
case _ =>
dataType
}
}
def transformArray(arr: ArrayType, field: StructField): ArrayType = {
arr.elementType match {
case struct: StructType => ArrayType(transformStruct(struct), arr.containsNull)
case arr: ArrayType => ArrayType(transformArray(arr, field))
case dataType: DataType => ArrayType(transformPrimitive(dataType, field), arr.containsNull)
}
}
def transformStruct(struct: StructType): StructType = {
StructType(struct.fields.map(transformField))
}
transformStruct(schema)
}
def getLengthFromMetadata(metadata: Metadata): Option[Int] = {
if (metadata.contains(MAX_LENGTH_METADATA_KEY)) {
val try1 = Try {
val length = metadata.getLong(MAX_LENGTH_METADATA_KEY).toInt
Option(length)
}
val try2 = if (try1.isFailure) {
Try {
val length = metadata.getString(MAX_LENGTH_METADATA_KEY).toInt
Option(length)
}
} else {
try1
}
try2.getOrElse(None)
} else {
None
}
}
/**
* Removes metadata of nested fields to make DDL compatible with some Hive-like catalogs.
* In addition, removes the nullability flag for all fields.
*
* This method is usually applied to make schemas comparable when reading a table from a data catalog.
*
* @param schema The input schema.
* @return The transformed schema.
*/
def removeNestedMetadata(schema: StructType): StructType = {
def transformRootField(field: StructField): StructField = {
field.dataType match {
case struct: StructType => StructField(field.name, transformStruct(struct), nullable = true, field.metadata)
case arr: ArrayType => StructField(field.name, transformArray(arr), nullable = true, field.metadata)
case _: DataType => StructField(field.name, field.dataType, nullable = true, field.metadata)
}
}
def transformNestedField(field: StructField): StructField = {
field.dataType match {
case struct: StructType => StructField(field.name, transformStruct(struct), nullable = true)
case arr: ArrayType => StructField(field.name, transformArray(arr), nullable = true)
case dataType: DataType => StructField(field.name, dataType, nullable = true)
}
}
def transformStruct(struct: StructType): StructType = {
StructType(struct.fields.map(transformNestedField))
}
def transformArray(array: ArrayType): ArrayType = {
array.elementType match {
case struct: StructType => ArrayType(transformStruct(struct), containsNull = true)
case arr: ArrayType => ArrayType(transformArray(arr), containsNull = true)
case dataType: DataType => ArrayType(dataType, containsNull = true)
}
}
StructType(schema.fields.map(transformRootField))
}
def hasDataInPartition(infoDate: LocalDate,
infoDateColumn: String,
infoDateFormat: String,
basePath: String)(implicit spark: SparkSession): Boolean = {
val path = getPartitionPath(infoDate, infoDateColumn, infoDateFormat, basePath)
val fs = path.getFileSystem(spark.sparkContext.hadoopConfiguration)
val hasPartition = fs.exists(path)
if (hasPartition) {
val pathFilter = new PathFilter {
override def accept(path: Path): Boolean = {
val name = path.getName
!name.startsWith("_") && !name.startsWith(".")
}
}
val filesInTheFolder = fs.globStatus(new Path(path, "*"), pathFilter).nonEmpty
if (filesInTheFolder) {
true
} else {
log.warn(s"No content in: $path")
false
}
} else {
log.warn(s"No partition: $path")
false
}
}
def getPartitionPath(infoDate: LocalDate,
infoDateColumn: String,
infoDateFormat: String,
basePath: String): Path = {
val dateFormatter = DateTimeFormatter.ofPattern(infoDateFormat)
val partition = s"$infoDateColumn=${dateFormatter.format(infoDate)}"
new Path(basePath, partition)
}
def addProcessingTimestamp(df: DataFrame, timestampCol: String): DataFrame = {
if (df.schema.exists(_.name == timestampCol)) {
log.warn(s"Column $timestampCol already exists. Won't add it.")
df
} else {
val u = getActualProcessingTimeUdf
df.withColumn(timestampCol, u(unix_timestamp()).cast(TimestampType))
}
}
/**
* This helper method returns output of `df.show()` to a string
*
* @param df a dataframe to show
* @param numRows the maximum number of rows to show
* @return
*/
def showString(df: DataFrame, numRows: Int = 20): String = {
val outCapture = new ByteArrayOutputStream
Console.withOut(outCapture) {
df.show(numRows, truncate = false)
}
new String(outCapture.toByteArray).replace("\r\n", "\n")
}
def collectTable(df: DataFrame, maxRecords: Int = 200): Array[Array[String]] = {
val collected = if (maxRecords > 0) {
df.take(maxRecords)
} else {
df.collect()
}
val headers = df.schema.fields.map(_.name)
val rows = collected.map(row => {
val arr = new ArrayBuffer[String]
var i = 0
val len = row.length
while (i < len) {
val v = row.get(i)
val vs = if (v == null) "null" else v.toString
arr.append(vs)
i += 1
}
arr.toArray[String]
})
headers +: rows
}
/**
* Escapes all columns in Spark DDL. Used to make sure column names are not reserved words.
*
* Example:
* {{{
* Id INT NOT NULL,Name STRING,`System Date` ARRAY<STRING>
* }}}
* becomes
* {{{
* `Id` INT NOT NULL,`Name` STRING,`System Date` ARRAY<STRING>
* }}}
*
* @param sparkDdlExpr An expression as a Spark DDL (df.schema.toDDL)
* @return The same DDL with all column names escaped.
*/
def escapeColumnsSparkDDL(sparkDdlExpr: String): String = {
val STATE_WHITESPACE_OR_ID = 0
val STATE_POSSIBLE_ID = 1
val STATE_DATA_TYPE = 2
val STATE_PARENTHESIS = 3
val STATE_QUOTES = 4
val STATE_ESCAPE = 5
val output = new StringBuilder()
val token = new StringBuilder()
var state = 0
var depth = 0
var i = 0
val len = sparkDdlExpr.length
while (i < len) {
val c = sparkDdlExpr(i)
if (state == STATE_WHITESPACE_OR_ID) {
if (c == '<' || c == ':') {
output.append(s"$token")
token.clear()
state = STATE_DATA_TYPE
} else if (c == ' ') {
token.append(c)
} else {
output.append(s"$token")
token.clear()
state = STATE_POSSIBLE_ID
}
}
if (state == 1) {
if (c == '(') {
depth = 1
state = STATE_PARENTHESIS
token.append(c)
} else if (c == '\'') {
state = STATE_QUOTES
token.append(c)
} else if (c == '\\') {
state = STATE_ESCAPE
token.append(c)
} else if (c == ' ' || c == ':') {
state = STATE_DATA_TYPE
val tokenStr = token.toString().trim
if (tokenStr.isEmpty) {
output.append(s"$tokenStr$c")
} else if (tokenStr.head == '`') {
output.append(s"$tokenStr$c")
} else {
output.append(s"`$tokenStr`$c")
}
token.clear()
} else if (c == ',' || c == '<') {
output.append(s"$token$c")
token.clear()
state = STATE_WHITESPACE_OR_ID
} else if (c == '>') {
output.append(s"$token$c")
token.clear()
state = STATE_DATA_TYPE
} else {
token.append(c)
}
} else if (state == STATE_DATA_TYPE) {
if (c == '(') {
depth = 1
state = STATE_PARENTHESIS
} else if (c == '\'') {
state = STATE_QUOTES
} else if (c == '\\') {
state = STATE_ESCAPE
}
if (c == ',') {
state = STATE_WHITESPACE_OR_ID
output.append(s"$token$c")
token.clear()
} else if (c == '<') {
state = STATE_WHITESPACE_OR_ID
output.append(s"$token$c")
token.clear()
} else {
token.append(c)
}
} else if (state == STATE_PARENTHESIS) {
if (c == ')') {
depth -= 1
if (depth == 0)
state = STATE_DATA_TYPE
} else if (c == '(')
depth += 1
token.append(c)
} else if (state == STATE_QUOTES) {
if (c == '\'') {
state = STATE_DATA_TYPE
}
token.append(c)
} else if (state == STATE_ESCAPE) {
state = STATE_DATA_TYPE
token.append(c)
}
i += 1
}
if (token.nonEmpty) {
output.append(token.toString())
}
output.toString()
}
private def getActualProcessingTimeUdf: UserDefinedFunction = {
udf((_: Long) => Instant.now().getEpochSecond)
}
}