Skip to main content

Python SDK for Linkis Service

Project description

Linkis Python SDK

Linkis Python SDK是与Apache Linkis服务交互的Python客户端库。该SDK提供了简便的接口,用于执行代码并获取结果,支持同步和异步操作。

Linkis Python SDK is the Python client library for interacting with Apache Linkis services. This SDK provides convenient interfaces for executing code and retrieving results, supporting both synchronous and asynchronous operations.

PyPI version

Python Version

安装

Installation

pip install linkis-python-sdk

功能特点

Features

  • 支持同步和异步客户端

  • 支持结果缓存

  • 提供数据过滤、排序和分页功能

  • 轻松将结果转换为Pandas DataFrame

  • Support for synchronous and asynchronous clients

  • Result caching capabilities

  • Data filtering, sorting, and pagination

  • Easy conversion of results to Pandas DataFrame

使用示例

Usage Examples

同步客户端

Synchronous Client

from linkis_python_sdk.client import LinkisClient
from linkis_python_sdk.config.config import LinkisConfig

# 创建配置
# Create configuration
config = LinkisConfig(
    base_url="http://your-linkis-server/api/rest_j/v1",
    username="your_username",
    password="your_password"
)

# 创建客户端
# Create client
client = LinkisClient(config)

# 执行代码
# Execute code
code = "SELECT * FROM my_table LIMIT 10"
query_req = {
    "columns": [{"column": "id"}, {"column": "name"}],
    "filters": [{"column": "age", "operator": "gt", "value": 18}],
    "orderbys": [{"column": "id", "ascending": True}],
    "page": 1,
    "page_size": 10
}
data, total, error = client.execute_code(
    code=code,
    query_req=query_req,
    engine_type="spark",
    run_type="sql"
)

print(f"Total records: {total}")
print(f"Data: {data}")

异步客户端

Asynchronous Client

import asyncio
from linkis_python_sdk.asyncio.client import LinkisClient
from linkis_python_sdk.config.config import LinkisConfig

async def main():
    # 创建配置
    # Create configuration
    config = LinkisConfig(
        base_url="http://your-linkis-server/api/rest_j/v1",
        username="your_username",
        password="your_password"
    )
    
    # 创建异步客户端
    # Create asynchronous client
    client = LinkisClient(config)
    
    # 执行代码
    # Execute code
    code = "SELECT * FROM my_table LIMIT 10"
    query_req = {
        "columns": [{"column": "id"}, {"column": "name"}],
        "filters": [{"column": "age", "operator": "gt", "value": 18}],
        "orderbys": [{"column": "id", "ascending": True}],
        "page": 1,
        "page_size": 10
    }
    data, total, error = await client.execute_code(
        code=code,
        query_req=query_req,
        engine_type="spark",
        run_type="sql"
    )
    
    print(f"Total records: {total}")
    print(f"Data: {data}")

if __name__ == "__main__":
    asyncio.run(main())

带缓存的执行

Execution with Caching

SDK还支持使用Redis缓存执行结果,以提高性能:

The SDK also supports caching execution results with Redis to improve performance:

from redis import ConnectionPool
from linkis_python_sdk.client import LinkisClient
from linkis_python_sdk.config.config import LinkisConfig

# 创建Redis连接池
# Create Redis connection pool
redis_pool = ConnectionPool(host="localhost", port=6379, db=0)

# 创建配置
# Create configuration
config = LinkisConfig(
    base_url="http://your-linkis-server/api/rest_j/v1",
    username="your_username",
    password="your_password"
)

# 创建带缓存的客户端
# Create client with caching
client = LinkisClient(
    linkis_conf=config,
    r_pool=redis_pool,
    cache_expire=1800,  # 缓存过期时间(秒)/ Cache expiration time (seconds)
    cache_code_prefix="linkis:codecache"  # 缓存键前缀 / Cache key prefix
)

# 执行带缓存的代码
# Execute code with caching
data, total, error = client.execute_code_with_cache(
    code="SELECT * FROM my_table LIMIT 10",
    query_req={"columns": [{"column": "id"}, {"column": "name"}]},
    engine_type="spark",
    run_type="sql"
)

依赖项

Dependencies

  • Python >= 3.9
  • requests
  • pandas
  • redis
  • aiohttp

开发贡献

Development and Contribution

欢迎为Linkis Python SDK做出贡献!请参阅PUBLISH.md了解如何构建和发布该包。 Contributions to the Linkis Python SDK are welcome! Please refer to PUBLISH.md for information on how to build and publish the package.

许可证

License

该项目采用Apache License 2.0许可证。

This project is licensed under the Apache License 2.0.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

linkis_python_sdk-0.2.0.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

linkis_python_sdk-0.2.0-py2.py3-none-any.whl (14.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file linkis_python_sdk-0.2.0.tar.gz.

File metadata

  • Download URL: linkis_python_sdk-0.2.0.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for linkis_python_sdk-0.2.0.tar.gz
Algorithm Hash digest
SHA256 722f8104b745b09b31d2db1d8bce3030b33303a739d9a05711af116cf9abddc6
MD5 3a4b2f340132793685c486fce3293ad6
BLAKE2b-256 874e870d0da6b34dc14b1dbd3f00f7932f33d3fff5b5b9e5db5b31487cf66434

See more details on using hashes here.

File details

Details for the file linkis_python_sdk-0.2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for linkis_python_sdk-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f9ee5d679634c082e03265b7af13395de8cf2ce7d190da480b81e955f3fa52e5
MD5 119335d50ecb6f75095897cf77e1430d
BLAKE2b-256 ba581dc6f524bff4ce502598e1f394a50a7a04a91da39949b78c8dc50693bc26

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page