|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +import requests |
| 19 | +import json |
| 20 | +import os |
| 21 | +import pyarrow as pa |
| 22 | + |
| 23 | + |
| 24 | +HOST = "http://localhost:8008/" |
| 25 | + |
| 26 | +JSON_FORMAT = "application/json" |
| 27 | +ARROW_STREAM_FORMAT = "application/vnd.apache.arrow.stream" |
| 28 | + |
| 29 | +json_response = requests.get(HOST) |
| 30 | + |
| 31 | +response_status = json_response.status_code |
| 32 | +if not response_status == 200: |
| 33 | + raise ValueError(f"Expected response status 200, got {response_status}") |
| 34 | + |
| 35 | +content_type = json_response.headers.get("Content-Type", "") |
| 36 | +if not content_type.startswith(JSON_FORMAT): |
| 37 | + raise ValueError(f"Expected content type {JSON_FORMAT}, got {content_type}") |
| 38 | + |
| 39 | +print("Downloaded JSON file listing.") |
| 40 | + |
| 41 | +parsed_data = json_response.json() |
| 42 | +uris = [file["uri"] for file in parsed_data["arrow_stream_files"]] |
| 43 | + |
| 44 | +if not all(uri.endswith(".arrows") for uri in uris): |
| 45 | + raise ValueError(f"Some listed files do not have extension '.arrows'") |
| 46 | + |
| 47 | +print(f"Parsed JSON and found {len(uris)} Arrow stream files.") |
| 48 | + |
| 49 | +tables = {} |
| 50 | + |
| 51 | +for uri in uris: |
| 52 | + arrow_response = requests.get(uri) |
| 53 | + |
| 54 | + response_status = arrow_response.status_code |
| 55 | + if not response_status == 200: |
| 56 | + raise ValueError(f"Expected response status 200, got {response_status}") |
| 57 | + |
| 58 | + content_type = arrow_response.headers.get("Content-Type", "") |
| 59 | + if not content_type.startswith(ARROW_STREAM_FORMAT): |
| 60 | + raise ValueError(f"Expected content type {ARROW_STREAM_FORMAT}, got {content_type}") |
| 61 | + |
| 62 | + filename = os.path.basename(uri) |
| 63 | + |
| 64 | + print(f"Downloaded file '{filename}'.") |
| 65 | + |
| 66 | + tablename = os.path.splitext(filename)[0] |
| 67 | + with pa.ipc.open_stream(arrow_response.content) as reader: |
| 68 | + tables[tablename] = reader.read_all() |
| 69 | + |
| 70 | + print(f"Loaded into in-memory Arrow table '{tablename}'.") |
0 commit comments