import ndjson into R skip first n lines












0















How to read a big ndjson (20GB) file by chunk into R?



I have a big data file that I want to read 1M rows at a time.



currently, I'm using below code to load data into R.



jsonlite::stream_in(
file(fileName)
)


But I don't need to load all data together. how can I split this file to chunk to load faster?










share|improve this question

























  • I'd highly suggest using Apache Drill along with the sergeant package rud.is/books/drill-sergeant-rstats/…. In fact, I'd highly suggest converting the ndjson to parquet with Drill and use the parquet version in Drill via the sergeant package.

    – hrbrmstr
    Nov 12 '18 at 20:51


















0















How to read a big ndjson (20GB) file by chunk into R?



I have a big data file that I want to read 1M rows at a time.



currently, I'm using below code to load data into R.



jsonlite::stream_in(
file(fileName)
)


But I don't need to load all data together. how can I split this file to chunk to load faster?










share|improve this question

























  • I'd highly suggest using Apache Drill along with the sergeant package rud.is/books/drill-sergeant-rstats/…. In fact, I'd highly suggest converting the ndjson to parquet with Drill and use the parquet version in Drill via the sergeant package.

    – hrbrmstr
    Nov 12 '18 at 20:51
















0












0








0








How to read a big ndjson (20GB) file by chunk into R?



I have a big data file that I want to read 1M rows at a time.



currently, I'm using below code to load data into R.



jsonlite::stream_in(
file(fileName)
)


But I don't need to load all data together. how can I split this file to chunk to load faster?










share|improve this question
















How to read a big ndjson (20GB) file by chunk into R?



I have a big data file that I want to read 1M rows at a time.



currently, I'm using below code to load data into R.



jsonlite::stream_in(
file(fileName)
)


But I don't need to load all data together. how can I split this file to chunk to load faster?







r ndjson






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 21:46









hrbrmstr

60.4k687148




60.4k687148










asked Nov 12 '18 at 20:43









Mahdi JadalihaMahdi Jadaliha

1,1561018




1,1561018













  • I'd highly suggest using Apache Drill along with the sergeant package rud.is/books/drill-sergeant-rstats/…. In fact, I'd highly suggest converting the ndjson to parquet with Drill and use the parquet version in Drill via the sergeant package.

    – hrbrmstr
    Nov 12 '18 at 20:51





















  • I'd highly suggest using Apache Drill along with the sergeant package rud.is/books/drill-sergeant-rstats/…. In fact, I'd highly suggest converting the ndjson to parquet with Drill and use the parquet version in Drill via the sergeant package.

    – hrbrmstr
    Nov 12 '18 at 20:51



















I'd highly suggest using Apache Drill along with the sergeant package rud.is/books/drill-sergeant-rstats/…. In fact, I'd highly suggest converting the ndjson to parquet with Drill and use the parquet version in Drill via the sergeant package.

– hrbrmstr
Nov 12 '18 at 20:51







I'd highly suggest using Apache Drill along with the sergeant package rud.is/books/drill-sergeant-rstats/…. In fact, I'd highly suggest converting the ndjson to parquet with Drill and use the parquet version in Drill via the sergeant package.

– hrbrmstr
Nov 12 '18 at 20:51














1 Answer
1






active

oldest

votes


















1














If you don't want to level-up and use Drill, this will work on any system zcat (or gzcat) and sed live:



stream_in_range <- function(infile, start, stop, cat_kind = c("gzcat", "zcat")) {

infile <- path.expand(infile)
stopifnot(file.exists(infile))

gzip <- (tools::file_ext(infile) == "gz")
if (gzip) cat_kind <- match.arg(cat_kind, c("gzcat", "zcat"))

start <- as.numeric(start[1])
stop <- as.numeric(stop[1])

sed_arg <- sprintf("%s,%sp;", start, stop, (stop+1))

sed_command <- sprintf("sed -n '%s'", sed_arg)

if (gzip) {
command <- sprintf("%s %s | %s ", cat_kind, infile, sed_command)
} else {
command <- sprintf("%s %s", sed_command, infile)
}

ndjson::flatten(system(command, intern=TRUE), "tbl")

}

stream_in_range("a-big-compressed-ndjson-file.json.gz", 100, 200)

stream_in_range("a-big-uncompressed-nsjdon-file.json", 1, 10)


