-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathasync_example_arrow.py
More file actions
203 lines (164 loc) · 7.58 KB
/
async_example_arrow.py
File metadata and controls
203 lines (164 loc) · 7.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"""
Asynchronous Ingestion Example - Arrow Flight Mode
This example demonstrates record ingestion using the asynchronous API with Arrow Flight.
Record Type Mode: Arrow (RecordBatch)
- Records are sent as pyarrow RecordBatches
- Uses Arrow Flight protocol for columnar data transfer
- Best for structured/columnar data, DataFrames, Parquet workflows
Requirements:
pip install databricks-zerobus-ingest-sdk[arrow]
Note: Arrow Flight support is experimental and not yet supported for production use.
"""
import asyncio
import logging
import os
import time
import pyarrow as pa
from zerobus.sdk.aio import ZerobusSdk
from zerobus.sdk.shared.arrow import ArrowStreamConfigurationOptions
# Configure logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)
# Configuration - update these with your values
SERVER_ENDPOINT = os.getenv(
"ZEROBUS_SERVER_ENDPOINT",
"https://your-shard-id.zerobus.region.cloud.databricks.com",
)
UNITY_CATALOG_ENDPOINT = os.getenv("DATABRICKS_WORKSPACE_URL", "https://your-workspace.cloud.databricks.com")
TABLE_NAME = os.getenv("ZEROBUS_TABLE_NAME", "catalog.schema.table")
# For OAuth authentication
CLIENT_ID = os.getenv("DATABRICKS_CLIENT_ID", "your-oauth-client-id")
CLIENT_SECRET = os.getenv("DATABRICKS_CLIENT_SECRET", "your-oauth-client-secret")
# Number of batches to ingest
NUM_BATCHES = 10
ROWS_PER_BATCH = 100
# Define the Arrow schema
SCHEMA = pa.schema(
[
("device_name", pa.large_utf8()),
("temp", pa.int32()),
("humidity", pa.int64()),
]
)
def create_sample_batch(batch_index):
"""
Creates a sample RecordBatch with air quality data.
Returns a pyarrow.RecordBatch with ROWS_PER_BATCH rows.
"""
return pa.record_batch(
{
"device_name": [f"sensor-{(batch_index * ROWS_PER_BATCH + i) % 10}" for i in range(ROWS_PER_BATCH)],
"temp": [20 + ((batch_index * ROWS_PER_BATCH + i) % 15) for i in range(ROWS_PER_BATCH)],
"humidity": [50 + ((batch_index * ROWS_PER_BATCH + i) % 40) for i in range(ROWS_PER_BATCH)],
},
schema=SCHEMA,
)
async def main():
print("Starting asynchronous ingestion example (Arrow Flight Mode)...")
print("=" * 60)
# Check if credentials are configured
if CLIENT_ID == "your-oauth-client-id" or CLIENT_SECRET == "your-oauth-client-secret":
logger.error("Please set DATABRICKS_CLIENT_ID and DATABRICKS_CLIENT_SECRET environment variables")
return
if SERVER_ENDPOINT == "https://your-shard-id.zerobus.region.cloud.databricks.com":
logger.error("Please set ZEROBUS_SERVER_ENDPOINT environment variable")
return
if TABLE_NAME == "catalog.schema.table":
logger.error("Please set ZEROBUS_TABLE_NAME environment variable")
return
try:
# Step 1: Initialize the SDK
sdk = ZerobusSdk(SERVER_ENDPOINT, UNITY_CATALOG_ENDPOINT)
logger.info("SDK initialized")
# Step 2: Configure arrow stream options (all optional, shown with defaults)
options = ArrowStreamConfigurationOptions(
max_inflight_batches=10,
recovery=True,
recovery_timeout_ms=15000,
recovery_backoff_ms=2000,
recovery_retries=3,
)
logger.info("Arrow stream configuration created")
# Step 3: Create an Arrow Flight stream
#
# Pass a pyarrow.Schema - the SDK handles serialization internally.
# The SDK automatically:
# - Includes authorization header with OAuth token
# - Includes x-databricks-zerobus-table-name header
stream = await sdk.create_arrow_stream(TABLE_NAME, SCHEMA, CLIENT_ID, CLIENT_SECRET, options)
logger.info(f"Arrow stream created for table: {stream.table_name}")
# Step 4: Ingest Arrow RecordBatches asynchronously
logger.info(f"\nIngesting {NUM_BATCHES} batches of {ROWS_PER_BATCH} rows each...")
start_time = time.time()
total_rows = 0
try:
# ========================================================================
# Ingest RecordBatches - each call returns an offset
# ========================================================================
offsets = []
for i in range(NUM_BATCHES):
batch = create_sample_batch(i)
offset = await stream.ingest_batch(batch)
offsets.append(offset)
total_rows += batch.num_rows
logger.info(f" Batch {i + 1}: {batch.num_rows} rows, offset: {offset}")
# ========================================================================
# You can also ingest a pyarrow.Table directly
# The SDK converts it to a single RecordBatch internally
# ========================================================================
table = pa.table(
{
"device_name": [f"sensor-table-{i}" for i in range(50)],
"temp": list(range(20, 70)),
"humidity": list(range(50, 100)),
},
schema=SCHEMA,
)
offset = await stream.ingest_batch(table)
offsets.append(offset)
total_rows += table.num_rows
logger.info(f" Table ingested: {table.num_rows} rows, offset: {offset}")
submit_end_time = time.time()
submit_duration = submit_end_time - start_time
logger.info(f"\nAll batches submitted in {submit_duration:.2f} seconds")
# ========================================================================
# Wait for the last offset to be acknowledged
# ========================================================================
logger.info(f"Waiting for offset {offsets[-1]} to be acknowledged...")
await stream.wait_for_offset(offsets[-1])
logger.info(f" Offset {offsets[-1]} acknowledged")
# Step 5: Flush and close the stream
logger.info("\nFlushing stream...")
await stream.flush()
logger.info("Stream flushed")
end_time = time.time()
total_duration = end_time - start_time
rows_per_second = total_rows / total_duration
await stream.close()
logger.info("Stream closed")
# Print summary
print("\n" + "=" * 60)
print("Ingestion Summary:")
print(f" Total batches: {NUM_BATCHES + 1}")
print(f" Total rows: {total_rows}")
print(f" Submit time: {submit_duration:.2f} seconds")
print(f" Total time: {total_duration:.2f} seconds")
print(f" Throughput: {rows_per_second:.2f} rows/sec")
print(f" Record type: Arrow Flight (RecordBatch)")
print("=" * 60)
except Exception as e:
logger.error(f"\nError during ingestion: {e}")
# On failure, you can retrieve unacked batches for retry
if stream.is_closed:
unacked = await stream.get_unacked_batches()
if unacked:
logger.info(f" {len(unacked)} unacked batches available for retry")
for i, batch in enumerate(unacked):
logger.info(f" Batch {i}: {batch.num_rows} rows, schema: {batch.schema}")
await stream.close()
raise
except Exception as e:
logger.error(f"\nFailed to initialize stream: {e}")
raise
if __name__ == "__main__":
asyncio.run(main())