csv_to_tql_insert_script ( WORK IN PROGRESS )
4 JAN 2022: I ran into an issue where Pandas was coercing integers into floats; so far the only fix I’ve found for this (that wouldn’t significantly complicate user experience) causes more problems than it solves, so I’m looking into a non-Pandas-based solution.
Automatically generates a TypeQL script for doing entity and relationship insertions from a .csv file, so you don’t have to mess with writing TypeQL.
Each row represents either an entity or a relationship.
3 columns are mandatory , the rest contain the attribute and role names defined in your schema.
ent_or_rel– the script uses this to figure out if it’s supposed to be making an entity or a relationship for this row of data. You can write
relation. Capitalization doesn’t matter- just the letters
rel. You can even write
entropyand it’ll recognize the
entas meaning this is an entity row.
sub_type– defines what kind of entity or relationship is in this row . For example, if you have a
personentity type in your schema, and this row is for a person, then you put
alias– this is the typeQL-style variable you will use to represent this entity. e.g.
$x. Each row must a unique one in this column .
Other Column Names
The rest of the column names must correspond to your schema’s
The values in your role columns ( in my example
client) must correspond to the aliases you used when defining your entities.
Column order is not important. The script will figure out which columns contain attributes, and which ones contain info describing roles in a relationship.
is important, but no more than it is in any TypeQL script. Just make sure you define your entities before you start making links between them.
HOW TO USE
- Install the Python package
conda install pandas -y
pip3 install pandas
- Have your database & schema created already. This only adds entities and relations to a pre-existing database with a schema loaded.
- Save this script somewhere on your computer. It has been tested with Python3.6, but it should work with any Python3.x. If it doesn’t, let me know.
- EXECUTE THE SCRIPT LIKE THIS:
python3 path/to/data.csv path/to/desired_script_name.tql
- Launch your TypeDB console (method varies depending on your install)
> transaction <database_name> data write
> source /path/to/desired_script_name.tql
Voila, your data is now in your database.