简体   繁体   English

如何将数据帧数据类型转换为String?

[英]How to covert dataframe datatypes to String?

I have a hive Table having Date and Timestamp datatypes. 我有一个具有DateTimestamp数据类型的配置单元表。 I am creating DataFrame using below java code: 我正在使用以下java代码创建DataFrame

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. 我想将这些列转换为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 因为问题我需要这个: 对于Hive的日期和时间戳数据类型,Spark csv数据验证失败

在scala中,我们通常会抛出这样的数据类型:

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

您可以执行以下操作:

df.withColumn("c_date", df.col("c_date").cast(StringType))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将嵌套结构转换为 Spark DataFrame 的嵌套映射 - How to covert nested struct into nested map for Spark DataFrame 如何在 Scala/Spark 中将带有 String 的列转换为 Array [String]? - How to covert a column with String to Array[String] in Scala/Spark? 如何使用 SparkSQL 按列数据类型将 dataframe 拆分为多个数据帧? - How to split dataframe into multiple dataframes by their column datatypes using SparkSQL? 如何将 Dataframe 转换为数据集,具有父 class 的 object 引用作为另一个 ZA2F2ED4ADC98EBC2CBBDZC21 内部的组合? - How to covert a Dataframe to a Dataset,having a object reference of the parent class as a composition inside another class? 将火花数据框隐藏到 Scala 地图集合 - Covert spark dataframe to Scala Map collection 根据列数据类型过滤Spark数据帧 - Filter a spark dataframe based on column datatypes 在Scala中将一个DataFrame展平,其中包含不同的DataTypes - Flatten a DataFrame in Scala with different DataTypes inside 将Spark数据帧数据类型映射到jdbc数据类型 - mapping spark dataframe datatypes to jdbc datatype 多个字符串列上的隐蔽日期 - PySpark / Databricks - covert dates on multiple string columns - PySpark / Databricks 如何将Iterable [String,String,String]转换为DataFrame? - How to convert Iterable[String, String, String] to DataFrame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM