Discover how a bimodal integration strategy can address the major data management challenges facing your organization today.
Get the Report →Using the CData ODBC Driver for IBM Cloud Object Storage in PyCharm
Connect to IBM Cloud Object Storage as an ODBC data source in PyCharm using the CData ODBC Driver for IBM Cloud Object Storage.
The CData ODBC Drivers can be used in any environment that supports loading an ODBC Driver. In this tutorial we will explore using the CData ODBC Driver for IBM Cloud Object Storage from within PyCharm. Included are steps for adding the CData ODBC Driver as a data source, as well as basic PyCharm code to query the data source and display results.
To begin, this tutorial will assume that you have already installed the CData ODBC Driver for IBM Cloud Object Storage as well as PyCharm.
Add Pyodbc to the Project
Follow the steps below to add the pyodbc module to your project.
- Click File -> Settings to open the project settings window.
- Click Project Interpreter from the Project: YourProjectName menu.
- To add pyodbc, click the + button and enter pyodbc.
- Click Install Package to install pyodbc.
Connect to IBM Cloud Object Storage
You can now connect with an ODBC connection string or a DSN. See the Getting Started section in the CData driver documentation for a guide to creating a DSN on your OS.
Register a New Instance of Cloud Object Storage
If you do not already have Cloud Object Storage in your IBM Cloud account, follow the procedure below to install an instance of SQL Query in your account:
- Log in to your IBM Cloud account.
- Navigate to the page, choose a name for your instance and click Create. You will be redirected to the instance of Cloud Object Storage you just created.
Connecting using OAuth Authentication
There are certain connection properties you need to set before you can connect. You can obtain these as follows:
API Key
To connect with IBM Cloud Object Storage, you need an API Key. You can obtain this as follows:
- Log in to your IBM Cloud account.
- Navigate to the Platform API Keys page.
- On the middle-right corner click "Create an IBM Cloud API Key" to create a new API Key.
- In the pop-up window, specify the API Key name and click "Create". Note the API Key as you can never access it again from the dashboard.
Cloud Object Storage CRN
If you have multiple accounts, you will need to specify the CloudObjectStorageCRN explicitly. To find the appropriate value, you can:
- Query the Services view. This will list your IBM Cloud Object Storage instances along with the CRN for each.
- Locate the CRN directly in IBM Cloud. To do so, navigate to your IBM Cloud Dashboard. In the Resource List, Under Storage, select your Cloud Object Storage resource to get its CRN.
Connecting to Data
You can now set the following to connect to data:
- InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
- ApiKey: Set this to your API key which was noted during setup.
- CloudObjectStorageCRN (Optional): Set this to the cloud object storage CRN you want to work with. While the connector attempts to retrieve this automatically, specifying this explicitly is recommended if you have more than Cloud Object Storage account.
When you connect, the connector completes the OAuth process.
- Extracts the access token and authenticates requests.
- Saves OAuth values in OAuthSettingsLocation to be persisted across connections.
Below is the syntax for a DSN:
[CData IBMCloudObjectStorage Source]
Driver = CData ODBC Driver for IBM Cloud Object Storage
Description = My Description
ApiKey = myApiKey
CloudObjectStorageCRN = MyInstanceCRN
Region = myRegion
OAuthClientId = MyOAuthClientId
OAuthClientSecret = myOAuthClientSecret
Execute SQL to IBM Cloud Object Storage
Instantiate a Cursor and use the execute method of the Cursor class to execute any SQL statement.
import pyodbc
cnxn = pyodbc.connect('DRIVER={CData ODBC Driver for IBMCloudObjectStorage};ApiKey = myApiKey;CloudObjectStorageCRN = MyInstanceCRN;Region = myRegion;OAuthClientId = MyOAuthClientId;OAuthClientSecret = myOAuthClientSecret;')
cursor = cnxn.cursor()
cursor.execute("SELECT Key, Etag FROM Objects WHERE Bucket = 'someBucket'")
rows = cursor.fetchall()
for row in rows:
print(row.Key, row.Etag)
After connecting to IBM Cloud Object Storage in PyCharm using the CData ODBC Driver, you will be able to build Python apps with access to IBM Cloud Object Storage data as if it were a standard database. If you have any questions, comments, or feedback regarding this tutorial, please contact us at [email protected].