-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a simpler version to read bson data
- Loading branch information
Showing
4 changed files
with
71 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,24 @@ | ||
#' Standalone BSON reader | ||
#' | ||
#' Utility to parse BSON data into R without using MongoDB. Useful to read data | ||
#' from a `mongoexport` dump without mongodb if it fits in memory. | ||
#' Utility to read BSON data into R without MongoDB. Useful to read a dump from | ||
#' from a `mongoexport` dump if it fits in memory. This utility does not attempt | ||
#' to convert data into a data.frame: the output is a vector with length equal | ||
#' to the number of documents in the collection. | ||
#' | ||
#' To import a bson dump into a local mongodb server, use the [mongo$import][mongo] | ||
#' function instead. This requires less memory and once data is in mongo you can | ||
#' easily query it. | ||
#' Alternatively, to import a bson dump into your local mongodb server, use the | ||
#' [mongo$import][mongo] function instead. This requires little memory and once | ||
#' data is in mongo you can easily query it. | ||
#' | ||
#' @export | ||
#' @useDynLib mongolite R_bson_reader_new R_bson_reader_read | ||
#' @param con either a path to a file, a url, or a a connection object | ||
#' @param as_json return data as json strings instead of R lists | ||
#' @param verbose print some output as we read | ||
#' @return list with either data objects or json strings | ||
#' @useDynLib mongolite R_bson_reader_file | ||
#' @param file path to a bson file on disk | ||
#' @param as_json read data into json strings instead of R lists. | ||
#' @param verbose print some progress output while reading | ||
#' @examples | ||
#' diamonds <- read_bson("http://jeroen.github.io/data/diamonds.bson") | ||
read_bson <- function(con, as_json = FALSE, verbose = interactive()){ | ||
if(length(con) && is.character(con)){ | ||
con <- if(grepl("^https?://", con)){ | ||
url(con) | ||
} else { | ||
file(normalizePath(con, mustWork = TRUE), raw = TRUE) | ||
} | ||
} | ||
stopifnot(inherits(con, 'connection')) | ||
open(con, 'rb') | ||
on.exit(close(con)) | ||
reader <- .Call(R_bson_reader_new, con) | ||
output <- new.env(parent = emptyenv()) | ||
i <- 0 | ||
one <- function(as_json = FALSE){ | ||
.Call(R_bson_reader_read, reader, as_json) | ||
} | ||
while(length(obj <- one(as_json))){ | ||
i <- i+1 | ||
if(isTRUE(verbose)) | ||
cat("\rRead", i, file = stderr()) | ||
output[[sprintf('%09d', i)]] <- obj | ||
} | ||
if(isTRUE(verbose)) | ||
cat("\rDone!\n", file = stderr()) | ||
unname(as.list(output, sorted = TRUE)) | ||
#' download.file("http://jeroen.github.io/data/diamonds.bson", "diamonds.bson") | ||
#' diamonds <- read_bson("diamonds.bson") | ||
#' unlink("diamonds.bson") | ||
read_bson <- function(file, as_json = FALSE, verbose = interactive()){ | ||
file <- normalizePath(file, mustWork = TRUE) | ||
.Call(R_bson_reader_file, file, as_json, verbose) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters