Import CSV Data
We’re overhauling Dgraph’s docs to make them clearer and more approachable. If you notice any issues during this transition or have suggestions, please let us know.
Convert CSV to JSON
There are many tools available to convert CSV to JSON. You can import large data
sets to Dgraph using Dgraph Live Loader or
Dgraph Bulk Loader. In these examples, the
csv2json
tool is used, and the data is imported using the Mutate tab in
Ratel.
Before you begin
- Install
csv2json
conversion tool. - Install
jq
a lightweight and flexible command-line JSON processor. - Connect the Dgraph instance to Ratel for queries, mutations and visualizations.
Example 1
-
Create a
names.csv
file with these details: -
Change to the directory that contains the
names.csv
file and convert it tonames.json
: -
To prettify a JSON file, use the
jq '.'
command:The output is similar to:
This JSON file follows the JSON Mutation Format, it can be loaded into Dgraph using Dgraph Live Loader , Dgraph Bulk Loader or the programmatic clients.
-
To load the data to Ratel and HTTP clients. The JSON data has to be stored within the
"set"
key. You can usejq
to transform the JSON into the correct format:An output similar to this appears:
-
Paste the output in the Mutate tab of Console in Ratel.
-
Click Run to import data.
-
To view the imported data paste the following in the Query tab and click Run:
Example 2
-
Create a
connects.csv
file that’s connecting nodes together. Theconnects
field should be of theuid
type. -
To get the correct JSON format, you can convert the CSV into JSON and use
jq
to transform it in the correct format where theconnects
edge is a nodeuid
. This JSON file can be loaded into Dgraph using the programmatic clients.The output is similar to:
-
To get an output of the mutation format accepted in Ratel UI and HTTP clients:
The output is similar to:
To reuse existing integer IDs from a CSV file as UIDs in Dgraph, use Dgraph Zero’s assign endpoint before loading data to allocate a range of UIDs that can be safely assigned.
-
Paste the output in the Mutate tab of Console in Ratel, and click Run to import data.
Was this page helpful?