Skip to content

Add Flight Service#4

Merged
lmangani merged 4 commits intomainfrom
flight
Jan 28, 2025
Merged

Add Flight Service#4
lmangani merged 4 commits intomainfrom
flight

Conversation

@lmangani
Copy link
Contributor

@lmangani lmangani commented Jan 28, 2025

Based on https://github.com/definite-app/duck-takes-flight/tree/main

Tests

Generic

import pyarrow.flight as flight
import pyarrow as pa
import pandas as pd

class FlightClient:
    def __init__(self, host="localhost", port=8815):
        self.client = flight.FlightClient(f"grpc://{host}:{port}")

    def execute_query(self, query):
        """Executes a query on the Flight server and returns the result as a DataFrame."""
        # Create a Flight ticket with the SQL query
        ticket = flight.Ticket(query.encode("utf-8"))

        # Request the query execution
        info = self.client.get_flight_info(flight.FlightDescriptor.for_command(query))
        reader = self.client.do_get(info.endpoints[0].ticket)

        # Convert the result to a Pandas DataFrame
        data = reader.read_all()
        return data.to_pandas()

if __name__ == "__main__":
    client = FlightClient()
    query = "SELECT version()"
    print("Executing query:", query)
    result_df = client.execute_query(query)
    print("Query result:")
    print(result_df)
Executing query: SELECT version()
Query result:
  "version"()
0      v1.1.3

Headers

from pyarrow.flight import FlightClient, Ticket, FlightCallOptions 
import json
import pandas
import tabulate

sql="""SELECT version()"""
  
flight_ticket = Ticket(sql)

token = (b"authorization", bytes(f"user:password".encode('utf-8')))
options = FlightCallOptions(headers=[token])
client = FlightClient(f"grpc://localhost:8815")

reader = client.do_get(flight_ticket, options)
arrow_table = reader.read_all()
# Use pyarrow and pandas to view and analyze data
data_frame = arrow_table.to_pandas()
print(data_frame.to_markdown())
|    | "version"()   |
|---:|:--------------|
|  0 | v1.1.3        |

@lmangani lmangani merged commit e40615c into main Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant