Python Examples for Querying through JDBC/ODBC

Using Ascend's JDBC / ODBC Connection, developers can query Ascend directly from Python scripts. While File-Based Access is a high-throughput option for retrieving all of a component's records, this interface enables developers to execute a query from a script to retrieve a subset of the records they are interested in. The query executes in Ascend, which helps to decrease processing requirements of the client.

Python scripts connect to Ascend through their choice of JDBC or ODBC:

  • JDBC requires a Java runtime, requires the Apache-licensed Hive Driver
  • ODBC does not require Java, requires a license for the Simba Spark ODBC Driver

Python Script with JDBC Connection

Prerequisites:

📘

CAUTION - CVE-2021-44228

We are aware that the hive-jdbc driver referenced here contains a vulnerable version of log4j2. A patch has been submitted upstream to address the log4j2 dependency, but there is not yet a release associated with it. When that release is available we will update the version reference here.

In the meantime, after downloading the hive-jdbc jar, run this command in the same directory as it:

zip -q -d hive-jdbc-2.3.7-standalone.jar org/apache/logging/log4j/core/lookup/JndiLookup.class

This removes the JndiLookup class from the hive jar, eliminating the vulnerability and making the jar safe to use. You can verify that it worked by running the following:

unzip hive-jdbc-2.3.7-standalone.jar > output.txt

You can verify in output.txt that the JndiLookup class is no longer present.

Follow the below "Recipe" for sample code that creates a connection and runs a query.

Python Script with ODBC Connection

Prerequisites:

Follow the below "Recipe" for sample code that creates a connection and runs a query.