Skip to content

Commit

Permalink
Add cypherl output format (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
gitbuda committed May 20, 2023
1 parent 58e3b8d commit 120af6f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ memgraph> :quit
Bye
```

## Export & import into Memgraph

An interesting use-case for `mgconsole` is exporting and importing data.
You can close the loop by running the following example queries:

```
# Export to cypherl formatted data file
echo "DUMP DATABASE;" | mgconsole --output-format=cypherl > data.cypherl
# Import from cypherl file
cat data.cypherl | mgconsole
```

## Batched and parallelized import (EXPERIMENTAL)

Since Memgraph v2 expects vertices to come first (vertices has to exist to
Expand All @@ -137,7 +150,7 @@ leverage parallelism in the best possible way.

```
cat data.cypherl | mgconsole --import-mode=batched-parallel
// STORAGE MODE IN_MEMORY_ANALYTICAL; is optional
# STORAGE MODE IN_MEMORY_ANALYTICAL; is optional
```

IMPORTANT NOTE: Inside the import file, vertices always have to come first
Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ DEFINE_bool(use_ssl, false, "Use SSL when connecting to the server.");
DEFINE_bool(fit_to_screen, false, "Fit output width to screen width.");
DEFINE_bool(term_colors, false, "Use terminal colors syntax highlighting.");
DEFINE_string(output_format, "tabular",
"Query output format can be csv or tabular. If output format is "
"Query output format can be csv, tabular or cypherl. If output format is "
"not tabular `fit-to-screen` flag is ignored.");
DEFINE_bool(verbose_execution_info, false,
"Output the additional information about query such as query cost, parsing, planning and execution times.");
DEFINE_validator(output_format, [](const char *, const std::string &value) {
if (value == constants::kCsvFormat || value == constants::kTabularFormat) {
if (value == constants::kCsvFormat || value == constants::kTabularFormat || value == constants::kCypherlFormat) {
return true;
}
return false;
Expand Down
1 change: 1 addition & 0 deletions src/utils/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ constexpr const std::string_view kCommandDocs = ":docs";
// Supported formats.
constexpr const std::string_view kCsvFormat = "csv";
constexpr const std::string_view kTabularFormat = "tabular";
constexpr const std::string_view kCypherlFormat = "cypherl";

// Supported modes.
constexpr const std::string_view kSerialMode = "serial";
Expand Down
16 changes: 16 additions & 0 deletions src/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1186,12 +1186,28 @@ void PrintCsv(const std::vector<std::string> &header, const std::vector<mg_memor
}
}

void PrintCypherl(const std::vector<std::string> &header, const std::vector<mg_memory::MgListPtr> &records) {
if (header.size() != 1) {
std::cerr << "ERROR: cypherl output format requires exactly 1 output column" << std::endl;
std::exit(1);
}
for (size_t record_i = 0; record_i < records.size(); ++record_i) {
const auto &fields = records[record_i];
for (uint32_t field_i = 0; field_i < mg_list_size(fields.get()); ++field_i) {
const auto *query_ptr = mg_value_string(mg_list_at(fields.get(), field_i));
std::cout << std::string(mg_string_data(query_ptr), mg_string_size(query_ptr)) << std::endl;
}
}
}

void Output(const std::vector<std::string> &header, const std::vector<mg_memory::MgListPtr> &records,
const OutputOptions &out_opts, const CsvOptions &csv_opts) {
if (out_opts.output_format == constants::kTabularFormat) {
PrintTabular(header, records, out_opts.fit_to_screen);
} else if (out_opts.output_format == constants::kCsvFormat) {
PrintCsv(header, records, csv_opts);
} else if (out_opts.output_format == constants::kCypherlFormat) {
PrintCypherl(header, records);
}
}

Expand Down

0 comments on commit 120af6f

Please sign in to comment.