import pyarrow as pa
import pyarrow.flight as flight
class MyFlightServer(flight.FlightServerBase):
def get_flight_info(self, context, descriptor):
# Define schema
schema = pa.schema([
('id', pa.int64()),
('name', pa.string())
])
# Create endpoint
endpoint = flight.FlightEndpoint(
ticket=b"ticket-123",
locations=[flight.Location.for_grpc_tcp("localhost", 8815)]
)
return flight.FlightInfo(
schema=schema,
descriptor=descriptor,
endpoints=[endpoint],
total_records=-1,
total_bytes=-1
)
def do_get(self, context, ticket):
# Create sample data
schema = pa.schema([
('id', pa.int64()),
('name', pa.string())
])
data = [
pa.array([1, 2, 3]),
pa.array(['Alice', 'Bob', 'Charlie'])
]
batch = pa.record_batch(data, schema=schema)
return flight.RecordBatchStream(pa.Table.from_batches([batch]))
# Start server
if __name__ == '__main__':
location = flight.Location.for_grpc_tcp("0.0.0.0", 8815)
server = MyFlightServer(location)
print(f"Server listening on {location.uri}")
server.serve()