Created
October 26, 2020 09:08
-
-
Save gdubya/a2489e4b9451720bb2be996725ce35bb to your computer and use it in GitHub Desktop.
An example of using the JayDeBeAPI for a JDBC connection to Apache Drill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jaydebeapi | |
def ResultIter(cursor, arraysize=1000): | |
'An iterator that uses fetchmany to keep memory usage down' | |
while True: | |
results = cursor.fetchmany(arraysize) | |
if not results: | |
break | |
for result in results: | |
yield result | |
class RegisterColumnNames(object): | |
def __init__(self, cursor, row): | |
for (attr, val) in zip((d[0] for d in cursor.description), row): | |
setattr(self, attr, val) | |
drill_conn = None | |
try: | |
drill_conn = jaydebeapi.connect( | |
"org.apache.drill.jdbc.Driver", | |
"jdbc:drill:drillbit=localhost", | |
[], | |
"/path/to/drill-jdbc-all-1.17.0.jar" | |
) | |
except Exception as e: | |
# A problem in the JPype bootstrapping requires us to retry this? | |
print("Caught exception {}. Trying again".format(e)) | |
drill_conn = jaydebeapi.connect( | |
"org.apache.drill.jdbc.Driver", | |
"jdbc:drill:drillbit=localhost", | |
[], | |
"/path/to/drill-jdbc-all-1.17.0.jar" | |
) | |
cursor = drill_conn.cursor() | |
cursor.execute("SELECT * FROM a_large_table") | |
for result in ResultIter(cursor): | |
row = RegisterColumnNames(cursor, result) | |
# a_large_table has a column named "my_value" | |
my_value = row.my_value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment