MySQL data
You can use the Dgraph migration tool to convert a MySQL database tables into a schema and RDF file, and then load the resulting dataset into Dgraph.
Deriving a Dgraph schema from SQL
Before converting the data, the migration tool needs to derive the schema of each predicate. Dgraph follows two simple rules for converting the schema:
- For plain attributes, there is usually a one-to-one mapping between a SQL data type and the
Dgraph datatype. For instance, a
Bodycolumn in thePoststable is of typetext, and hence, the predicateposts.Bodyis of typestring:posts.Body: string . - The predicates representing inter-object relationships, like
posts.OwnerUserId., simply have the type[uid], meaning following the predicate leads us to a set of other objects.
Using the Migration tool
You can run the Dgraph migrate tool using this command:
dgraph migrate [flags]
-
Create a
config.propertiesfile that has the following settings and values should not be in quotes:user = <SQL_DB_USERNAME>
password = <SQL_DB_PASSWORD>
db = <SQL_DB> -
Export the SQL database into
schema.txtandsql.rdffile:dgraph migrate --config config.properties --output_schema schema.txt --output_data sql.rdfAn output similar to this appears:
Dumping table xyz
Dumping table constraints xyz
...
If you are connecting to a remote DB hosted on AWS, GCP, and others, you need to pass the flags --host, and --port.
For description of the various flags in the migration tool, see command line options.
After the migration is complete, two new files are available:
- an RDF file
sql.rdfcontaining all the N-Quad entries - a schema file
schema.txt.
Importing the data
The two files can then be imported into Dgraph using the Dgraph Live Loader
or Bulk Loader. Sometimes you might want to customize your schema.
For example, you might add an index to a predicate, or change an inter-object predicate (edge) from
unidirectional to bidirectional by adding the @reverse directive. If you would like such customizations, you should do it by editing the schema file generated by the migration tool before feeding the files to the Live Loader or Bulk Loader.
-
To import the data into Dgraph using the Live Loader to Dgraph Zero and Alpha servers running on the default ports use:
dgraph live -z localhost:5080 -a localhost:9080 --files sql.rdf --format=rdf --schema schema.txt -
To import data to Dgraph Cloud use:
dgraph live --slash_grpc_endpoint=<grpc-endpoint>:443 -f sql.rdf --format=rdf --schema schema.txt -t <api-token>For detailed instructions to import data to Dgraph cloud, see import data.