|
| 1 | +/* |
| 2 | + * Copyright 2022 ABSA Group Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package za.co.absa.pramen.core |
| 18 | + |
| 19 | +import org.apache.spark.sql.types._ |
| 20 | +import org.apache.spark.sql.{DataFrame, Row, SparkSession} |
| 21 | + |
| 22 | +object NestedDataFrameFactory { |
| 23 | + private val testCaseSchema = StructType( |
| 24 | + Array( |
| 25 | + StructField("id", LongType), |
| 26 | + StructField("key1", LongType), |
| 27 | + StructField("key2", LongType), |
| 28 | + StructField("struct1", StructType(Array( |
| 29 | + StructField("key3", IntegerType), |
| 30 | + StructField("key4", IntegerType) |
| 31 | + ))), |
| 32 | + StructField("struct2", StructType(Array( |
| 33 | + StructField("inner1", StructType(Array( |
| 34 | + StructField("key5", LongType), |
| 35 | + StructField("key6", LongType), |
| 36 | + StructField("skey1", StringType) |
| 37 | + ))) |
| 38 | + ))), |
| 39 | + StructField("struct3", StructType(Array( |
| 40 | + StructField("inner3", StructType(Array( |
| 41 | + StructField("array3", ArrayType(StructType(Array( |
| 42 | + StructField("a1", LongType), |
| 43 | + StructField("a2", LongType), |
| 44 | + StructField("a3", StringType) |
| 45 | + )))) |
| 46 | + ))) |
| 47 | + ))), |
| 48 | + StructField("array1", ArrayType(StructType(Array( |
| 49 | + StructField("key7", LongType), |
| 50 | + StructField("key8", LongType), |
| 51 | + StructField("skey2", StringType) |
| 52 | + )))), |
| 53 | + StructField("array2", ArrayType(StructType(Array( |
| 54 | + StructField("key2", LongType), |
| 55 | + StructField("inner2", ArrayType(StructType(Array( |
| 56 | + StructField("key9", LongType), |
| 57 | + StructField("key10", LongType), |
| 58 | + StructField("struct3", StructType(Array( |
| 59 | + StructField("k1", IntegerType), |
| 60 | + StructField("k2", IntegerType) |
| 61 | + ))) |
| 62 | + )))) |
| 63 | + )))) |
| 64 | + )) |
| 65 | + |
| 66 | + private val testCaseWithMapSchema = new StructType() |
| 67 | + .add("name", StringType) |
| 68 | + .add("addresses", ArrayType(new StructType() |
| 69 | + .add("city", StringType) |
| 70 | + .add("state", StringType))) |
| 71 | + .add("properties", MapType(StringType, StringType)) |
| 72 | + |
| 73 | + private val tstCaseWithMapData = Seq( |
| 74 | + Row("John", List(Row("Newark", "NY"), Row("Brooklyn", "NY")), Map("hair" -> "black", "eyes" -> "brown", "height" -> "178")), |
| 75 | + Row("Kate", List(Row("San Jose", "CA"), Row("Sandiago", "CA")), Map("hair" -> "brown", "eyes" -> "black", "height" -> "165")), |
| 76 | + Row("William", List(Row("Las Vegas", "NV")), Map("hair" -> "red", "eye" -> "gray", "height" -> "185")), |
| 77 | + Row("Sarah", null, Map("hair" -> "blond", "eyes" -> "red", "height" -> "162")), |
| 78 | + Row("Michael", List(Row("Sacramento", "CA"), Row("San Diego", "CA")), Map("white" -> "black", "eyes" -> "black", "height" -> "180")) |
| 79 | + ) |
| 80 | + |
| 81 | + def getNestedTestCase(implicit spark: SparkSession): DataFrame = { |
| 82 | + spark.read |
| 83 | + .schema(testCaseSchema) |
| 84 | + .json(getClass.getResource("/test/nestedDf1.json").getPath) |
| 85 | + } |
| 86 | + |
| 87 | + def getMapTestCase(implicit spark: SparkSession): DataFrame = { |
| 88 | + spark.createDataFrame( |
| 89 | + spark.sparkContext.parallelize(tstCaseWithMapData), |
| 90 | + testCaseWithMapSchema |
| 91 | + ).orderBy("name") |
| 92 | + } |
| 93 | +} |
0 commit comments