0

I have a dataframe with schema like below

 |-- aaa: string (nullable = true)
 |-- bbb: long (nullable = false)
 |-- ccc: string (nullable = true)
 |-- ddd: string (nullable = true)
 |-- eee: long (nullable = false)

Already i have results in val for the above .

I want to convert the schema as below using spark scala

  |ddd:struct (nullable = true)
      |-- aaa: string (nullable = true)
      |-- bbb: string (nullable = true)
      |-- ccc: string (nullable = true)
      |-- eee: string (nullable = true)

Please help

2
  • 1
    Please show your effort and post your code. Commented Aug 31, 2017 at 4:45
  • Welcome to stackoverflow. Please give a more detailed description of your question. Include code when possible. Commented Sep 1, 2017 at 13:00

1 Answer 1

2

You should use struct function

the first method is to use withColumn api and if you don't want the original columns you can drop them as

import org.apache.spark.sql.functions._
df.withColumn("ddd", struct(col("aaa"), col("bbb").cast(StringType).as("bbb"), col("ccc"), col("eee").cast(StringType).as("eee"))).drop("aaa", "bbb", "ccc", "eee")

the second way is to use select api as

df.select(struct(col("aaa"), col("bbb").cast(StringType).as("bbb"), col("ccc"), col("eee").cast(StringType).as("eee")).as("ddd"))
Sign up to request clarification or add additional context in comments.

8 Comments

Hi Harsh Thanks for your reply
But it is appending
i Have to create a new schema as above
it does create a new schema :) see my updated answer
and i am not Harsh @gayathri :P
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.