Summary
How to connect to Dremio using the Dremio JDBC driver with Python and JayDeBeApi and utilize Inbound Impersonation.
Overview
The following article gives an example of using Python and JDBC to connect to Dremio and use inbound impersonation.
Steps to Resolve
This is an example Python script that utilizes the JayDeBeApi to use the JDBC driver with Python3.
import jaydebeapi
# Path to the Dremio JDBC driver JAR file
JDBC_DRIVER_JAR_PATH='INSERT_PATH_TO_DREMIO_JDBC_DRIVER'
# Driver name as specified in the Dremio JDBC driver documentation
DRIVERNAMES = ['com.dremio.jdbc.Driver']
# Connection string format: jdbc:dremio:<host>:<port>/<project>
CONNECTION_STRING = 'jdbc:dremio:direct:31010'
try:
# Establish the connection to Dremio
# Note the use of impersonation_target in the connection string . This is the user whom
# you want the query to run as.
# Note: ensure you have theexec.impersonation.inbound_policies
support key set .
# eg: the following allows user dremio to impersonation user ii.
# ALTER SYSTEM "exec".impersonation.inbound_policies = '
# [ {proxy_principals:{users:["dremio","adminUser"] }
# ,target_principals:{users:["ii","ii"]}}]'
conn = jaydebeapi.connect(
DRIVERNAMES[0],
CONNECTION_STRING,
{ 'user': "dremio", 'password': "XXXXXXX",'impersonation_target': "ii"},
JDBC_DRIVER_JAR_PATH
)
print( conn)
print("Connected successfully to Dremio!")
# Create a cursor and execute a query
with conn.cursor() as cur:
cur.execute('SELECT 1,2,3')
# Fetch and print the results
rows = cur.fetchall()
for row in rows:
print(row)
except jaydebeapi.Error as e:
print("Error connecting to Dremio: {e}")
finally:
if conn:
conn.close()
print("Connection closed.")
Additional Resources
Please see the documentation on Inbound Impersonation