diff --git a/http/post_simple/python/client/README.md b/http/post_simple/python/client/README.md new file mode 100644 index 0000000..6c4ca61 --- /dev/null +++ b/http/post_simple/python/client/README.md @@ -0,0 +1,32 @@ + + +# HTTP Post Arrow Data: Simple Python Client Example using Requests + +This directory contains a minimal example of an HTTP client implemented in Python using the `requests` library. The client: +1. Sends an HTTP POST request to a server. +2. Receives an HTTP 200 response from the server, with the response body containing an Arrow IPC stream of record batches. +3. Adds the record batches to a list as they are received. + +To run this example, start one of the server examples in the parent directory, then: + +```sh +pip install pyarrow requests +python client.py +``` diff --git a/http/post_simple/python/client/client.py b/http/post_simple/python/client/client.py new file mode 100644 index 0000000..9332ba2 --- /dev/null +++ b/http/post_simple/python/client/client.py @@ -0,0 +1,47 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +import requests +import pyarrow as pa +import time + +start_time = time.time() + +params = {'n_records': 100000} +url = 'http://localhost:8008' +response = requests.post(url, data={}) + +if response.status_code >= 400: + print (response.json()) +elif response.status_code == 200: + buffer = response.content + batches = [] + + with pa.ipc.open_stream(buffer) as reader: + schema = reader.schema + try: + while True: + batches.append(reader.read_next_batch()) + except StopIteration: + pass + + end_time = time.time() + execution_time = end_time - start_time + + print(f"{len(buffer)} bytes received") + print(f"{len(batches)} record batches received") + print(f"{execution_time} seconds elapsed") diff --git a/http/post_simple/python/server/README.md b/http/post_simple/python/server/README.md new file mode 100644 index 0000000..ad9eff7 --- /dev/null +++ b/http/post_simple/python/server/README.md @@ -0,0 +1,38 @@ + + +# HTTP GET Arrow Data: Simple Python Server Example + +This directory contains a minimal example of an HTTP server implemented in Python. The server: +1. Creates a list of record batches and populates it with synthesized data. +2. Listens for HTTP POST requests from clients. +3. Upon receiving a request, sends an HTTP 200 response with the body containing an Arrow IPC stream of record batches. + +To run this example: + +```sh +pip install fastapi +pip install "uvicorn[standard]" +pip install pyarrow +# start the server using uvicorn +uvicorn server:app --port 8008 +``` + +> [!NOTE] +> This example uses Python's built-in [`http.server`](https://docs.python.org/3/library/http.server.html) module. This server does not implement chunked transfer encoding automatically like more sophisticated HTTP servers do, so this example implements it manually, with each chunk consisting of one Arrow record batch. Note that in servers that implement chunked transfer encoding automatically, each chunk will generally not correspond to one Arrow record batch. diff --git a/http/post_simple/python/server/server.py b/http/post_simple/python/server/server.py new file mode 100644 index 0000000..fc27201 --- /dev/null +++ b/http/post_simple/python/server/server.py @@ -0,0 +1,89 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +import pyarrow as pa +import urllib.parse +from random import randbytes +import io + +from fastapi import Request, FastAPI +from fastapi.responses import StreamingResponse, JSONResponse + +app = FastAPI() + +def get_post_data_and_schema(total_records: int): + schema = pa.schema([('a', pa.int64()), ('b', pa.int64()), + ('c', pa.int64()), ('d', pa.int64())]) + length = 4096 + ncolumns = 4 + + arrays = [] + + for x in range(0, ncolumns): + buffer = pa.py_buffer(randbytes(length * 8)) + arrays.append( + pa.Int64Array.from_buffers(pa.int64(), + length, [None, buffer], + null_count=0)) + + batch = pa.record_batch(arrays, schema) + batches = [] + + records = 0 + while records < total_records: + if records + length > total_records: + last_length = total_records - records + batches.append(batch.slice(0, last_length)) + records += last_length + else: + batches.append(batch) + records += length + + return batches, schema + + +def make_reader(schema, batches): + return pa.RecordBatchReader.from_batches(schema, batches) + + +def generate_batches(schema, reader): + with io.BytesIO() as sink, pa.ipc.new_stream(sink, schema) as writer: + for batch in reader: + sink.seek(0) + sink.truncate(0) + writer.write_batch(batch) + yield sink.getvalue() + + sink.seek(0) + sink.truncate(0) + writer.close() + yield sink.getvalue() + +@app.post("/") +async def main(request: Request): + request_body = await request.body() + params = urllib.parse.parse_qs(request_body.decode('ascii')) + if 'n_records' not in params.keys(): + return JSONResponse(status_code=400, content='request failed. n_records value not found in request data.') + + n_records = int(params['n_records'][0]) + + batches, schema = get_post_data_and_schema(n_records) + def iterbuffer(): + for buffer in generate_batches(schema, make_reader(schema, batches)): + yield buffer + return StreamingResponse(iterbuffer())