You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

influxr_write向InfluxDB写入日期序列时报timestamp错误如何解决

报错原因

  • 首先是数据列长度不匹配:你定义的时间列长度为n1=100,但生成Temp、H、DOY、Code等其他字段时仍然使用了原示例的变量n=1e5(10万),导致数据框各列行数不一致,时间列无法正常匹配其他数据被识别。
  • 其次是时间类型与参数不匹配:influxr_write的内部时间解析函数默认适配POSIXt日期时间类型,你直接传入Date类型的日期序列时,未同时指定时区参数source_tz,导致函数无法将日期值正确转换为InfluxDB要求的时间戳格式。

解决方法

按照以下步骤修改代码即可正常运行:

  1. 统一所有数据列的生成长度,把后续变量生成的n全部替换为n1
  2. 要么将Date类型的时间列转为POSIXct类型,要么在写入函数中补充时区参数source_tz

修正后的完整代码

library(influxr)
# 创建连接(和原示例逻辑一致即可)
tss_client <- influxr_connection(host = 'localhost', ssl = FALSE)
ping(tss_client)

n1 = 1e2

# 创建待上传数据框,统一用n1生成长度,时间转成POSIXct类型适配解析规则
dummy_upload <-
  data.frame(
    time = seq(as.POSIXct('2015-09-15'), by = "1 days", length.out=n1),
    Temp = round(rnorm(n1) * 50, digits = 2),
    H = rnorm(n1),
    DOY = floor(runif(n1, 0, 365)),
    Code = replicate(n1, paste0(sample(LETTERS, 4), collapse = ''))
  )

head(dummy_upload)

tags <-
  c(
    station = 'Houston',
    instrument = 'weather2'
  )

# 写入数据,补充时区参数
res <- influxr_write(
  x = dummy_upload,
  client = tss_client,
  measurement = 'test1',
  database = 'test',
  precision = 'ms',
  missing = c(NA, -9999),
  tags = tags,
  timestamp_format='%Y-%m-%d',
  source_tz = "UTC", # 可按需替换为实际使用时区,比如"Asia/Shanghai"
  timestamp = 1, 
  verbose = TRUE)

内容的提问来源于stack exchange,提问作者learning-man

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 07:39:01