gpt4 book ai didi

r - 使用@timestamp将数据帧写入Elasticsearch

转载 作者:行者123 更新时间:2023-12-02 22:19:22 25 4
gpt4 key购买 nike

我正在探索elastic R包以将数据帧写入ElasticSearch。
我正在使用docs_bulk函数。

我的数据框中的一列是 @timestamp ,其格式为POSIXct
但是该字段已在字符串中保存在Elastic Search中。
关于如何获取以时间格式保存的列的任何想法。

我还尝试通过使用正确的数据类型定义手动创建索引映射,但是它不起作用。

请提出建议。

版本:

R:3.3.1

flex 搜索-2.4.1

操作系统-Redhat

最佳答案

elastic不会尝试从输入data.frame或列表中捕获数据类型到docs_bulk()-我们可以考虑尝试这样做,但是我认为R数据类型不能准确映射到Elasticsearch类型-可能会尝试 map 数据类型。这是我的处理方式:

library('elastic')
connect()

虚拟数据框
df <- data.frame(
date = as.POSIXct(seq(from = as.Date("2016-10-01"),
to = as.Date("2016-10-31"), by = 'day')),
num = 1:31
)

创建列表或JSON字符串的映射
mapping <- list(
world = list(properties = list(
date = list(
type = "date",
format = "yyyy-mm-dd HH:mm:ss"
),
num = list(type = "long")
)))

编制索引
index_create(index = "hello")

在索引中创建映射
mapping_create(index = "hello", type = "world", body = mapping)

获取映射
mapping_get("hello")
#> $hello
#> $hello$mappings
#> $hello$mappings$world
#> $hello$mappings$world$properties
#> $hello$mappings$world$properties$date
#> $hello$mappings$world$properties$date$type
#> [1] "date"
#>
#> $hello$mappings$world$properties$date$format
#> [1] "yyyy-mm-dd HH:mm:ss"
#>
#>
#> $hello$mappings$world$properties$num
#> $hello$mappings$world$properties$num$type
#> [1] "long"

批量加载data.frame
docs_bulk(df, index = "hello", type = "world")

搜索索引
Search("hello")

关于r - 使用@timestamp将数据帧写入Elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40511719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com