- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑下面的例子
dataframe_test<- data_frame(mydate = c('2011-03-01T00:00:04.226Z', '2011-03-01T00:00:04.226Z'))
# A tibble: 2 x 1
mydate
<chr>
1 2011-03-01T00:00:04.226Z
2 2011-03-01T00:00:04.226Z
sdf <- copy_to(sc, dataframe_test, overwrite = TRUE)
> sdf
# Source: table<dataframe_test> [?? x 1]
# Database: spark_connection
mydate
<chr>
1 2011-03-01T00:00:04.226Z
2 2011-03-01T00:00:04.226Z
timestamp
以使其具有更常规的格式。我尝试使用
regexp_replace
这样做,但失败了。
> sdf <- sdf %>% mutate(regex = regexp_replace(mydate, '(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2}).(\\d{3})Z', '$1-$2-$3 $4:$5:$6.$7'))
> sdf
# Source: lazy query [?? x 2]
# Database: spark_connection
mydate regex
<chr> <chr>
1 2011-03-01T00:00:04.226Z 2011-03-01T00:00:04.226Z
2 2011-03-01T00:00:04.226Z 2011-03-01T00:00:04.226Z
最佳答案
Spark SQL 和 Hive 提供两种不同的功能:
regexp_extract
- 它需要字符串、模式和要提取的组的索引。 regexp_replace
- 它接受一个字符串、模式和替换字符串。 java.util.regex.Matcher
相同
regexp_replace
模式必须匹配整个字符串,如果没有匹配,则返回输入字符串:
sdf %>% mutate(
regex = regexp_replace(mydate, '^([0-9]{4}).*', "$1"),
regexp_bad = regexp_replace(mydate, '([0-9]{4})', "$1"))
## Source: query [2 x 3]
## Database: spark connection master=local[8] app=sparklyr local=TRUE
##
## # A tibble: 2 x 3
## mydate regex regexp_bad
## <chr> <chr> <chr>
## 1 2011-03-01T00:00:04.226Z 2011 2011-03-01T00:00:04.226Z
## 2 2011-03-01T00:00:04.226Z 2011 2011-03-01T00:00:04.226Z
regexp_extract
则不需要:
sdf %>% mutate(regex = regexp_extract(mydate, '([0-9]{4})', 1))
## Source: query [2 x 2]
## Database: spark connection master=local[8] app=sparklyr local=TRUE
##
## # A tibble: 2 x 2
## mydate regex
## <chr> <chr>
## 1 2011-03-01T00:00:04.226Z 2011
## 2 2011-03-01T00:00:04.226Z 2011
sdf %>% mutate(
regex = regexp_replace(
mydate,
'^(\\\\d{4})-(\\\\d{2})-(\\\\d{2})T(\\\\d{2}):(\\\\d{2}):(\\\\d{2}).(\\\\d{3})Z$',
'$1-$2-$3 $4:$5:$6.$7'))
spark_session(sc) %>%
invoke("sql",
"SELECT *, DATE_FORMAT(CAST(mydate AS timestamp), 'yyyy-MM-dd HH:mm:ss.SSS') parsed from dataframe_test") %>%
sdf_register
## Source: query [2 x 2]
## Database: spark connection master=local[8] app=sparklyr local=TRUE
##
## # A tibble: 2 x 2
## mydate parsed
## <chr> <chr>
## 1 2011-03-01T00:00:04.226Z 2011-03-01 01:00:04.226
## 2 2011-03-01T00:00:04.226Z 2011-03-01 01:00:04.226
sparklyr
在这方面似乎极其有限,并将时间戳视为字符串。
关于r - Sparklyr/Hive : how to use regex (regexp_replace) correctly?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44658852/
当我尝试使用 Rstudio 和 sparklyr 使用此代码访问 Hive 表时: library(sparklyr) library(dplyr) Sys.setenv(SPARK_HOME="/
以下示例描述了如何在不使用 dplyr 和 sparklyr 聚合行的情况下计算不同值的数量。 有没有不破坏命令链的解决方法? 更一般地说,如何在 sparklyr 数据帧上使用类似 sql 的窗口函
我正在尝试在 sparklyr 中复制 tidyr:complete 函数。我有一个包含一些缺失值的数据框,我必须填写这些行。在 dplyr/tidyr 中我可以这样做: data
我想从 sparklyr 中我的 Spark DataFrame 的每个类中采样 n 行。 我知道 dplyr::sample_n 函数不能用于此 (Is sample_n really a rand
希望将一些 R 代码转换为 Sparklyr,函数如 lmtest::coeftest() 和 sandwich::sandwich()。尝试开始使用 Sparklyr 扩展,但对 Spark API
我想跳过(退出)文本文件的前两行: 据我所知,使用 sparklyr 方法是不可能的 spark_read_csv .有一些解决方法可以解决这个简单的问题吗? 我知道 sparklyr extensi
在 Spark 2.0 中,我可以将多个文件路径合并为一个加载(参见例如 How to import multiple csv files in a single load?)。 如何使用 spark
Sparklyr 处理分类变量 我来自 R 背景,习惯于在后端处理分类变量(作为因子)。对于 Sparklyr,使用 string_indexer 或 onehotencoder 非常令人困惑。 例如
我正在尝试在sparklyr中读取2GB〜(5mi行)的.csv: bigcsvspark <- spark_read_csv(sc, "bigtxt", "path",
我很抱歉这个问题很难完全重现,因为它涉及一个正在运行的 spark 上下文(在下面引用为 sc),但我正在尝试在 sparklyr 中设置一个 hadoopConfiguration,专门用于从 RS
我有一个朴素贝叶斯模型在 sparklyr 中使用 ml_naive_bayes 运行,如下所示: library(sparklyr) library(dplyr) sc model Call: m
我在使用 ft_.. sparklyr R 包中的函数时遇到了一些问题。 ft_bucketizer 有效,但 ft_normalizer 或 ft_min_max_scaler 无效。这是一个例子:
即使在相当小的数据集上,我也会遇到堆空间错误。我可以确定我没有耗尽系统内存。例如,考虑一个包含大约 20M 行和 9 列的数据集,它在磁盘上占用 1GB。我在具有 30GB 内存的 Google Co
尝试在 sparklyr 中拆分一个字符串,然后将其用于连接/过滤 我尝试了将字符串标记化然后将其分离到新列的建议方法。这是一个可重现的示例(请注意,我必须将在 copy_to 之后变成字符串“NA”
我对 sparklyr 和 spark 很陌生,所以如果这不是执行此操作的“spark”方式,请告诉我。 我的问题 我有 50 多个 .txt 文件,每个文件大约 300 mb,都在同一个文件夹中,将
我对 Spark 很陌生,目前正在通过 sparkly 包使用 R API 使用它。我从 hive 查询创建了一个 Spark 数据框。源表中未正确指定数据类型,我试图通过利用来自 dplyr 的函数
我需要使用 sparklyr 计算 R 中两个字符串之间的距离。有没有办法使用 stringdist 或任何其他包?我想使用cousine distance。此距离用作 stringdist 函数的方
在以下示例中,我加载了一个 Parquet 文件,该文件包含 meta 中 map 对象的嵌套记录。 field 。 sparklyr似乎在处理这些方面做得很好。然而tidyr::unnest不会转换
我是 sparklyr 的新手(但熟悉 spark 和 pyspark),我有一个非常基本的问题。我正在尝试根据部分匹配过滤列。在 dplyr 中,我会这样写我的操作: businesses %>%
我在 Spark 中有一个数据框,希望在按特定列分组后计算 0.1 分位数。 例如: > library(sparklyr) > library(tidyverse) > con = spark_co
我是一名优秀的程序员,十分优秀!