UltipaDocs
Try Playground
  • Introduction
  • RESTful API
    • Installation
    • Connection
      • Request Configuration
      • Querying Methods
      • UQL Execution
      • Graphset Management
      • Schema and Property Management
      • Data Insertion and Deletion
      • Query Acceleration
      • Algorithm Management
      • Downloads and Exports
      • Process and Task Management
      • Access Management
      • Others
    • Result Processing
    • Types Mapping
    • Installation
    • Connection
      • Request Configuration
      • Querying Methods
      • UQL Execution
      • Graphset Management
      • Schema and Property Management
      • Data Insertion and Deletion
      • Query Acceleration
      • Algorithm Management
      • Downloads and Exports
      • Process and Task Management
      • Access Management
      • Others
    • Result Processing
    • Types Mapping
    • Installation
    • Connection
      • Request Configuration
      • Querying Methods
      • UQL Execution
      • Graphset Management
      • Schema and Property Management
      • Data Insertion and Deletion
      • Query Acceleration
      • Algorithm Management
      • Downloads and Exports
      • Process and Task Management
      • Access Management
      • Others
    • Result Processing
    • Types Mapping
    • Installation
    • Connection
      • Request Configuration
      • Querying Methods
      • UQL Execution
      • Graphset Management
      • Schema and Property Management
      • Data Insertion and Deletion
      • Query Acceleration
      • Algorithm Management
      • Downloads and Exports
      • Process and Task Management
      • Access Management
      • Others
    • Result Processing
    • Types Mapping
    • Installation
    • Connection
      • Request Configuration
      • Querying Methods
      • UQL Execution
      • Graphset Management
      • Schema and Property Management
      • Data Insertion and Deletion
      • Query Acceleration
      • Algorithm Management
      • Downloads and Exports
      • Process and Task Management
      • Access Management
      • Others
    • Result Processing
    • Types Mapping
  1. Docs
  2. /
  3. Ultipa Drivers
  4. /
  5. Python

Connection

After installing the Ultipa Python SDK and setting up a running Ultipa instance, you should be able to connect your application to the Ultipa graph database.

Connection to Ultipa can be established by creating a driver with configurations specified using the following methods:

  • Code Configuration Connection: through the UltipaConfig class
  • File Configuration Connection: through the .env file and the UltipaConfig class

The values of configuration items are preferentially determined by UltipaConfig, followed by .env. If an item is not found in either configuration, the default value is used.

Code Configuration Connection

Python
from ultipa import Connection, UltipaConfig

ultipaConfig = UltipaConfig()
# URI example: ultipaConfig.hosts = ["mqj4zouys.us-east-1.cloud.ultipa.com:60010"]
ultipaConfig.hosts = ["192.168.1.85:60061", "192.168.1.87:60061", "192.168.1.88:60061"]
ultipaConfig.username = "<username>"
ultipaConfig.password = "<password>"

Conn = Connection.NewConnection(defaultConfig=ultipaConfig)

response = Conn.test()
# The connection is successfully established if the code is 0
print("Code = ", response.status.code)
Output
Code =  0

A driver is created with the configurations specified using UltipaConfig. Please refer to Configuration Items for all items available for configuring connection details with UltipaConfig.

The Connection.NewConnection() method obtains a connection to Ultipa. The graphset identified by the configuration item defaultGraph will be used.

File Configuration Connection

Python
import os
from dotenv import load_dotenv, dotenv_values
from pathlib import Path
from ultipa import Connection, UltipaConfig
from ultipa.utils.logger import LoggerConfig

# Loads the .env file into the environment, and overrides system environment variables
env_path = Path('./.env')
env_dict = dotenv_values(dotenv_path=env_path)
load_dotenv(encoding='utf-8', override=True)

# Fetches environment variables
env_config = {
    "hosts": os.getenv("hosts"),
    "username": os.getenv("username"),
    "password": os.getenv("password"),
    "defaultGraph": os.getenv("defaultGraph")
}

def getConn():
    hosts = env_config.get("hosts", "").split(",")
    username = env_config.get("username", "")
    password = env_config.get("password", "")
    defaultGraph = env_config.get("defaultGraph", "")

    uqlLoggerConfig = LoggerConfig(name="testLog", fileName="../intergration_tests/Logs/test.log", isWriteToFile=True, isStream=True)
    defaultConfig = UltipaConfig(hosts=hosts, username=username, password=password, heartBeat=10, uqlLoggerConfig=uqlLoggerConfig)
    Conn = Connection.NewConnection(defaultConfig)
    return Conn

response = getConn().test()
print("Code = ", response.status.code)
Output
2024-08-19 10:21:00,347 - INFO: Test Welcome To Ultipa!
2024-08-19 10:21:00,357 - INFO: Test Welcome To Ultipa!
2024-08-19 10:21:00,370 - INFO: Test Welcome To Ultipa!
2024-08-19 10:21:00,374 - INFO: Test Welcome To Ultipa!
Code =  1000

A driver is created with the configurations specified using the .env file. The .env file should be placed under root path of the project.

Example of the .env file:

.env
#hosts=mqj4zouys.us-east-1.cloud.ultipa.com:60010
hosts=192.168.1.85:60061,192.168.1.86:60061,192.168.1.87:60061
username=<username>
password=<password>
passwordEncrypt=PasswordEncrypt.MD5
timeoutWithSeconds=300
consistency=true
#crtFilePath=F:\\ultipa.crt
#maxRecvSize=10240
defaultGraph=miniCircle
#timeZone=Asia/Tokyo
#timeZoneOffset=+0700
#responseWithRequestInfo=false
#debug=false

Please refer to Configuration Items for all items available for configuring connection details with the .env file.

Configuration Items

Below are all the configuration items available for UltipaConfig and .env file:

Items
Type
Default
Description
hostsList[str]Database host addresses or URI (excluding https:// or http://). For clusters, multiple addresses are separated by commas. Required.
usernamestrUsername of the host authentication. Required.
passwordstrPassword of the host authentication. Required.
passwordEncryptPasswordEncryptPasswordEncrypt.MD5Password encryption method of the driver. Supports MD5, LDAP and NOTHING.
timeoutWithSecondsint3600Request timeout threshold in seconds.
consistencyboolFalseWhether to use the leader node to ensure consistency read.
crtFilePathstrThe file path of SSL certificate when both Ultipa server and client-end are in SSL mode.
maxRecvSizeint-1Maximum size in bytes when receiving data.
defaultGraphstrdefaultName of the graph in the database to use by default.
heartBeatint10Heartbeat interval in seconds for all instances, set 0 to disable heartbeat.
heartBeatint10Heartbeat interval in seconds for all instances, set 0 to disable heartbeat.
timeZonestrTimezone, e.g., Europe/Paris.
timeZoneOffsetint/strSpecifies how far the target timezone is from UTC, either in seconds (if an integer) or a 5-character string such as +0700 and -0430.
responseWithRequestInfoboolFalseWhether to return request.
debugboolFalseWhether to use debug mode.
uqlLoggerConfigLoggerConfigConfigures logging for the UQL operations, including name, filename, isWriteToFile, level and isStream.