Choose and/or add a different cat_kind for whatever works for you.






share|improve this answer



















  • 1





    Thanks, @hrbrmstr. your answer helped a lot. I add this here for Windows users that you may install Cygwin as well to use sed function in system(command, intern=TRUE). I have one more question: I see that you are the author of ndjson. Is this possible to add skip parameters to ndjson::stream_in function?

    – Mahdi Jadaliha
    Nov 13 '18 at 18:20











  • sure. can you post that as an issue? i thought Rtools.exe install came w/sed but I don't run Windows so I'm very likely wrong abt that.

    – hrbrmstr
    Nov 13 '18 at 18:53













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269796%2fimport-ndjson-into-r-skip-first-n-lines%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














If you don't want to level-up and use Drill, this will work on any system zcat (or gzcat) and sed live:



stream_in_range <- function(infile, start, stop, cat_kind = c("gzcat", "zcat")) {

infile <- path.expand(infile)
stopifnot(file.exists(infile))

gzip <- (tools::file_ext(infile) == "gz")
if (gzip) cat_kind <- match.arg(cat_kind, c("gzcat", "zcat"))

start <- as.numeric(start[1])
stop <- as.numeric(stop[1])

sed_arg <- sprintf("%s,%sp;", start, stop, (stop+1))

sed_command <- sprintf("sed -n '%s'", sed_arg)

if (gzip) {
command <- sprintf("%s %s | %s ", cat_kind, infile, sed_command)
} else {
command <- sprintf("%s %s", sed_command, infile)
}

ndjson::flatten(system(command, intern=TRUE), "tbl")

}

stream_in_range("a-big-compressed-ndjson-file.json.gz", 100, 200)

stream_in_range("a-big-uncompressed-nsjdon-file.json", 1, 10)


Choose and/or add a different cat_kind for whatever works for you.






share|improve this answer



















  • 1





    Thanks, @hrbrmstr. your answer helped a lot. I add this here for Windows users that you may install Cygwin as well to use sed function in system(command, intern=TRUE). I have one more question: I see that you are the author of ndjson. Is this possible to add skip parameters to ndjson::stream_in function?

    – Mahdi Jadaliha
    Nov 13 '18 at 18:20











  • sure. can you post that as an issue? i thought Rtools.exe install came w/sed but I don't run Windows so I'm very likely wrong abt that.

    – hrbrmstr
    Nov 13 '18 at 18:53


















1














If you don't want to level-up and use Drill, this will work on any system zcat (or gzcat) and sed live:



stream_in_range <- function(infile, start, stop, cat_kind = c("gzcat", "zcat")) {

infile <- path.expand(infile)
stopifnot(file.exists(infile))

gzip <- (tools::file_ext(infile) == "gz")
if (gzip) cat_kind <- match.arg(cat_kind, c("gzcat", "zcat"))

start <- as.numeric(start[1])
stop <- as.numeric(stop[1])

sed_arg <- sprintf("%s,%sp;", start, stop, (stop+1))

sed_command <- sprintf("sed -n '%s'", sed_arg)

if (gzip) {
command <- sprintf("%s %s | %s ", cat_kind, infile, sed_command)
} else {
command <- sprintf("%s %s", sed_command, infile)
}

ndjson::flatten(system(command, intern=TRUE), "tbl")

}

stream_in_range("a-big-compressed-ndjson-file.json.gz", 100, 200)

stream_in_range("a-big-uncompressed-nsjdon-file.json", 1, 10)


Choose and/or add a different cat_kind for whatever works for you.






share|improve this answer



















  • 1





    Thanks, @hrbrmstr. your answer helped a lot. I add this here for Windows users that you may install Cygwin as well to use sed function in system(command, intern=TRUE). I have one more question: I see that you are the author of ndjson. Is this possible to add skip parameters to ndjson::stream_in function?

    – Mahdi Jadaliha
    Nov 13 '18 at 18:20











  • sure. can you post that as an issue? i thought Rtools.exe install came w/sed but I don't run Windows so I'm very likely wrong abt that.

    – hrbrmstr
    Nov 13 '18 at 18:53
















1












1








1







If you don't want to level-up and use Drill, this will work on any system zcat (or gzcat) and sed live:



