Условие NotNull не работает для условия withColumn в scala фрейма данных
Поэтому я пытаюсь добавить столбец, когда я нахожу его, но я не хочу добавлять, когда столбец отсутствует в схеме XML. Это то, что я делаю, я думаю, что я делаю что-то не так при проверке состояния.
val temp = tempNew1
.withColumn("BookMark", when($"AsReportedItem.fs:BookMark".isNotNull or $"AsReportedItem.fs:BookMark" =!= "", 0))
.withColumn("DocByteOffset", when($"AsReportedItem.fs:DocByteOffset".isNotNull or $"AsReportedItem.fs:DocByteOffset" =!= "", 0))
.withColumn("DocByteLength", when($"AsReportedItem.fs:DocByteLength".isNotNull or $"AsReportedItem.fs:DocByteLength" =!= "", 0))
.withColumn("EditedDescription", when($"AsReportedItem.fs:EditedDescription".isNotNull or $"AsReportedItem.fs:EditedDescription" =!= "", 0))
.withColumn("EditedDescription", when($"AsReportedItem.fs:EditedDescription._VALUE".isNotNull or $"AsReportedItem.fs:EditedDescription._VALUE" =!= "", 0))
.withColumn("EditedDescription_languageId", when($"AsReportedItem.fs:EditedDescription._languageId".isNotNull or $"AsReportedItem.fs:EditedDescription._languageId" =!= "", 0))
.withColumn("ReportedDescription", when($"AsReportedItem.fs:ReportedDescription._VALUE".isNotNull or $"AsReportedItem.fs:ReportedDescription._VALUE" =!= "", 0))
.withColumn("ReportedDescription_languageId", when($"AsReportedItem.fs:ReportedDescription._languageId".isNotNull or $"AsReportedItem.fs:ReportedDescription._languageId" =!= "", 0))
.withColumn("FinancialAsReportedLineItemName_languageId", when($"FinancialAsReportedLineItemName._languageId".isNotNull or $"FinancialAsReportedLineItemName._languageId" =!= "", 0))
.withColumn("FinancialAsReportedLineItemName", when($"FinancialAsReportedLineItemName._VALUE".isNotNull or $"FinancialAsReportedLineItemName._VALUE" =!= "", 0))
.withColumn("PeriodPermId_objectTypeId", when($"PeriodPermId._objectTypeId".isNotNull or $"PeriodPermId._objectTypeId" =!= "", 0))
.withColumn("PeriodPermId", when($"PeriodPermId._VALUE".isNotNull or $"PeriodPermId._VALUE" =!= "", 0))
.drop($"AsReportedItem").drop($"AsReportedItem")
Но когда я нахожу столбец, он работает нормально для меня, но когда столбец отсутствует в tempNew1
Я получаю ошибку.
По сути, я вообще не хочу делать с Column, если тег не найден в схеме.
Успокаиваю, что мне здесь не хватает. Пожалуйста, помогите мне определить проблему.
Я получаю ошибку ниже
Исключение в потоке "main" org.apache.spark.sql.AnalysisException: не удается разрешить '
AsReportedItem.fs:BookMark
заданные входные столбцы: [IsAsReportedCurrencySetManually,
Это я тоже пробовал
def hasColumn(df: DataFrame, path: String) = Try(df(path)).isSuccess
val temp = tempNew1.withColumn("BookMark", when(hasColumn(tempNew1,"AsReportedItem.fs:BookMark") == true, $"AsReportedItem.fs:BookMark"))
Но не в состоянии заставить его работать полностью..
Это работает, но как я могу написать это для всех столбцов.
val temp = if (hasColumn(tempNew1, "AsReportedItem")) {
tempNew1
.withColumn("BookMark", $"AsReportedItem.fs:BookMark")
.withColumn("DocByteOffset", $"AsReportedItem.fs:DocByteOffset")
.withColumn("DocByteLength", $"AsReportedItem.fs:DocByteLength")
.withColumn("EditedDescription", $"AsReportedItem.fs:EditedDescription")
.withColumn("EditedDescription", $"AsReportedItem.fs:EditedDescription._VALUE")
.withColumn("EditedDescription_languageId", $"AsReportedItem.fs:EditedDescription._languageId")
.withColumn("ReportedDescription", $"AsReportedItem.fs:ReportedDescription._VALUE")
.withColumn("ReportedDescription_languageId", $"AsReportedItem.fs:ReportedDescription._languageId")
.withColumn("FinancialAsReportedLineItemName_languageId", $"FinancialAsReportedLineItemName._languageId")
.withColumn("FinancialAsReportedLineItemName", $"FinancialAsReportedLineItemName._VALUE")
.withColumn("PeriodPermId_objectTypeId", $"PeriodPermId._objectTypeId")
.withColumn("PeriodPermId", $"PeriodPermId._VALUE")
.drop($"AsReportedItem")
} else {
tempNew1
.withColumn("BookMark", lit(null))
.withColumn("DocByteOffset", lit(null))
.withColumn("DocByteLength", lit(null))
.withColumn("EditedDescription", lit(null))
.withColumn("EditedDescription", lit(null))
.withColumn("EditedDescription_languageId", lit(null))
.withColumn("ReportedDescription", lit(null))
.withColumn("ReportedDescription_languageId", lit(null))
.withColumn("FinancialAsReportedLineItemName_languageId", $"FinancialAsReportedLineItemName._languageId")
.withColumn("FinancialAsReportedLineItemName", $"FinancialAsReportedLineItemName._VALUE")
.withColumn("PeriodPermId_objectTypeId", $"PeriodPermId._objectTypeId")
.withColumn("PeriodPermId", $"PeriodPermId._VALUE")
.drop($"AsReportedItem")
}
Добавление схемы основного фрейма данных
root
|-- DataPartition: string (nullable = true)
|-- TimeStamp: string (nullable = true)
|-- PeriodId: long (nullable = true)
|-- SourceId: long (nullable = true)
|-- FinancialStatementLineItem_lineItemId: long (nullable = true)
|-- FinancialStatementLineItem_lineItemInstanceKey: long (nullable = true)
|-- StatementCurrencyId: long (nullable = true)
|-- StatementTypeCode: string (nullable = true)
|-- uniqueFundamentalSet: long (nullable = true)
|-- AuditID: string (nullable = true)
|-- EstimateMethodCode: string (nullable = true)
|-- EstimateMethodId: long (nullable = true)
|-- FinancialAsReportedLineItemName: struct (nullable = true)
| |-- _VALUE: string (nullable = true)
| |-- _languageId: long (nullable = true)
|-- FinancialStatementLineItemSequence: long (nullable = true)
|-- FinancialStatementLineItemValue: double (nullable = true)
|-- FiscalYear: long (nullable = true)
|-- IsAnnual: boolean (nullable = true)
|-- IsAsReportedCurrencySetManually: boolean (nullable = true)
|-- IsCombinedItem: boolean (nullable = true)
|-- IsDerived: boolean (nullable = true)
|-- IsExcludedFromStandardization: boolean (nullable = true)
|-- IsFinal: boolean (nullable = true)
|-- IsTotal: boolean (nullable = true)
|-- PeriodEndDate: string (nullable = true)
|-- PeriodPermId: struct (nullable = true)
| |-- _VALUE: long (nullable = true)
| |-- _objectTypeId: long (nullable = true)
|-- ReportedCurrencyId: long (nullable = true)
|-- StatementSectionCode: string (nullable = true)
|-- StatementSectionId: long (nullable = true)
|-- StatementSectionIsCredit: boolean (nullable = true)
|-- SystemDerivedTypeCode: string (nullable = true)
|-- SystemDerivedTypeCodeId: long (nullable = true)
|-- Unit: double (nullable = true)
|-- UnitEnumerationId: long (nullable = true)
|-- FFAction|!|: string (nullable = true)
|-- PartitionYear: long (nullable = true)
|-- PartitionStatement: string (nullable = true)
Добавление схемы после появления столбцов в схеме
|-- uniqueFundamentalSet: long (nullable = true)
|-- AsReportedItem: struct (nullable = true)
| |-- fs:BookMark: string (nullable = true)
| |-- fs:DocByteLength: long (nullable = true)
| |-- fs:DocByteOffset: long (nullable = true)
| |-- fs:EditedDescription: struct (nullable = true)
| | |-- _VALUE: string (nullable = true)
| | |-- _languageId: long (nullable = true)
| |-- fs:ItemDisplayedNegativeFlag: boolean (nullable = true)
| |-- fs:ItemDisplayedValue: double (nullable = true)
| |-- fs:ItemScalingFactor: long (nullable = true)
| |-- fs:ReportedDescription: struct (nullable = true)
| | |-- _VALUE: string (nullable = true)
| | |-- _languageId: long (nullable = true)
| |-- fs:ReportedValue: double (nullable = true)
|-- EstimateMethodCode: string (nullable = true)
|-- EstimateMethodId: long (nullable = true)
|-- FinancialAsReportedLineItemName: struct (nullable = true)
| |-- _VALUE: string (nullable = true)
| |-- _languageId: long (nullable = true)
|-- FinancialLineItemSource: long (nullable = true)
2 ответа
Я собираюсь показать вам общий способ применения логики к столбцу структуры AsReportedItem (для ясности я прокомментировал код)
//required column names even though the elements are not present in AsReportedItem struct column
val requiredAsReportedItemColumns = Array("BookMark", "DocByteOffset", "DocByteLength", "EditedDescription", "EditedDescription", "EditedDescription_languageId", "ReportedDescription", "ReportedDescription_languageId")
//selecting the elements of AsReportedItem struct columns for checking condition using when
//checking for structfields inside the selected struct field
def getFields(parent: String, schema: StructType): Seq[String] = schema.fields.flatMap {
case StructField(name, t: StructType, _, _) => getFields(parent + name + ".", t)
case StructField(name, _, _, _) => Seq(s"$parent$name")
}
//checking for struct column if present the get the fields of nested structs as well
val AsReportedItemColumns = if(tempNew1.columns.contains("AsReportedItem")) getFields("", tempNew1.select("AsReportedItem.*").schema).toArray.map(x => x.substring(3, x.length)) else Array.empty[String]
//finding difference between required number of columns and the elements present in AsReportedItem struct column
val notInAsReportedItemColumns = requiredAsReportedItemColumns.diff(AsReportedItemColumns.map(x => x.toString.replace(".", "")))
//checking condition for the elements present in AsReportedItem struct column
val temp_for_AsReportedItem = AsReportedItemColumns.foldLeft(tempNew1){(tempdf, name) => tempdf.withColumn(name.replace(".", ""), col(s"AsReportedItem.fs:$name"))}
//populating nulls for the columns that are not present in AsReportedItem struct column
val final_AsReportedItem = notInAsReportedItemColumns.foldLeft(temp_for_AsReportedItem){(tempdf, name)=> tempdf.withColumn(name, lit(null))}.drop("AsReportedItem")
Примените ту же логику к остальным двум столбцам структуры FinancialAsReportedLineItemName
а также PeriodPermId
но на преобразованном датафрейме т.е. наfinal_AsReportedItem
и не наtempNew1
Положив его в качестве ответа, так как он становится слишком большим для комментариев.
Предполагая, что у вас есть коллекция столбцов, которые вы хотите добавить:
val cols = Seq("BookMark")
вам нужно будет повторно позвонить withColumn
на вашем оригинале DataFrame
, присваивая результат новому DataFrame
, Там есть функциональная операция, которая делает именно это, называется fold
:
val result = cols.foldLeft(tempNew1)((df, name) =>
df.withColumn(name, if (df.column.contains(s"AsReportedItem.fs:$name"))
col(s"AsReportedItem.fs:$name") else lit("null")))
fold
принимает первый аргумент (tempNew1
в вашем случае) и вызывает предоставленную функцию для каждого элемента в cols
, присваивая результат новому DataFrame
каждый раз