Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 如何从拼花文件中选择13位时间戳列,将其转换为日期并存储为数据帧?_Scala_Apache Spark_Intellij Idea_Apache Spark Sql_Parquet - Fatal编程技术网

Scala 如何从拼花文件中选择13位时间戳列,将其转换为日期并存储为数据帧?

Scala 如何从拼花文件中选择13位时间戳列,将其转换为日期并存储为数据帧?,scala,apache-spark,intellij-idea,apache-spark-sql,parquet,Scala,Apache Spark,Intellij Idea,Apache Spark Sql,Parquet,因为我是ApacheSpark和Scala方法的新手,所以我想执行以下需求 -从拼花文件中读取特定列(13位时间戳)。 -将时间戳转换为普通日期格式(yyyy-MM-dd HH:MM:ss)。 -将结果存储为数据集中的另一列。 我可以使用以下代码读取时间戳 import org.apache.spark.SparkConf import org.apache.spark.SparkContext import org.apache.spark.sql.SQLContext object Te

因为我是ApacheSpark和Scala方法的新手,所以我想执行以下需求

-从拼花文件中读取特定列(13位时间戳)。

-将时间戳转换为普通日期格式(yyyy-MM-dd HH:MM:ss)。

-将结果存储为数据集中的另一列。

我可以使用以下代码读取时间戳

import org.apache.spark.SparkConf

import org.apache.spark.SparkContext

import org.apache.spark.sql.SQLContext
object Test {

  def main(args: Array[String]){
    val conf=new SparkConf().setAppName("TEST_APP").setMaster("local")
    val sc=new SparkContext(conf)
    val sqlcon=new SQLContext(sc)
    val Testdata = sqlcon.read.parquet("D:\\TestData.parquet")
    val data_eve_type_end=Testdata.select(Testdata.col("heading.timestamp")).where(Testdata.col("status").equalTo("Success")).toDF("13digitTime")
  }
}
我尝试使用下面的参考链接转换时间戳

[

但这对我不起作用。我不知道我是否正确地将数据提取到数据帧中。无论如何,这会使输出成为一个columnname13digitTime的表,值是一些大小为13位的数字

当我试图从上面提到的链接执行代码时,它显示错误如下

WARN Utils: Truncated the string representation of a plan since it was too large. This behavior can be adjusted by setting 'spark.debug.maxToStringFields' in SparkEnv.conf.
Exception in thread "main" org.apache.spark.sql.AnalysisException: cannot resolve '(`13digitTime` / 1000000)' due to data type mismatch:
我期望数据框有两列,其中一列应包含13位时间戳,另一列应包含从13位到通用日期格式(yyyy-MM-dd HH:MM:ss)的转换时间


我希望得到一个解决方案,提前谢谢。

sqlcon.read.parquet将返回一个数据帧本身。您需要做的就是使用withcolumn方法添加一个新列。这应该可以工作

val data_eve_type_end = Testdata.withColumn("13digitTime", from_unixtime($"heading.timestamp"))
我更新了我的代码,将13位unix时间除以1000转换为10位,并将其转换为tiimestamp

val date_conv=data_eve_type_end.select(col("timestamp_value").as("UNIX TIME"),from_unixtime(col("timestamp_value")/1000).cast("timestamp").as("GENERAL TIME"))
而输出就像

+-------------+-------------------+
|    UNIX TIME|       GENERAL TIME|
+-------------+-------------------+
|1551552902793|  2019-03-0 6:55:02|

你能添加一些heading.timestamp的示例吗?@ApurbaPandey例如:1551551552902793Sqlcon.read.parquet将返回一个数据帧本身。你所需要做的就是使用withcolumn方法添加一个新列。这应该行得通。val data\u eve\u type\u end=Testdata.withcolumn(“13digitTime”,from_unixtime($“heading.timestamp”))
val-date\u-conv=data\u-eve\u-type\u-end。从unixtime(col(“timestamp\u-value”)/1000中选择(col(“timestamp\u-value”).as(“UNIX-TIME”).cast(“timestamp”).as(“GENERAL-TIME”)
@ApurbaPandey这是我的代码现在看起来的样子。@ApurbaPandey它工作得很好。感谢有价值的帮助。如何将其标记为正确答案?