R 如何在斯巴克按小时分组?

R 如何在斯巴克按小时分组?,r,apache-spark,sparkr,R,Apache Spark,Sparkr,我尝试使用SparkR和Spark 2.1.0按时间总结一些日期。 我的数据如下所示: created_at 1 Sun Jul 31 22:25:01 +0000 2016 2 Sun Jul 31 22:25:01 +0000 2016 3 Fri Jun 03 10:16:57 +0000 2016 4 Mon May 30 19:23:55 +0000 2016 5 Sat Jun 11 21:00:07 +0000 2016 6

我尝试使用SparkR和Spark 2.1.0按时间总结一些日期。 我的数据如下所示:

                       created_at
1  Sun Jul 31 22:25:01 +0000 2016
2  Sun Jul 31 22:25:01 +0000 2016
3  Fri Jun 03 10:16:57 +0000 2016
4  Mon May 30 19:23:55 +0000 2016
5  Sat Jun 11 21:00:07 +0000 2016
6  Tue Jul 12 16:31:46 +0000 2016
7  Sun May 29 19:12:26 +0000 2016
8  Sat Aug 06 11:04:29 +0000 2016
9  Sat Aug 06 11:04:29 +0000 2016
10 Sat Aug 06 11:04:29 +0000 2016
我希望输出是:

Hour      Count
22         2
10         1
19         1
11         3
....
我试过:

sumdf <- summarize(groupBy(df, df$created_at), count = n(df$created_at))
head(select(sumdf, "created_at", "count"),10)
sumdf <- summarize(groupBy(df, hr=hour(df$created_at)), count = n(hour(df$created_at)))
head(select(sumdf, "hour(created_at)", "count"),20)
sumdf <- summarize(groupBy(df, df$created_at), count = n(hour(df$created_at)))
head(select(sumdf, "created_at", "count"),10)
我试过:

sumdf <- summarize(groupBy(df, df$created_at), count = n(df$created_at))
head(select(sumdf, "created_at", "count"),10)
sumdf <- summarize(groupBy(df, hr=hour(df$created_at)), count = n(hour(df$created_at)))
head(select(sumdf, "hour(created_at)", "count"),20)
sumdf <- summarize(groupBy(df, df$created_at), count = n(hour(df$created_at)))
head(select(sumdf, "created_at", "count"),10)
我试过:

sumdf <- summarize(groupBy(df, df$created_at), count = n(df$created_at))
head(select(sumdf, "created_at", "count"),10)
sumdf <- summarize(groupBy(df, hr=hour(df$created_at)), count = n(hour(df$created_at)))
head(select(sumdf, "hour(created_at)", "count"),20)
sumdf <- summarize(groupBy(df, df$created_at), count = n(hour(df$created_at)))
head(select(sumdf, "created_at", "count"),10)

我如何使用hour函数来实现这一点,或者有更好的方法吗?

这是SCALA代码,我想您可以参考它

    var index = ss.sparkContext.parallelize( Seq(
  (1,"Sun Jul 31 22:25:01 +0000 2016"),
  (2,"Sun Jul 31 22:25:01 +0000 2016"),
  (3,"Fri Jun 03 10:16:57 +0000 2016"),
  (4,"Mon May 30 19:23:55 +0000 2016"),
  (5,"Sat Jun 11 21:00:07 +0000 2016"),
  (6,"Tue Jul 12 16:31:46 +0000 2016"),
  (7,"Sun May 29 19:12:26 +0000 2016"),
  (8,"Sat Aug 06 11:04:29 +0000 2016"),
  (9,"Sat Aug 06 11:04:29 +0000 2016"),
  (10,"Sat Aug 06 11:04:29 +0000 2016"))
).toDF("ID", "time")

val getHour = udf( (s : String) => {
  s.substring( 11, 13)
})
index.withColumn("hour", getHour($"time")).groupBy( "hour").agg( count("*").as("count")).show

假设本地表是
df
,这里真正的问题是从
列中提取创建的
小时数,然后使用分组代码。为此,您可以使用
dapply

library(SparkR)
sc1 <- sparkR.session()
df2 <- createDataFrame(df)

#with dapply you need to specify the schema i.e. the data.frame that will come out
#of the applied function - i.e. substringDF in our case
schema <- structType(structField('created_at', 'string'), structField('time', 'string'))

#a function that will be applied to each partition of the spark data frame.
#remember that each partition is a data.frame itself.
substringDF <- function(DF) {

 DF$time <- substr(DF$created_at, 15, 16)

 DF

}

#and then we use the above in dapply
df3 <- dapply(df2, substringDF, schema)
head(df3)
#                        created_at time
#1 1  Sun Jul 31 22:25:01 +0000 2016   22
#2 2  Sun Jul 31 22:25:01 +0000 2016   22
#3 3  Fri Jun 03 10:16:57 +0000 2016   10
#4 4  Mon May 30 19:23:55 +0000 2016   19
#5 5  Sat Jun 11 21:00:07 +0000 2016   21
#6 6  Tue Jul 12 16:31:46 +0000 2016   16
库(SparkR)

sc1我将使用
到时间戳
(Spark 2.2)或
unix\u时间戳%>%cast(“时间戳”)
(早期版本)解析日期,并访问
小时:

df <- createDataFrame(data.frame(created_at="Sat Aug 19 12:33:26 +0000 2017"))
head(count(group_by(df, 
  alias(hour(to_timestamp(column("created_at"), "EEE MMM d HH:mm:ss Z yyyy")), "hour")
)))
##  hour count
## 1   14     1

df您应该尝试将在
列中创建的
拆分为
小时
值,然后在
小时
列中使用groupBy。这是离题的,甚至对于Scala来说也是不好的做法。我相信这篇文章是关于sparkRYes的,所以我说“这是SCALA代码,我想你可以参考一下。”这仍然是糟糕的代码@Robin。人们应该接受批评。我记得我没有侮辱你。我没有投你的反对票,其他人也可以。我不懂斯巴克,我懂一点斯卡拉。所以我把我的想法放在这里,希望能给asker一些启发。这不是问题所在。但是,如果您想回顾一下Scala代码,这里就是:将日期视为字符串是不好的做法。如果格式发生变化,您应该使用日期库和解析器。谢谢。我在使用1.6.3时做了一些更改:df%cast(“timestamp”),“hour”))知道如何克服:as.POSIXlt.default(x,tz=tz(x))中的错误:不知道如何将“x”转换为类“POSIXlt”?谢谢。我应该提到我使用的是Spark 1.6.3,我认为dapply不适用于此。我决定使用Spark2并尝试一下您的解决方案。df已经是spark数据帧了。因此,我使用您的代码减去forst 3行,并将df2更改为df,将df3更改为df2。
head(df3)
给出:
ERROR Executor:task 0.0中的异常在10.0阶段(TID 12)java.lang.arrayindexoutofbounds异常:2在org.apache.spark.sql.api.r.SQLUti
有什么想法吗?如果你把我的
df3
改为
df2
你应该使用
head(df2)
对吗?此外,请尝试使用我的代码,看看它是否有效(即,将您的spark data.frame df2命名)。我在一个独立版本和一个纱线版本上都试过了,它们都很好用。您确定Spark 2已正确安装和配置吗?是
head(df2)
。我觉得Spark2还可以,已经用了一段时间了。这很有效:
head(substringDF(df))
。我将尝试重新启动Spark2。因此,这可能是模式的一种情况。您需要告诉
dapply
sparkdata.frame的结构是什么,即它需要知道列名和列的数据类型(例如,在上述情况下,列名
time
作为
string
)。确保您已正确设置了这些。