【问题标题】:Write data frame to Elastic Search with @timestamp使用 @timestamp 将数据框写入 Elastic Search
【发布时间】:2016-11-10 13:36:16
【问题描述】:

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

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

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

请提出建议。

版本:

R:3.3.1

弹性搜索 - 2.4.1

操作系统 - 红帽

【问题讨论】:

    标签: r elasticsearch ropensci


    【解决方案1】:

    elastic 不会尝试从您的输入 data.frame 或列表中捕获数据类型到docs_bulk() - 我们可以考虑尝试这样做,但我想 R 数据类型不会完全映射到 Elasticsearch 类型- 可能会尝试映射数据类型。这是我的做法:

    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")
    

    【讨论】:

    • 谢谢,它帮助了我。在格式中,月份必须是 MM
    猜你喜欢
    • 1970-01-01
    • 2017-03-26
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多