Skip to main content

Class to extract, transform and load (ETL) dicts/json to a Neo4j graph

Project description

Dict2graph

Transfer (json compatible) Python dicts into a Neo4j graph database.
dict2graph also comes with some powerful data transform capabilities.

Maintainer: bleimehl@dzd-ev.de

Licence: MIT

public issue tracker: https://github.com/dzd-ev/dict2graph-docs/issues

user docs: https://dzd-ev.github.io/dict2graph-docs/

source code: https://git.connect.dzd-ev.de/dzdpythonmodules/dict2graph

important upstream modules:


Content:


Install

pip3 install dict2graph

What is dict2graph

With dict2graph you can transfer python dicts into a neo4j graph out of the box.
And if you are not happy with the structure of the result, dict2graph comes with a bunch of, transformation tools, which are easy to apply to your model.

Recommended workflow

The recommended workflow is:

  • Load your dict (or a sample of your larger datasets) as it is, with dict2graph into a neo4j test instance
  • Inspect the result in neo4j
  • Add dict2graph-transformators to shape your resulting graph model
  • Wipe your neo4j test instance
  • Repeat the work flow with the changed dict2graph-transformators until your happy with the result

What dict2graph is not

dict2graph can not be used for de-/serializing your dict into a graph database. There is no graph2dict functionality (nore is it planned to have one).
Your data/dict will be transformed to be more suitable in a graph represantation. On the way, certain informations can be lost. Reproducing the exact same dict from the graph is not possible in many cases.

py2neo depcrecation warning

In past versions of dict2graph, the awesome py2neo library was the only way to connect to a Neo4j instance.
But (sadly) this lib is in a low-maintanance mode. For now it is still supported but marked as deprecated. We recommend to switch to the official Neo4j Python Driver.

Basic Usage Example

With formalities out of the way, lets have some simple examples how dict2graph works...

Load a dict

from dict2graph import Dict2graph
from neo4j import GraphDatabase

# connect to our neo4j database
NEO4J_DRIVER = GraphDatabase.driver("neo4j://localhost")

# lets create a small random  dict
dic = {
    "Action": {
        "id": 1,
        "target": "El Oued",
        "Entities": [{"id": "Isabelle Eberhardt"}, {"id": "Slimène Ehnni"}],
    }
}
# create a dict2graph instance, 
# parse the dict 
# and load it into our neo4j instance.
Dict2graph().parse(dic).create(NEO4J_DRIVER)

This will result in following graph:

Transform the model

We now have loaded the dict data into a Neo4j Graph. But usally we dont need stuff like explicit lists in a graph. Also it is common to uppercase relationship types and capitalize labels.

So we need to make some adjustments to improve the graph represenation of the dict.
This is where dict2graph.Transformers come into play.

from dict2graph import Dict2graph, Transformer, NodeTrans, RelTrans
from py2neo import Graph
NEO4J_DRIVER = GraphDatabase.driver("neo4j://localhost")
data = {
    "Action": {
        "id": 1,
        "target": "El Oued",
        "Entities": [{"id": "Isabelle Eberhardt"}, {"id": "Slimène Ehnni"}],
    }
}
d2g = Dict2graph()
d2g.add_transformation(
    [
        Transformer.match_nodes().do(NodeTrans.CapitalizeLabels()),
        Transformer.match_rels().do(RelTrans.UppercaseRelationType()),
        Transformer.match_nodes().do(NodeTrans.PopListHubNodes()),
    ]
)
d2g.parse(data)
d2g.create(NEO4J_DRIVER)

Now that looks more like a graph we are used to, isn't it?

There are a lot of more powerful Transformators and you can even make your own 🚀!
Have a deeper look into the docs to learn more how Transformators work.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dict2graph-3.0.5.tar.gz (985.6 kB view details)

Uploaded Source

Built Distribution

dict2graph-3.0.5-py3-none-any.whl (26.1 kB view details)

Uploaded Python 3

File details

Details for the file dict2graph-3.0.5.tar.gz.

File metadata

  • Download URL: dict2graph-3.0.5.tar.gz
  • Upload date:
  • Size: 985.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for dict2graph-3.0.5.tar.gz
Algorithm Hash digest
SHA256 28ac69b49d348ba26c8c48513a6c61132266b3ff7f38c9f251f4ff214084c5a0
MD5 4ca911e1d7ff02426421ff5ae4d7b792
BLAKE2b-256 25910346064ad82ac5766a0f05866e66e148549acb973f4730b4bb25a5cc8665

See more details on using hashes here.

File details

Details for the file dict2graph-3.0.5-py3-none-any.whl.

File metadata

  • Download URL: dict2graph-3.0.5-py3-none-any.whl
  • Upload date:
  • Size: 26.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for dict2graph-3.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 8fac14571db8ad5090fd37d2e5cd7675a3a897dcb1045de2ef7f9de62cd9540e
MD5 f4f6e5c7d236925a821d4a1537cde99b
BLAKE2b-256 338f2d05639b1d9a316008836c88072a5b9d3c0a5a9b0786538225b3248a32c8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page