Skip to content

Commit

Permalink
[ARROW-4409] Add convenience to parse JSON from file
Browse files Browse the repository at this point in the history
  • Loading branch information
David Li authored and David Li committed Feb 5, 2019
1 parent 111b3e6 commit 3e185cb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
12 changes: 3 additions & 9 deletions cpp/src/arrow/flight/test-integration-client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,11 @@ int main(int argc, char** argv) {

// 1. Put the data to the server.
std::unique_ptr<arrow::ipc::internal::json::JsonReader> reader;
std::shared_ptr<arrow::io::ReadableFile> in_file;
std::cout << "Opening JSON file '" << FLAGS_path << "'" << std::endl;
std::shared_ptr<arrow::io::ReadableFile> in_file;
ABORT_NOT_OK(arrow::io::ReadableFile::Open(FLAGS_path, &in_file));

int64_t file_size = 0;
ABORT_NOT_OK(in_file->GetSize(&file_size));

std::shared_ptr<arrow::Buffer> json_buffer;
ABORT_NOT_OK(in_file->Read(file_size, &json_buffer));

ABORT_NOT_OK(arrow::ipc::internal::json::JsonReader::Open(json_buffer, &reader));
ABORT_NOT_OK(arrow::ipc::internal::json::JsonReader::Open(arrow::default_memory_pool(),
in_file, &reader));

std::unique_ptr<arrow::ipc::RecordBatchWriter> write_stream;
ABORT_NOT_OK(client->DoPut(descr, reader->schema(), &write_stream));
Expand Down
13 changes: 13 additions & 0 deletions cpp/src/arrow/ipc/json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <string>

#include "arrow/buffer.h"
#include "arrow/io/file.h"
#include "arrow/ipc/json-internal.h"
#include "arrow/memory_pool.h"
#include "arrow/record_batch.h"
Expand Down Expand Up @@ -157,6 +158,18 @@ Status JsonReader::Open(MemoryPool* pool, const std::shared_ptr<Buffer>& data,
return (*reader)->impl_->ParseAndReadSchema();
}

Status JsonReader::Open(MemoryPool* pool,
const std::shared_ptr<io::ReadableFile>& in_file,
std::unique_ptr<JsonReader>* reader) {
int64_t file_size = 0;
RETURN_NOT_OK(in_file->GetSize(&file_size));

std::shared_ptr<arrow::Buffer> json_buffer;
RETURN_NOT_OK(in_file->Read(file_size, &json_buffer));

return Open(pool, json_buffer, reader);
}

std::shared_ptr<Schema> JsonReader::schema() const { return impl_->schema(); }

int JsonReader::num_record_batches() const { return impl_->num_record_batches(); }
Expand Down
13 changes: 13 additions & 0 deletions cpp/src/arrow/ipc/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class MemoryPool;
class RecordBatch;
class Schema;

namespace io {
class ReadableFile;
} // namespace io

namespace ipc {
namespace internal {
namespace json {
Expand Down Expand Up @@ -95,6 +99,15 @@ class ARROW_EXPORT JsonReader {
static Status Open(const std::shared_ptr<Buffer>& data,
std::unique_ptr<JsonReader>* reader);

/// \brief Create a new JSON reader from a file
///
/// \param[in] pool a MemoryPool to use for buffer allocations
/// \param[in] in_file a ReadableFile containing JSON data
/// \param[out] reader the returned reader object
/// \return Status
static Status Open(MemoryPool* pool, const std::shared_ptr<io::ReadableFile>& in_file,
std::unique_ptr<JsonReader>* reader);

/// \brief Return the schema read from the JSON
std::shared_ptr<Schema> schema() const;

Expand Down

0 comments on commit 3e185cb

Please sign in to comment.