我正在测试INFUXR以从R:写入INFXDB:
https://github.com/influxr/influxr
以下示例摘自GitHub自述文件并运行正常:
# Load the influxr package
library(influxr)
# Define a client to hold connection parameters to your server running the influxDB database instance. Here you can also provide username and password if authentication is needed.
tss_client <- influxr_connection(host = 'localhost', ssl = FALSE)
# Test the connection
ping(tss_client)
# Generate random data with different data types
# Number of data points to generate
n = 1e5
# Create dummy data.frame for uploading
dummy_upload <-
data.frame(
time = Sys.time() + 1:n,
Temp = round(rnorm(n) * 50, digits = 2),
H = rnorm (n),
DOY = floor(runif(n, 0, 365)),
Code = replicate(n, paste0(sample(LETTERS, 4), collapse = ''))
)
# Define meta data, knows as tags, for uploading to influxDB
tags <-
c(
station = 'New York',
instrument = 'weather1'
)
# Upload the data with the meta data
res <- influxr_write(
x = dummy_upload,
client = tss_client,
measurement = 'test1',
database = 'test',
precision = 'ms',
missing = c(NA, -9999),
tags = tags,
timestamp = 1, verbose = TRUE)
我仅使用一系列日期进行了更改:
n1 = 1e2
# Create dummy data.frame for uploading
dummy_upload <-
data.frame(
time = seq(as.Date('2015-09-15'), by = "1 days", length.out=n1),
Temp = round(rnorm(n) * 50, digits = 2),
H = rnorm (n),
DOY = floor(runif(n, 0, 365)),
Code = replicate(n, paste0(sample(LETTERS, 4), collapse = ''))
)
head(dummy_upload)
# Define meta data, knows as tags, for uploading to influxDB
tags <-
c(
station = 'Houston',
instrument = 'weather2'
)
# Upload the data with the meta data
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',
timestamp = 1,
verbose = TRUE)
我得到以下输出:
和以下错误:
Format_Timestamp(x,Timestamp,Timestamp_Format,SOURCE_TZ= TZ,:无法使用提供的时间戳。有人知道问题出在哪里吗?
这个可爱的包裹的管理员通过电子邮件回复了我,并给了我几个提示。 我的代码在第二部分中有一些问题,因为它在某些部分仍然使用n,而它应该使用n1。 但我的方法中最重要的问题是,R使用as.Date和Time戳不同地处理日期。而且,因为impxDB期望时间戳中包含时间部分。一种解决办法是使用
将日期转换为POSIXctas.POSIXct(seq(as.Date('2015-09-15'), by = "1 days", length.out=10))
我在这里附加了我的代码的完整版本:
# Load the influxr package
library(influxr)
# Define a client to hold connection parameters to your server running the influxDB database instance. Here you can also provide username and password if authentication is needed.
tss_client <- influxr_connection(host = 'localhost', ssl = FALSE)
# Test the connection
ping(tss_client)
# Generate random data with different data types
# Number of data points to generate
n = 1e5
# Create dummy data.frame for uploading
dummy_upload <-
data.frame(
time = Sys.time() + 1:n,
Temp = round(rnorm(n) * 50, digits = 2),
H = rnorm (n),
DOY = floor(runif(n, 0, 365)),
Code = replicate(n, paste0(sample(LETTERS, 4), collapse = ''))
)
# Define meta data, knows as tags, for uploading to influxDB
tags <-
c(
station = 'New York',
instrument = 'weather1'
)
# Upload the data with the meta data
res <- influxr_write(
x = dummy_upload,
client = tss_client,
measurement = 'test1',
database = 'test',
precision = 'ms',
missing = c(NA, -9999),
tags = tags,
timestamp = 1, verbose = TRUE)
n1 = 1e2
# Create dummy data.frame for uploading
dummy_upload <-
data.frame(
time = as.POSIXct(seq(as.Date('2015-09-15'), by = "2 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)
# Define meta data, knows as tags, for uploading to influxDB
tags <-
c(
station = 'Houston',
instrument = 'weather2'
)
# Upload the data with the meta data
res <- influxr_write(
x = dummy_upload,
client = tss_client,
measurement = 'test2',
database = 'test',
precision = 'ms',
missing = c(NA, -9999),
tags = tags,
timestamp_format='%Y-%m-%d %H:%M:%S',
timestamp = 1,
verbose = TRUE)