Skip to content
Explore

Connect Your Application

Python
  • 1. Choose Scenario
  • 2. Get Ready
  • 3. Run CrateDB
  • 4. Import Data
  • 5. Explore Queries
  • 6. More Queries
  • 7. Connect
  • 8. Next Steps

Basic CrateDB connectivity

CrateDB can use any PostgreSQL-compatible Python driver. An example connection string using psycopg2 is:

host=<host> port=5432 dbname=crate user=<user> password=<password> sslmode=<sslmode>

Sample Python application

sample_application

See a working Python sample application on GitHub →

This is a Python/psycopg2 load generator that connects to a CrateDB cluster over the PostgreSQL wire protocol and runs a configurable mix of queries against the weather dataset. It assumes you have already loaded the data in the "Import Data" step.

The single application script is query_crate.py.

The query_crate.py script

Here is what the script does:

  1. Opens a psycopg2 connection to a CrateDB cluster.
  2. Prints the cluster name (SELECT name FROM sys.cluster) as a connectivity smoke test.
  3. Pre-loads reference data from the database (only for query types that will be run):
    • WKT queries: loads every distinct geo_location and timestamp from demo.climate_data.
    • REGION queries: loads every region_name from demo.german_regions.
  4. Runs a workload of queries at a configurable rate, choosing from three query types:
    • WKT — geo-proximity query: finds min/max temperature within 1 metre of a random point at a random timestamp.
    • REGION — three-table join: finds the latest temperature readings for every sensor location inside a named German region, converting Kelvin to Celsius.
    • FTS — full-text search: searches the economics column of demo.german_regions using CrateDB's MATCH predicate and returns the top 3 results by relevance score. 
  5. Records the round-trip latency of each query in an HdrHistogram and prints percentile summaries when the run finishes.