You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Python pyODBC and pyArrow connect to Dremio Sample Code
pyODBC
#----------------------------------# IMPORTS#----------------------------------## Import pyodbcimportpyodbc## import pandasimportpandasaspd## import environment variablesfromosimportenviron#----------------------------------# SETUP#----------------------------------token=environ.get("token", "personal token not defined")
connector="Driver={Dremio ODBC Driver 64-bit};ConnectionType=Direct;HOST=sql.dremio.cloud;PORT=443;AuthenticationType=Plain;"+f"UID=$token;PWD={token};ssl=true;"#----------------------------------# CREATE CONNECTION AND CURSOR#----------------------------------# establish connectioncnxn=pyodbc.connect(connector, autocommit=True)
# set encodingcnxn.setdecoding(pyodbc.SQL_CHAR, encoding='utf-8')
# creating a cursor to send messages through the connectioncursor=cnxn.cursor()
#----------------------------------# RUN QUERY#----------------------------------## run a queryrows=cursor.execute("SELECT * FROM \"@dremio.demo@gmail.com\".\"nyc-taxi-data\" limit 1000000").fetchall()
##convert into pandas dataframedf=pd.DataFrame([tuple(t) fortinrows])
print(df)
pyArrow
#----------------------------------# IMPORTS#----------------------------------## Import Pyarrowfrompyarrowimportflightfrompyarrow.flightimportFlightClient## import pandasimportpandasaspd## Get environment variablesfromosimportenvironconsttoken=environ.get('token', 'no personal token defined')
#----------------------------------# Setup#----------------------------------## Headers for Authenticationheaders= [
(b"authorization", f"bearer {token}".encode("utf-8"))
]
## Create Clientclient=FlightClient(location=("grpc+tls://data.dremio.cloud:443"))
#----------------------------------# Function Definitions#----------------------------------## makeQuery functiondefmake_query(query, client, headers):
## Get Schema Description and build headersflight_desc=flight.FlightDescriptor.for_command(query)
options=flight.FlightCallOptions(headers=headers)
schema=client.get_schema(flight_desc, options)
## Get ticket to for query execution, used to get resultsflight_info=client.get_flight_info(flight.FlightDescriptor.for_command(query), options)
## Get Results results=client.do_get(flight_info.endpoints[0].ticket, options)
returnresults#----------------------------------# Run Query#----------------------------------results=make_query("SELECT * FROM \"@dremio.demo@gmail.com\".\"nyc-taxi-data\" limit 1000000", client, headers)
# convert to pandas dataframedf=results.read_pandas()
print(df)