I have a .csv file that is 112GB in weight but neither vroom nor data.table::fread will open it. Even if I ask to read in 10 rows or just a couple of columns it complains with mapping error: Cannot allocate memory.
df<-data.table::fread("FINAL_data_Bus.csv", select = c(1:2),nrows=10)
System errno 22 unmapping file: Invalid argument
Error in data.table::fread("FINAL_data_Bus.csv", select = c(1:2), nrows = 10) :
Opened 112.3GB (120565605488 bytes) file ok but could not memory map it. This is a 64bit process. There is probably not enough contiguous virtual memory available.
read.csv on the other hand will read the ten rows happily.
Why won't vroom or fread read it using the usual altrep, even for 10 rows?
This matter has been discussed by the main creator of
data.tablepackage at https://github.com/Rdatatable/data.table/issues/3526. See the comment by Matt Dowle himself at https://github.com/Rdatatable/data.table/issues/3526#issuecomment-488364641. From what I understand, the gist of the matter is that to read even 10 lines from a huge csv file withfread, the entire file needs to be memory mapped. Sofreadcannot be used on its own in case your csv file is too big for your machine. Please correct me if I'm wrong.Also, I haven't been able to use
vroomwith big more-than-RAM csv files. Any pointers towards this end will be appreciated.For me, the most convenient way to check out a huge (gzipped) csv file is by using a small command line tool
csvtkfrom https://bioinf.shenwei.me/csvtk/e.g., check dimensions with
and, check out head with top 100 rows
get a better view of above with
Here I've used
lesscommand available with "Gnu On Windows" at https://github.com/bmatzelle/gowA word of caution - many people suggest using command
to check out no. of
linesfrom a big csv file. In most cases, it will be equal to the no. ofrows. But in case the big csv file contains newline characters within a cell, to use a spreadsheet term, the above command will not show the no. ofrows. In such cases the no. oflinesis different from the no. ofrows. So it is advisable to usecsvtk dimorcsvtk nrow. Other csv command line tools likexsv,millerwill also show correct results.Another word of caution - the short command
fread(cmd="head -n 10 BigFile.csv")is not advisable to preview top few lines in case some columns contain significant leading zeros in data such as 0301, 0542, etc. since without column specification,freadwill interpret them as integers and not show leading zeros from such columns. For example, in some databases that I have to analyse, the first digit zero in a particular column means that it is aRevenue Receipt. So better use a command line tool likecsvtk,miller,xsvwithless -SNfor previewing a big csv file which show the file "as is" without any potentially wrong interpretation.PS1: Even spreadsheets like MS Excel and LibreOffice Calc loses leading zeroes in csv files by default. LibreOffice Calc actually shows leading zeroes in the preview window but loses them when you load the file! I'm yet to find a spreadsheet that does not lose leading zeroes in csv files by default.
PS2: I've posted my approach to querying very large csv files at https://stackoverflow.com/a/68693819/8079808
EDIT:
VROOM does have difficulty when dealing with huge files since it needs to store the index in memory as well as any data you read from the file. See development thread https://github.com/r-lib/vroom/issues/203