Как проверить, есть ли в массиве столбцы в схеме? - PullRequest
0 голосов
/ 07 февраля 2020

У меня есть схема, и я хотел бы проверить массив, если в нем есть столбцы, прежде чем взорвать его. моя схема выглядит следующим образом:

 |-- CaseNumber: string (nullable = true)
 |-- Interactions: struct (nullable = true)
 |    |-- EmailInteractions: array (nullable = true)
 |    |    |-- element: struct (containsNull = true)
 |    |    |    |-- CreatedBy: string (nullable = true)
 |    |    |    |-- CreatedOn: string (nullable = true)
 |    |    |    |-- Direction: string (nullable = true)
 |    |-- PhoneInteractions: array (nullable = true)
 |    |    |-- element: string (containsNull = true)
 |    |-- WebInteractions: array (nullable = true)
 |    |    |-- element: string (containsNull = true)
 |-- EntityAction: string (nullable = true)

Я хотел бы проверить, есть ли под ней элементы "EmailInteractions", прежде чем запускать задание, которое взорвет его,

Я отредактировал вопрос для ясности

1. check if email interactions array exist and check if it has columns, if both true, explode the array and finish, if one of the conditions is false, pass to step 2

2.check if phone interactions array exist and check if it has columns, if both true, explode the array and finish, if one of the conditions is false, pass to step 3

3.check if web interactions exist and check if it has columns, if both true, explode the array and finish, if one of the conditions is false, finish

Я новичок в кодировании и бриках данных, помогите пожалуйста в этом.

1 Ответ

0 голосов
/ 07 февраля 2020

Это один из способов сделать это. К сожалению, информация в StructField не легко доступна для поиска, я преобразовал ее в строку и выполнил поиск по имени поля или ключевому слову "StructField", чтобы узнать, что оно содержит поле.

    val jsonWithNull = """{"a": 123,"b":null,"EmailInteractions":[{"CreatedBy":"test"}]}"""
    val jsonWithoutNull = """{"a": 123,"b":3,"EmailInteractions":[{"CreatedBy":"test1"}]}"""

    import spark.implicits._
    val df = spark.read.json(Seq(jsonWithNull,jsonWithoutNull).toDS)
    df.printSchema()

    val field = df.schema.filter { f =>
      if (f.dataType.typeName == "array" && f.toString().contains("CreatedBy")){
        true
      }
      else{
        false
      }
    }

    println(field)

// check for field value being not null then explode 

Результат List(StructField(EmailInteractions,ArrayType(StructType(StructField(CreatedBy,StringType,true)),true),true))

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...