how to convert Array of any datatype into comma seprated strings in scala

920 Views Asked by At

My current code is :

schema.fields.foreach(f => {
                              if (f.dataType.typeName == "array") {
                                throw ArrayDataTypeNotSupportedException(s"${f.name} column is ArrayType, " +
                                  "writing arrays to CSV isn't supported. Please convert this column to a different data type.")
                              }
                            })

currently we dont support arrays in csv but now want to support array of any datatype by converting it to Strings. String should be separated by comma.

Test case:

test("testArrayInSchema") {
    val df = spark.createDataFrame(Seq(
      TestDataSetArrays(
        Array(1, 2, 3),
        Array("a", "b", "c"),
        Array(new Timestamp(0), new Timestamp(1), new Timestamp(3))
      )
    ))
    assertThrows[ArrayDataTypeNotSupportedException] {
            writeDataFrame(df)

    }

Now we need to remove this exception as we need to support arrays by converting them into string

1

There are 1 best solutions below

0
Shweta Valunj On

To convert the array into comma separated string you can use mkString(", ") method for example:

 val arr = Array("test1", "test2")
 arr.mkString(", ")