Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
I have a dataframe and want to add a column of type String with null values. How can it be done using Spark Java API.
String
I used lit functions, but getting error when tried writing the DF and saveAsTable.
lit
saveAsTable
df.withColumn("col_name", lit(null).cast("string"))
or
import org.apache.spark.sql.types.StringType df.withColumn("col_name", lit(null).cast(StringType))
Add a comment
Was able to solve by using lit function on the column with null value and type cast the column to String type.
df.withColumn( "col_name", functions.lit(null) ).withColumn("col_name", df.col("channel_name").cast(DataTypes.StringType) )
Required, but never shown
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.
Explore related questions
See similar questions with these tags.