3

I have a hive Table having Date and Timestamp datatypes. I am creating DataFrame using below java code:

SparkConf conf = new SparkConf(true).setMaster("yarn-cluster").setAppName("SAMPLE_APP");
SparkContext sc = new SparkContext(conf);
HiveContext hc = new HiveContext(sc);
DataFrame df = hc.table("testdb.tbl1");

Dataframe schema:

 df.printSchema
root
 |-- c_date: date (nullable = true)
 |-- c_timestamp: timestamp (nullable = true)

I want to covert these columns to String. How can I achieve this?

I need this because of issue : Spark csv data validation failed for date and timestamp data types of Hive

2 Answers 2

2

You can do the following:

df.withColumn("c_date", df.col("c_date").cast(StringType))
Sign up to request clarification or add additional context in comments.

Comments

2

In scala, we generally cast datatypes like this:

df.select($"date".cast(StringType).as("new_date"))

Comments

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.