Package: gqlalchemy-tailor
GQLAlchemyGQLAlchemyis a fully open-source Python library andObject Graph Mapper(OGM) - a link between graph database objects and Python objects.An Object Graph Mapper or OGM provides a developer-friendly workflow that allows for writing object-oriented notation to communicate with graph databases. Instead of writing Cypher queries, you will be able to write object-oriented code, which the OGM will automatically translate into Cypher queries.InstallationGQLAlchemy is built on top of Memgraph's low-level Python clientpymgclient(PyPI/Documentation/GitHub).To install GQLAlchemy, you first need to installpymgclientbuild prerequisites.After you have installed the prerequisites, run the following command:pip install gqlalchemyWith the above command, you get the basic GQLAlchemy capabilities. To add additional import/export capabilities, install GQLAlchemy with one of the following commands:pip install gqlalchemy[arrow]# Support for the CSV, Parquet, ORC and IPC/Feather/Arrow formatspip install gqlalchemy[dgl]# DGL support (includes PyTorch)pip install gqlalchemy[all]# All of the aboveIf you intend to use GQLAlchemy with PyTorch Geometric support, that library must be installed manually:pipinstallgqlalchemy[torch_pyg]# prerequisitepipinstalltorch-scattertorch-sparsetorch-clustertorch-spline-convtorch-geometric-fhttps://data.pyg.org/whl/torch-1.13.0+cpu.html"If you are usingCondafor Python environment management, you can install GQLAlchemy through pip.Build & TestThe project usesPoetryto build the library. Clone or download theGQLAlchemy source codelocally and run the following command to build it from source with Poetry:poetryinstall--all-extrasThepoetry install --all-extrascommand installs GQLAlchemy with all extras
(optional dependencies). Alternatively, you can use the-Eoption to define
what extras to install:poetryinstall# No extraspoetryinstall-Earrow# Support for the CSV, Parquet, ORC and IPC/Feather/Arrow formatspoetryinstall-Edgl# DGL support (also includes torch)To run the tests, make sure you have anactive Memgraph instance, and execute one of the following commands:poetryrunpytest.-k"not slow"# If all extras installedpoetryrunpytest.-k"not slow and not extras"# OtherwiseIf you’ve installed only certain extras, it’s also possible to run their associated tests:poetryrunpytest.-k"arrow"poetryrunpytest.-k"dgl"GQLAlchemy capabilities🗺️ Object graph mapperBelow you can see an example of how to createUserandLanguagenode classes, and a relationship class of typeSPEAKS. Along with that, you can see how to create a new node and relationship and how to save them in the database. After that, you can load those nodes and relationship from the database.fromgqlalchemyimportMemgraph,Node,Relationship,FieldfromtypingimportOptionaldb=Memgraph()classUser(Node,index=True,db=db):id:str=Field(index=True,exist=True,unique=True,db=db)classLanguage(Node):name:str=Field(unique=True,db=db)classSpeaks(Relationship,type="SPEAKS"):passuser=User(id="3",username="John").save(db)language=Language(name="en").save(db)speaks_rel=Speaks(_start_node_id=user._id,_end_node_id=language._id).save(db)loaded_user=User(id="3").load(db=db)print(loaded_user)loaded_speaks=Speaks(_start_node_id=user._id,_end_node_id=language._id).load(db)print(loaded_speaks)🔨 Query builderWhen building a Cypher query, you can use a set of methods that are wrappers around Cypher clauses.fromgqlalchemyimportcreate,matchfromgqlalchemy.query_builderimportOperatorquery_create=create().node(labels="Person",name="Leslie").to(relationship_type="FRIENDS_WITH").node(labels="Person",name="Ron").execute()query_match=match().node(labels="Person",variable="p1").to().node(labels="Person",variable="p2").where(item="p1.name",operator=Operator.EQUAL,literal="Leslie").return_(results=["p1",("p2","second")]).execute()🚰 Manage streamsYou can create and start Kafka or Pulsar stream using GQLAlchemy.Kafka streamfromgqlalchemyimportMemgraphKafkaStreamstream=MemgraphKafkaStream(name="ratings_stream",topics=["ratings"],transform="movielens.rating",bootstrap_servers="localhost:9093")db.create_stream(stream)db.start_stream(stream)Pulsar streamfromgqlalchemyimportMemgraphPulsarStreamstream=MemgraphPulsarStream(name="ratings_stream",topics=["ratings"],transform="movielens.rating",service_url="localhost:6650")db.create_stream(stream)db.start_stream(stream)🗄️ Import table data from different sourcesImport table data to a graph databaseYou can translate table data from a file to graph data and import it to Memgraph. Currently, we support reading of CSV, Parquet, ORC and IPC/Feather/Arrow file formats via the PyArrow package.Read all about it intable to graph importer how-to guide.Make a custom file system importerIf you want to read from a file system not currently supported by GQLAlchemy, or use a file type currently not readable, you can implement your own by extending abstract classesFileSystemHandlerandDataLoader, respectively.Read all about it incustom file system importer how-to guide.⚙️ Manage Memgraph instancesYou can start, stop, connect to and monitor Memgraph instances with GQLAlchemy.Manage Memgraph Docker instancefromgqlalchemy.instance_runnerimport(DockerImage,MemgraphInstanceDocker)memgraph_instance=MemgraphInstanceDocker(docker_image=DockerImage.MEMGRAPH,docker_image_tag="latest",host="0.0.0.0",port=7687)memgraph=memgraph_instance.start_and_connect(restart=False)memgraph.execute_and_fetch("RETURN 'Memgraph is running' AS result"))[0]["result"]Manage Memgraph binary instancefromgqlalchemy.instance_runnerimportMemgraphInstanceBinarymemgraph_instance=MemgraphInstanceBinary(host="0.0.0.0",port=7698,binary_path="/usr/lib/memgraph/memgraph",user="memgraph")memgraph=memgraph_instance.start_and_connect(restart=False)memgraph.execute_and_fetch("RETURN 'Memgraph is running' AS result"))[0]["result"]🔫 Manage database triggersBecause Memgraph supports database triggers onCREATE,UPDATEandDELETEoperations, GQLAlchemy also implements a simple interface for maintaining these triggers.fromgqlalchemyimportMemgraph,MemgraphTriggerfromgqlalchemy.modelsimport(TriggerEventType,TriggerEventObject,TriggerExecutionPhase,)db=Memgraph()trigger=MemgraphTrigger(name="ratings_trigger",event_type=TriggerEventType.CREATE,event_object=TriggerEventObject.NODE,execution_phase=TriggerExecutionPhase.AFTER,statement="UNWIND createdVertices AS node SET node.created_at = LocalDateTime()",)db.create_trigger(trigger)triggers=db.get_triggers()print(triggers)💽 On-disk storageSince Memgraph is an in-memory graph database, the GQLAlchemy library provides an on-disk storage solution for large properties not used in graph algorithms. This is useful when nodes or relationships have metadata that doesn’t need to be used in any of the graph algorithms that need to be carried out in Memgraph, but can be fetched after. Learn all about it in theon-disk storage how-to guide.If you want to learn more about OGM, query builder, managing streams, importing data from different source, managing Memgraph instances, managing database triggers and using on-disk storage, check out the GQLAlchemyhow-to guides.Development (how to build)poetryrunflake8.
poetryrunblack.
poetryrunpytest.-k"not slow and not extras"DocumentationThe GQLAlchemy documentation is available onmemgraph.com/docs/gqlalchemy.The documentation can be generated by executing:pip3 install pydoc-markdown
pydoc-markdownLicenseCopyright (c) 2016-2022Memgraph Ltd.Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.