Yandex Query HTTP Client

0.1.4 · active · verified Fri Apr 10

The `yandex-query-client` is the official Python HTTP client for Yandex Query, an interactive, fully managed service for running analytical and streaming queries on structured and semi-structured data using a common SQL dialect (YQL). The current version is 0.1.4, indicating it is an early-stage but actively maintained library. Its release cadence is currently irregular, consistent with a newer project.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Yandex Query client, create a simple SQL query, wait for its completion, and retrieve the results. It highlights the essential steps for basic interaction with the Yandex Query service. Ensure `YQ_IAM_TOKEN` and `YQ_FOLDER_ID` environment variables are set or replaced with actual values for authentication and operation within a specific Yandex Cloud folder.

import os
from yandex_query_client import YQHttpClient, YQHttpClientConfig

# --- Configuration ---
# Replace with your Yandex Cloud IAM token and Folder ID.
# It is recommended to use environment variables for sensitive credentials.
IAM_TOKEN = os.environ.get('YQ_IAM_TOKEN', 'YOUR_IAM_TOKEN')
PROJECT = os.environ.get('YQ_FOLDER_ID', 'YOUR_FOLDER_ID')

if IAM_TOKEN == 'YOUR_IAM_TOKEN' or PROJECT == 'YOUR_FOLDER_ID':
    print("WARNING: Please set YQ_IAM_TOKEN and YQ_FOLDER_ID environment variables or replace placeholders.")
    # In a real application, you might raise an error or exit.
    exit("Missing Yandex Query credentials.")

config = YQHttpClientConfig(IAM_TOKEN, PROJECT)
client = YQHttpClient(config)

# --- Example: Run a simple query ---
query_text = "SELECT 777;"
query_name = "my_sample_query"

try:
    print(f"Creating query: '{query_text}' with name '{query_name}'...")
    query_id = client.create_query(query_text=query_text, name=query_name)
    print(f"Query created with ID: {query_id}")

    print(f"Waiting for query {query_id} to succeed...")
    # wait_query_to_succeed returns the number of result sets
    result_set_count = client.wait_query_to_succeed(query_id)
    print(f"Query {query_id} succeeded with {result_set_count} result set(s).")

    print(f"Fetching results for query {query_id}...")
    # get_query_all_result_sets fetches all result sets
    results = client.get_query_all_result_sets(query_id, result_set_count=result_set_count)

    for i, result_set in enumerate(results):
        print(f"\n--- Result Set {i+1} ---")
        # Access column names (if available)
        column_names = [col.name for col in result_set.columns] if result_set.columns else []
        print(f"Columns: {column_names}")

        # Iterate through rows
        for row in result_set.rows:
            # Access values (example: assuming single column 'column0')
            # The exact structure of 'row' might vary, often it's a list/tuple of values
            print(f"Row: {row}")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →