|
| 1 | +# Databend UDF Client Library |
| 2 | + |
| 3 | +Simple Python client library for testing Databend UDF servers. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +pip install databend-udf |
| 9 | +``` |
| 10 | + |
| 11 | +## Quick Start |
| 12 | + |
| 13 | +```python |
| 14 | +from databend_udf import UDFClient, create_client |
| 15 | + |
| 16 | +# Create client (default: localhost:8815) |
| 17 | +client = create_client() |
| 18 | + |
| 19 | +# Or specify host/port |
| 20 | +client = UDFClient(host="localhost", port=8815) |
| 21 | + |
| 22 | +# Health check |
| 23 | +if client.health_check(): |
| 24 | + print("Server is running!") |
| 25 | + |
| 26 | +# Echo test |
| 27 | +result = client.echo("Hello, Databend!") |
| 28 | +print(result) # "Hello, Databend!" |
| 29 | + |
| 30 | +# Call UDF function |
| 31 | +result = client.call_function("gcd", 48, 18) |
| 32 | +print(result[0]) # 6 |
| 33 | +``` |
| 34 | + |
| 35 | +## API Reference |
| 36 | + |
| 37 | +### UDFClient |
| 38 | + |
| 39 | +#### Methods |
| 40 | + |
| 41 | +- `__init__(host="localhost", port=8815)` - Create client |
| 42 | +- `health_check() -> bool` - Check server health |
| 43 | +- `echo(message: str) -> str` - Echo test message |
| 44 | +- `call_function(name, *args) -> List[Any]` - Call UDF with arguments |
| 45 | +- `call_function_batch(name, **kwargs) -> List[Any]` - Call UDF with batch data |
| 46 | +- `get_function_info(name) -> FlightInfo` - Get function schema |
| 47 | +- `list_functions() -> List[str]` - List available functions |
| 48 | + |
| 49 | +### Examples |
| 50 | + |
| 51 | +#### Single Value Calls |
| 52 | + |
| 53 | +```python |
| 54 | +client = create_client() |
| 55 | + |
| 56 | +# Numeric functions |
| 57 | +result = client.call_function("add_signed", 1, 2, 3, 4) |
| 58 | +print(result[0]) # 10 |
| 59 | + |
| 60 | +# String functions |
| 61 | +result = client.call_function("split_and_join", "a,b,c", ",", "-") |
| 62 | +print(result[0]) # "a-b-c" |
| 63 | + |
| 64 | +# Array functions |
| 65 | +result = client.call_function("array_access", ["hello", "world"], 1) |
| 66 | +print(result[0]) # "hello" |
| 67 | +``` |
| 68 | + |
| 69 | +#### Batch Calls |
| 70 | + |
| 71 | +```python |
| 72 | +client = create_client() |
| 73 | + |
| 74 | +# Process multiple values at once |
| 75 | +x_values = [48, 56, 72] |
| 76 | +y_values = [18, 21, 24] |
| 77 | +results = client.call_function_batch("gcd_batch", a=x_values, b=y_values) |
| 78 | +print(results) # [6, 7, 24] |
| 79 | +``` |
| 80 | + |
| 81 | +## Testing |
| 82 | + |
| 83 | +Run the example server: |
| 84 | + |
| 85 | +```bash |
| 86 | +cd python/example |
| 87 | +python server.py |
| 88 | +``` |
| 89 | + |
| 90 | +In another terminal, run tests: |
| 91 | + |
| 92 | +```bash |
| 93 | +# Simple test |
| 94 | +python simple_test.py |
| 95 | + |
| 96 | +# Comprehensive test suite |
| 97 | +python client_test.py |
| 98 | +``` |
| 99 | + |
| 100 | +## Error Handling |
| 101 | + |
| 102 | +```python |
| 103 | +try: |
| 104 | + result = client.call_function("my_function", arg1, arg2) |
| 105 | + print(result[0]) |
| 106 | +except Exception as e: |
| 107 | + print(f"Function call failed: {e}") |
| 108 | +``` |
| 109 | + |
| 110 | +## Performance Testing |
| 111 | + |
| 112 | +The client supports testing concurrent I/O operations: |
| 113 | + |
| 114 | +```python |
| 115 | +# Sequential calls (slow) |
| 116 | +for i in range(10): |
| 117 | + client.call_function("slow_function", i) |
| 118 | + |
| 119 | +# Batch call (fast with io_threads) |
| 120 | +client.call_function_batch("slow_function", a=list(range(10))) |
| 121 | +``` |
0 commit comments