java .lang.ClassCast errors.GenericRowWithSchema не может быть приведен к scala .collection.Seq - PullRequest
0 голосов
/ 27 января 2020

Как преобразовать RDD карты в свернутую Array, я получаю ошибку

Схема:

enter image description here

Когда я пытаюсь преобразовать dataframe в pojo, я получаю исключение, как показано ниже:

java.lang.ClassCastException: 
  org.apache.spark.sql.catalyst.expressions.GenericRowWithSchema cannot 
  be cast to scala.collection.Seq

Код:

rdd.map(row => {
  var arrm_list: Seq[Row] = rows.getAs[AnyRef]("ArrTeber").asInstanceOf[Seq[Row]]
  //working fine here
  arrm_list.foreach(x => {
    var arrmtrb__list: Seq[Row] = rows.getAs[AnyRef]("ArrTeberPRD").asInstanceOf[Seq[Row]]

    //working fine here
    arrmtrb__list.foreach(pt => {

      var pd__list: Seq[Row] = rows.getAs[AnyRef]("Pduct").asInstanceOf[Seq[Row]] //raising error
    })
  })
})

1 Ответ

2 голосов
/ 27 января 2020

Вышеприведенное исключение является просто исключением преобразования класса, поскольку struct не может быть приведена к Seq of struct (см. Схему: - Pduct: struct (nullable = true)). Приведите Pduct к Row, а затем извлеките вложенные элементы.

df.map(row => {
  var arrm_list: Seq[Row] = row.getAs[Seq[Row]]("ArrTeber")

  arrm_list.foreach(x => {
    var arrmtrb__list: Seq[Row] = x.getAs[Seq[Row]]("ArrTeberPRD")


    arrmtrb__list.foreach(pt => {
      var pd__list: Row = pt.getAs[Row]("Pduct") //Pduct: struct (nullable = true)

    })
  })

})
...