Skip to main content

Manage data using Python and the JSON DB API tutorials

The JSON DB API allows users to access rich database functionality using JSON messages over HTTP(S) without any local binary dependencies.

While the API is simple to use, and writing a driver from scratch is very easy, FairCom provides drivers for the most common programming languages to make usage even easier.

This document goes over the Python driver for the JSON DB API and how to use it.

The Python driver for the JSON DB API makes requests using an HTTP client library as its only direct dependency.

Note

These procedures were written and tested using Windows 11 and Python 3.9.1.

Prerequisites:
  • Ensure you have FairCom DB installed and running.

  • Ensure that Python 3.9.1 or later is installed and executable from your command line.

Establish FairCom DB server communication:
  1. Open the driver folder that is inside the FairCom DB folder.

  2. navigate to and open the folder named python.json.nav in your command prompt.

  3. Run the >pip install -r requirements.txt to install the request library.

  4. Run >cd tutorials.

  5. Run >python ping.py.

  6. Observe the ping successful message.

    This indicates that your FairCom DB server is running, the JSON DB API is accessible, and the Python driver can communicate successfully with the server.

Create some test data:
  1. Run the following commands in this order:

    • python createDatabase.py

    • python createTable.py

    • python createIndex.py

    • python insertRecords.py

    • python getRecordsByTable.py

  2. Observe the results:

    [4, 13358, 'Ian Nepomniachtchi', 2789]

    [1, 13358, 'Magnus Carlsen', 2847]

    [2, 13358, 'Fabiano Caruana', 2820]

    [3, 13358, 'Ding Liren', 2791]

    [5, 13358, 'Levon Aronian', 2781]

    [6, 13358, 'Alexander Grischuk', 2777]

    [7, 13358, 'Anish Giri', 2776]

  3. Confirm the successful creation of the table inside the FairCom Data Explorer.

Example 1. Source code
from JNAClient import JNAClient, JNAClientException

client = JNAClient("http://localhost:8080/api")
client.login("ADMIN", "ADMIN")

try:
    client.createDatabase("test_py")
    print("created database: " + "test_py")
except JNAClientException as e:
    print(e)

client.logout()


  1. In ???, a new JNAClient is instantiated.

  2. We pass in the URL of the server that runs the FairCom DB server. If you want to use HTTPS you need a real SSL certificate.

  3. Attempt to login to the server using username and password ADMIN. This is the default and it should be changed for servers in production.

  4. If the login was successful the high-level API function createDatabase inside of a try/catch/finally block is called.

  5. If the call is successful a success message is printed.

  6. If the call is NOT successful a JsonDBException is thrown and caught and a failure message is printed.

  7. Finally, the logout function is called. This is important because the server caches a lot of resources associated with every session. Always make sure that you log out when you are done with the JSON DB API or that you keep the session alive using ping calls. A ping call once per minute should be enough for long-running tasks.

To look at the messages that are passed back and forth between the client and the server, set the second parameter to true , to view debug information when calling JNAClient.

Example 2. JNAClient
client = JNAClient("http://localhost:8080/api", True)


This procedure outlines the steps to use the Python driver for the JSON DB API in your own projects.

  1. Create a new empty folder called my_test .

  2. Open the new empty folder using a command prompt.

  3. Copy JNAClient.py from the python.json.nav (or python.json.db) folder into the new folder.

  4. Create a new file named my_test.py.

  5. Open my_test.py in a text editor.

  6. Edit the my_test.py to match the following:

    from JNAClient import JNAClient, JNAClientException
    
    client = JNAClient("http://localhost:8080/api", False)
    
    try:
        client.ping()
        print("ping successful.")
    except JNAClientException as e:
        print(e)
  7. Using the command prompt, run > python my_test.py.

  8. Observe the ping successful message.

    > ping successful.

    This indicates that you have successfully created your own project using the Python driver for the JSON DB API.