stream_in_range <- function(infile, start, stop, cat_kind = c("gzcat", "zcat")) {

infile <- path.expand(infile)
stopifnot(file.exists(infile))

gzip <- (tools::file_ext(infile) == "gz")
if (gzip) cat_kind <- match.arg(cat_kind, c("gzcat", "zcat"))

start <- as.numeric(start[1])
stop <- as.numeric(stop[1])

sed_arg <- sprintf("%s,%sp;", start, stop, (stop+1))

sed_command <- sprintf("sed -n '%s'", sed_arg)

if (gzip) {
command <- sprintf("%s %s | %s ", cat_kind, infile, sed_command)
} else {
command <- sprintf("%s %s", sed_command, infile)
}

ndjson::flatten(system(command, intern=TRUE), "tbl")

}

stream_in_range("a-big-compressed-ndjson-file.json.gz", 100, 200)

stream_in_range("a-big-uncompressed-nsjdon-file.json", 1, 10)


Choose and/or add a different cat_kind for whatever works for you.






share|improve this answer













If you don't want to level-up and use Drill, this will work on any system zcat (or gzcat) and sed live:



stream_in_range <- function(infile, start, stop, cat_kind = c("gzcat", "zcat")) {

infile <- path.expand(infile)
stopifnot(file.exists(infile))

gzip <- (tools::file_ext(infile) == "gz")
if (gzip) cat_kind <- match.arg(cat_kind, c("gzcat", "zcat"))

start <- as.numeric(start[1])
stop <- as.numeric(stop[1])

sed_arg <- sprintf("%s,%sp;", start, stop, (stop+1))

sed_command <- sprintf("sed -n '%s'", sed_arg)

if (gzip) {
command <- sprintf("%s %s | %s ", cat_kind, infile, sed_command)
} else {
command <- sprintf("%s %s", sed_command, infile)
}

ndjson::flatten(system(command, intern=TRUE), "tbl")

}

stream_in_range("a-big-compressed-ndjson-file.json.gz", 100, 200)

stream_in_range("a-big-uncompressed-nsjdon-file.json", 1, 10)


Choose and/or add a different cat_kind for whatever works for you.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 '18 at 21:46









hrbrmstrhrbrmstr

60.4k687148




60.4k687148








  • 1





    Thanks, @hrbrmstr. your answer helped a lot. I add this here for Windows users that you may install Cygwin as well to use sed function in system(command, intern=TRUE). I have one more question: I see that you are the author of ndjson. Is this possible to add skip parameters to ndjson::stream_in function?

    – Mahdi Jadaliha
    Nov 13 '18 at 18:20











  • sure. can you post that as an issue? i thought Rtools.exe install came w/sed but I don't run Windows so I'm very likely wrong abt that.

    – hrbrmstr
    Nov 13 '18 at 18:53
















  • 1





    Thanks, @hrbrmstr. your answer helped a lot. I add this here for Windows users that you may install Cygwin as well to use sed function in system(command, intern=TRUE). I have one more question: I see that you are the author of ndjson. Is this possible to add skip parameters to ndjson::stream_in function?

    – Mahdi Jadaliha
    Nov 13 '18 at 18:20











  • sure. can you post that as an issue? i thought Rtools.exe install came w/sed but I don't run Windows so I'm very likely wrong abt that.

    – hrbrmstr
    Nov 13 '18 at 18:53










1




1





Thanks, @hrbrmstr. your answer helped a lot. I add this here for Windows users that you may install Cygwin as well to use sed function in system(command, intern=TRUE). I have one more question: I see that you are the author of ndjson. Is this possible to add skip parameters to ndjson::stream_in function?

– Mahdi Jadaliha
Nov 13 '18 at 18:20





Thanks, @hrbrmstr. your answer helped a lot. I add this here for Windows users that you may install Cygwin as well to use sed function in system(command, intern=TRUE). I have one more question: I see that you are the author of ndjson. Is this possible to add skip parameters to ndjson::stream_in function?

– Mahdi Jadaliha
Nov 13 '18 at 18:20













sure. can you post that as an issue? i thought Rtools.exe install came w/sed but I don't run Windows so I'm very likely wrong abt that.

– hrbrmstr
Nov 13 '18 at 18:53







sure. can you post that as an issue? i thought Rtools.exe install came w/sed but I don't run Windows so I'm very likely wrong abt that.

– hrbrmstr
Nov 13 '18 at 18:53




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269796%2fimport-ndjson-into-r-skip-first-n-lines%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation