如何保存具有不同文件名(KDB)的大文件?
How to save large file with different file name (KDB)?
我在名为 "data" 的变量中有一个稍大的 table(保存时大约 3GB)。
我可以将它保存到文件中:
save `:data.csv
但是,如果我尝试用不同的文件名保存它,则会出现错误:
sd: 2017.12.08;
string[sd],".csv" set data
ERROR: 'limit
(tried to generate a list with more than 2,000,000,000 elements (keep in mind that any IPC result is a byte list, hence can't be longer than 2 metric Gb))
如何克服这个错误?我尝试了多种方法,我确信这很简单,但找不到解决方法。
您应该可以通过批量而不是批量写入文件来绕过限制。下面是一个允许您这样做的函数。该函数采用 3 个参数:批量大小 n
、文件名 f
和要写入文件的 data
。它打开文件句柄,使用 hcount
检查文件是否为空,并在必要时写入列 headers。然后批量保存数据,完成后关闭文件句柄。
chunk:{[n;f;data]
h:hopen f; / open file handle
if[0=hcount f;h","sv string cols data]; / write headers to empty file
{x raze"\n",/:1_","0:y}[h]'[n cut data]; / write chunks to file
:hclose h; / close file handle
};
此函数非常简单,因为它不检查模式,因此在将混合模式传递给它时可能会导致问题。
set will save the table in binary format, not csv. Use 0:(由 save
内部使用)用不同的文件名以 csv 格式保存 table:
q)hsym[`$string[sd],".csv"] 0: csv 0: data
`:2017.12.08.csv
我在名为 "data" 的变量中有一个稍大的 table(保存时大约 3GB)。
我可以将它保存到文件中:
save `:data.csv
但是,如果我尝试用不同的文件名保存它,则会出现错误:
sd: 2017.12.08;
string[sd],".csv" set data
ERROR: 'limit
(tried to generate a list with more than 2,000,000,000 elements (keep in mind that any IPC result is a byte list, hence can't be longer than 2 metric Gb))
如何克服这个错误?我尝试了多种方法,我确信这很简单,但找不到解决方法。
您应该可以通过批量而不是批量写入文件来绕过限制。下面是一个允许您这样做的函数。该函数采用 3 个参数:批量大小 n
、文件名 f
和要写入文件的 data
。它打开文件句柄,使用 hcount
检查文件是否为空,并在必要时写入列 headers。然后批量保存数据,完成后关闭文件句柄。
chunk:{[n;f;data]
h:hopen f; / open file handle
if[0=hcount f;h","sv string cols data]; / write headers to empty file
{x raze"\n",/:1_","0:y}[h]'[n cut data]; / write chunks to file
:hclose h; / close file handle
};
此函数非常简单,因为它不检查模式,因此在将混合模式传递给它时可能会导致问题。
set will save the table in binary format, not csv. Use 0:(由 save
内部使用)用不同的文件名以 csv 格式保存 table:
q)hsym[`$string[sd],".csv"] 0: csv 0: data
`:2017.12.08.csv