Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
147 lines (112 sloc) 5.69 KB
pageTitle keywords longTailKeywords Summary
Client Python
grakn, client, python
grakn python client, grakn client python, client python, python client
API Reference of Grakn Client Python.

Dependencies

Client Python Grakn Core Grakn KGMS Python
1.5.3 1.5.2 to 1.5.7 1.5.2 to 1.5.4 >= 2.7
1.5.2 1.5.2, 1.5.3 1.5.2 >= 2.7
1.5.1 1.5.0 N/A >= 2.7
1.4.2 1.3.0 to 1.4.3 1.2.0, 1.4.3 >= 3.6
1.3.0 to 1.3.2 1.3.0 1.4.3 >= 3.6

Installation

pip install grakn-client

If multiple Python versions are available, you may wish to use

pip3 install grakn-client

Quickstart

First make sure, the Grakn server is running.

In the interpreter or in your source, import GraknClient from grakn.client.

from grakn.client import GraknClient

Instantiate a client and open a session.

from grakn.client import GraknClient

with GraknClient(uri="localhost:48555") as client:
    with client.session(keyspace="social_network") as session:
        ## session is open
        pass
    ## session is closed
## client is closed

We can also pass the credentials, as specified when configuring authentication via Grakn Console, into the client constructor as a dictionary.

client = GraknClient(uri="localhost:48555", credentials={"username": "<username>", "password": "<password>"})

Create transactions to use for reading and writing data.

from grakn.client import GraknClient

with GraknClient(uri="localhost:48555") as client:
    with client.session(keyspace="social_network") as session:
        ## creating a write transaction
        with session.transaction().write() as write_transaction:
            ## write transaction is open
            ## write transaction must always be committed (closed)
            write_transaction.commit()

        ## creating a read transaction
        with session.transaction().read() as read_transaction:
            ## read transaction is open
            ## if not using a `with` statement, we must always close the read transaction like so
            # read_transaction.close()
            pass

Running basic retrieval and insertion queries.

from grakn.client import GraknClient

with GraknClient(uri="localhost:48555") as client:
    with client.session(keyspace="social_network") as session:
        ## Insert a Person using a WRITE transaction
        with session.transaction().write() as write_transaction:
            insert_iterator = write_transaction.query('insert $x isa person, has email "x@email.com";')
            concepts = insert_iterator.collect_concepts()
            print("Inserted a person with ID: {0}".format(concepts[0].id))
            ## to persist changes, write transaction must always be committed (closed)
            write_transaction.commit()

        ## Read the person using a READ only transaction
        with session.transaction().read() as read_transaction:
            answer_iterator = read_transaction.query("match $x isa person; get; limit 10;")

            for answer in answer_iterator:
                person = answer.map().get("x")
                print("Retrieved person with id " + person.id)

        ## Or query and consume the iterator immediately collecting all the results
        with session.transaction().read() as read_transaction:
            answer_iterator = read_transaction.query("match $x isa person; get; limit 10;")
            persons = answer_iterator.collect_concepts()
            for person in persons:
                print("Retrieved person with id "+ person.id)

        ## if not using a `with` statement, then we must always close the session and the read transaction
        # read_transaction.close()
        # session.close()
        # client.close()
[Important] Remember that transactions always need to be closed. The safest way is to use the `with ...` syntax which auto-closes at the end of the `with` block. Otherwise, remember to call `transaction.close()` explicitly.

Check out the Concept API to learn about the available methods on the concepts retrieved as the answers to Graql queries.

To view examples of running various Graql queries using the Grakn Client Python, head over to their dedicated documentation pages as listed below.


API Reference

{% include api/generic.html data=site.data.03_client_api.references.grakn language="python" %}

{% include api/generic.html data=site.data.03_client_api.references.client language="python" %}

{% include api/generic.html data=site.data.03_client_api.references.session language="python" %}

{% include api/generic.html data=site.data.03_client_api.references.transaction language="python" %}

{% include api/generic.html data=site.data.03_client_api.references.iterator language="python" %}

{% include api/answers.html data=site.data.03_client_api.references.answer language="python" %}

You can’t perform that action at this time.