CData ODBC Drivers は、ODBC ドライバーをサポートするあらゆる環境から利用可能です。本記事では、PyCharm からのCData ODBC Driver for ADLS の利用を説明します。CData ODBC Deriver をデータソースとして設定する方法、データソースをクエリして結果を表示するためのPyCharm の簡単なコードを含みます。
はじめに、このチュートリアルではCData ODBC Driver for ADLS とPyCharm が、既にインストールされていることを前提としています。
Pyodbc をプロジェクトに追加
以下のステップに従って、pyodbc モジュールをプロジェクトに追加します。
- [File]->[Settings]とクリックして[project settings]ウィンドウを開きます。
- [Project: YourProjectName]メニューから[Project Interpreter]をクリックします。
- pyodbc を追加するため、+ ボタンをクリックしてpyodbc と入力します。
- [Install Package]をクリックしてpyodbc をインストールします。
Azure Data Lake Storage への接続
これで、ODBC 接続文字列またはDSN で接続できます。お客様のOS でDSN を作成するためのガイドについては、CData ドライバードキュメントの[はじめに]セクションを参照してください。
Authenticating to a Gen 1 DataLakeStore Account
Gen 1 uses OAuth 2.0 in Azure AD for authentication.
For this, an Active Directory web application is required. You can create one as follows:
- Sign in to your Azure Account through the
[.
]- Select "Azure Active Directory".
- Select "App registrations".
- Select "New application registration".
- Provide a name and URL for the application. Select Web app for the type of application you want to create.
- Select "Required permissions" and change the required permissions for this app. At a minimum, "Azure Data Lake" and "Windows Azure Service Management API" are required.
- Select "Key" and generate a new key. Add a description, a duration, and take note of the generated key. You won't be able to see it again.
To authenticate against a Gen 1 DataLakeStore account, the following properties are required:
- Schema: Set this to ADLSGen1.
- Account: Set this to the name of the account.
- OAuthClientId: Set this to the application Id of the app you created.
- OAuthClientSecret: Set this to the key generated for the app you created.
- TenantId: Set this to the tenant Id. See the property for more information on how to acquire this.
- Directory: Set this to the path which will be used to store the replicated file. If not specified, the root directory will be used.
Authenticating to a Gen 2 DataLakeStore Account
To authenticate against a Gen 2 DataLakeStore account, the following properties are required:
- Schema: Set this to ADLSGen2.
- Account: Set this to the name of the account.
- FileSystem: Set this to the file system which will be used for this account.
- AccessKey: Set this to the access key which will be used to authenticate the calls to the API. See the property for more information on how to acquire this.
- Directory: Set this to the path which will be used to store the replicated file. If not specified, the root directory will be used.
以下はDSN の構文です。
[CData ADLS Source]
Driver = CData ODBC Driver for ADLS
Description = My Description
Schema = ADLSGen2
Account = myAccount
FileSystem = myFileSystem
AccessKey = myAccessKey
Azure Data Lake Storage へのクエリの実行
Cursor をインスタンス化し、Cursor クラスのexecute メソッドを使用してSQL ステートメントを実行します。
import pyodbc
cnxn = pyodbc.connect('DRIVER={CData ODBC Driver for ADLS};Schema = ADLSGen2;Account = myAccount;FileSystem = myFileSystem;AccessKey = myAccessKey;')
cursor = cnxn.cursor()
cursor.execute("SELECT FullPath, Permission FROM Resources WHERE Type = 'FILE'")
rows = cursor.fetchall()
for row in rows:
print(row.FullPath, row.Permission)
CData ODBC Driver を使用してPyCharm でAzure Data Lake Storage に接続すると、Azure Data Lake Storage にアクセスできるPython アプリケーションを標準データベースのように構築できるようになります。このチュートリアルに関する質問、コメント、フィードバックがある場合には、support@cdata.com までご連絡ください。