我在 R 中有许多大型数据帧,我计划使用 redis 进行存储。我对 redis 完全陌生,但今天一直在阅读它并且一直在使用 R 包 rredis
。
我一直在研究小数据,并使用 redisSet()
和 redisGet()
函数保存和检索小数据帧。但是,当涉及到保存较大的数据帧时(其中最大的数据帧为 430 万行,另存为 .RData 文件时为 365MB)
使用代码 redisSet('bigDF', bigDF)
我收到以下错误消息:
Error in doTryCatch(return(expr), name, parentenv, handler) :
ERR Protocol error: invalid bulk length
In addition: Warning messages:
1: In writeBin(v, con) : problem writing to connection
2: In writeBin(.raw("\r\n"), con) : problem writing to connection
大概是因为dataframe太大无法保存。我知道 redisSet 将数据帧写为字符串,这可能不是处理大型数据帧的最佳方式。有谁知道最好的方法吗?
编辑:我在创建一个非常大的虚拟数据框时重现了错误:
bigDF <- data.frame(
'lots' = rep('lots',40000000),
'of' = rep('of',40000000),
'data' = rep('data',40000000),
'here'=rep('here',40000000)
)
运行 redisSet('bigDF',bigDF)
给我错误:
Error in .redisError("Invalid agrument") : Invalid agrument
第一次,然后立即再次运行我得到错误
Error in doTryCatch(return(expr), name, parentenv, handler) :
ERR Protocol error: invalid bulk length
In addition: Warning messages:
1: In writeBin(v, con) : problem writing to connection
2: In writeBin(.raw("\r\n"), con) : problem writing to connection
谢谢
最佳答案
简而言之:您不能。 Redis can store a maximum of 512 Mb of data in a String value并且您的序列化演示数据框比那个大:
> length(serialize(bigDF, connection = NULL)) / 1024 / 1024
[1] 610.352
技术背景:
serialize
是通过redisSet
和rredis:::.redisCmd
在包的.cerealize
函数中调用的>:
> rredis:::.cerealize
function (value)
{
if (!is.raw(value))
serialize(value, ascii = FALSE, connection = NULL)
else value
}
<environment: namespace:rredis>
题外话:你为什么要把这么大的数据集存储在 redis 中? Redis 适用于小型键值对。另一方面,我在 CouchDB 中存储了一些大的 R 数据集。和 MongoDB (使用 GridFS)通过将压缩的 RData
作为附件添加到那里。
关于r - 通过R在redis中存储大数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16108544/