- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Spark Version: spark-2.0.1-bin-hadoop2.7
Scala: 2.11.8
我正在将原始 csv 加载到 DataFrame 中。在 csv 中,虽然该列支持日期格式,但它们被写为 20161025 而不是 2016-10-25。参数date_format
包括需要转换为 yyyy-mm-dd 格式的列名称字符串。
在下面的代码中,我首先通过 schema
将 Date 列的 csv 加载为 StringType ,然后我检查是否 date_format
不为空,即有列需要转换为Date
来自 String
,然后使用 unix_timestamp
转换每一列和 to_date
.然而,在csv_df.show()
,返回的行都是null
.
def read_csv(csv_source:String, delimiter:String, is_first_line_header:Boolean,
schema:StructType, date_format:List[String]): DataFrame = {
println("|||| Reading CSV Input ||||")
var csv_df = sqlContext.read
.format("com.databricks.spark.csv")
.schema(schema)
.option("header", is_first_line_header)
.option("delimiter", delimiter)
.load(csv_source)
println("|||| Successfully read CSV. Number of rows -> " + csv_df.count() + " ||||")
if(date_format.length > 0) {
for (i <- 0 until date_format.length) {
csv_df = csv_df.select(to_date(unix_timestamp(
csv_df(date_format(i)), "yyyy-MM-dd").cast("timestamp")))
csv_df.show()
}
}
csv_df
}
+-------------------------------------------------------------------------+
|to_date(CAST(unix_timestamp(prom_price_date, YYYY-MM-DD) AS TIMESTAMP))|
+-------------------------------------------------------------------------+
| null|
| null|
| null|
| null|
| null|
| null|
| null|
| null|
| null|
| null|
| null|
| null|
| null|
| null|
| null|
| null|
| null|
| null|
| null|
| null|
+-------------------------------------------------------------------------+
null
?
最佳答案
转换 yyyyMMdd
至 yyyy-MM-dd
你可以:
spark.sql("""SELECT DATE_FORMAT(
CAST(UNIX_TIMESTAMP('20161025', 'yyyyMMdd') AS TIMESTAMP), 'yyyy-MM-dd'
)""")
date_format(unix_timestamp(col, "yyyyMMdd").cast("timestamp"), "yyyy-MM-dd")
关于Scala:Spark SQL to_date(unix_timestamp) 返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40433065/
虽然写了几个查询,但我只需要返回那些在今年(2017 年)设置了日期列的行,这不是我的问题我知道如何用几种不同的方式编写这个查询,但我遇到了一些奇怪的事情出乎我意料。谁能解释为什么 Oracle db
SELECT TO_CHAR((select logical_date -1 from logical_date where logical_date_type='B'),'DD/MM/YYYY HH
return [ Date::make('From Date') ->sortable() ->rules('r
下面的查询返回结果: SELECT * FROM EMPLOYEES WHERE HIRE_DATE = TO_DATE('21-09-1989','DD-MM-YY'); 好像我将日期从 21-09
这怎么可能? Time.now.utc.to_date + 1.month + 15.days #=> Mon, 01 Dec 2014 Time.now.utc.to_date + 15.days
我一直认为 to_date 函数字符串和日期格式应该匹配。为什么下面的语句工作正常? select TO_DATE('20151014','yyyy-mm-dd') from dual; oracle
我的 SQL where 子句中有以下内容。这是针对 Oracle 数据库运行的。 sc_dt字段在数据库中定义为日期字段。 sc_dt = TO_DATE('2011-11-03 00:00:00.
我写这条语句是为了获取上个月的第一天 select '01.' || TO_CHAR(ADD_MONTHS(SYSDATE,-1),'MM.yyyy') from dual 输出:01.02.2016
我有一个字符串列,其中包含以下数据作为示例 10/20/2005 15:08:00 11252011 15:22:40 我创建了一个临时日期列,我将把内容复制到其中,删除字符串列并将临时列重命名为
我正在查询以下sql。请帮我。 select to_date(sysdate, 'DD MONTH YYYY') , to_date(sysdate, 'yyyy/mm/dd hh24:mi:ss'
我有一个表,其中需要获取的日期是字符类型。我尝试使用 CONVERT 函数,但它不起作用。该查询在 postgres 中运行良好,但在 db2 中则不行。 select * FROM abc wher
我正在尝试按组和按月排序。因为我使用 to_char(date_field, 'Month') as month 将月份作为字符串而不是日期获取。这会导致结果按字母顺序而不是按时间顺序对月份进行排序。
在Oracle数据库中,Oracle to_date()函数是我们经常使用的函数,下面就为您详细介绍Oracle to_date()函数的用法,希望可以对您有所启迪。 to_date()与24小时
在Oracle数据库中,Oracle to_date()函数是我们经常使用的函数,下面就为您详细介绍Oracle to_date()函数的用法,希望可以对您有所启迪。 to_date()与24小时
我有简单的计算,我用时间从日期中减去间隔: select TO_DATE('2016-12-05 23:04:59', 'YYYY-MM-DD HH24:MI:SS') - to_dsinterv
我想用Oracle数据库实现以下。 当前的 table FTE f_date f_stime f_eDate f_etime 20191125 14:00 20191125
select TO_CHAR(to_date(sysdate, 'DD-MON-YYYY'), 'DAY') FROM DUAL; 当我运行这个查询时,输出是:SUNDAY。但我们知道今天是星期二(1
此查询运行但未返回任何行。 SELECT DISTINCT TO_CHAR(START_DATE,'MM/DD/YYYY'),
我想按部分创建 DATETIME,但在某些情况下这些部分为空。 示例 SELECT TO_DATE(STARTDATEMONTH||'-01-'||STARTDATEYEAR, 'MM-DD-YYYY
我刚刚发现了 oracle 的奇怪行为 TO_DATE与 format_mask 一起使用时的功能范围。 基本上,我看到的是,在一种情况下,它会忽略给定的格式掩码,并使用自己的掩码解析输入,而在其他情
我是一名优秀的程序员,十分优秀!