製品をチェック

無償トライアル:

無償トライアルへ

製品の情報と無償トライアルへ:

Azure Data Lake Storage Python Connector

Azure Data Lake Storage データ連携用のPython コネクタライブラリ。Azure Data Lake Storage データをpandas、SQLAlchemy、Dash、petl などの人気のPython ツールにシームレスに統合。

データ連携でお困りですか?

お問い合わせ

Python でAzure Data Lake Storage データをETL


CData Python Connector for ADLS を使って、Python petl でAzure Data Lake Storage data のETL 連携・パイプラインアプリケーションを作成。


azuredatalake ロゴ画像
python ロゴ画像

Python

Python ロゴ画像

Pythonエコシステムには、多くのモジュールがあり、システム構築を素早く効率的に行うことができます。CData Python Connector for ADLS とpetl フレームワークを使って、Azure Data Lake Storage に連携するPython アプリや、Azure Data Lake Storage データをETL することが可能です。本記事では、CData Python Connector をpetl と一緒に使い、ETL 処理を実装します。

CData Python Connector は、ビルトインされた効率的なデータプロセスにより、リアルタイムAzure Data Lake Storage data データにPython からアクセスし、高いパフォーマンスと接続性を発揮します。Azure Data Lake Storage に複雑なクエリを投げる際に、ドライバーはフィルタリング、集計などがサポートされている場合、SQL 処理を直接Azure Data Lake Storage 側に行わせ、サポートされていないSQL 処理については、組み込まれたSQL エンジンによりクライアント側で処理を行います(特にJOIN やSQL 関数など)。

Azure Data Lake Storage Data への接続

Azure Data Lake Storage data への連携は、RDB ソースへのアクセスと同感覚で行うことができます。必要な接続プロパティを使って接続文字列を作成します。本記事では、接続文字列をcreate_engine 関数のパラメータとして送ります。

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:

  1. Sign in to your Azure Account through the .
  2. Select "Azure Active Directory".
  3. Select "App registrations".
  4. Select "New application registration".
  5. Provide a name and URL for the application. Select Web app for the type of application you want to create.
  6. 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.
  7. 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.

CData Azure Data Lake Storage Connector をインストールしたら、次のように必要なモジュールをインストールし、Python オブジェクトでAzure Data Lake Storage にアクセスします。

必要なモジュールのインストール

pip で必要なモジュールおよびフレームワークをインストールします:

pip install petl
pip install pandas

Python でAzure Data Lake Storage データをETL 処理するアプリを構築

モジュールとフレームワークをインストールしたら、ETL アプリケーションを組んでいきます。コードのスニペットは以下の通りです。フルコードは記事の末尾に付いています。

CData Connector を含むモジュールをインポートします:

import petl as etl
import pandas as pd
import cdata.adls as mod

接続文字列で接続を確立します。connect 関数を使って、CData Azure Data Lake Storage Connector からAzure Data Lake Storage への接続を行います

cnxn = mod.connect("Schema=ADLSGen2;Account=myAccount;FileSystem=myFileSystem;AccessKey=myAccessKey;InitiateOAuth=GETANDREFRESH;OAuthSettingsLocation=/PATH/TO/OAuthSettings.txt")")

Azure Data Lake Storage をクエリするSQL 文の作成

Azure Data Lake Storage にはSQL でデータアクセスが可能です。Resources エンティティからのデータを読み出します。

sql = "SELECT FullPath, Permission FROM Resources WHERE Type = 'FILE'"

Azure Data Lake Storage Data のETL 処理

DataFrame に格納されたクエリ結果を使って、petl でExtract(取得)、Transform(加工)、Load(ロード)を組みます。この例では、Azure Data Lake Storage data を取得して、Permission カラムでデータをソートして、CSV ファイルにデータをロードします。

table1 = etl.fromdb(cnxn,sql)

table2 = etl.sort(table1,'Permission')

etl.tocsv(table2,'resources_data.csv')

CData Python Connector for ADLS を使えば、データベースを扱う場合と同感覚で、Azure Data Lake Storage data を扱うことができ、petl のようなETL パッケージから直接データにアクセスが可能になります。

製品の無償トライアル情報

Azure Data Lake Storage Python Connector の30日の無償トライアル をぜひダウンロードして、Azure Data Lake Storage data への接続をPython アプリやスクリプトから簡単に作成しましょう。



フルソースコード

import petl as etl
import pandas as pd
import cdata.adls as mod

cnxn = mod.connect("Schema=ADLSGen2;Account=myAccount;FileSystem=myFileSystem;AccessKey=myAccessKey;InitiateOAuth=GETANDREFRESH;OAuthSettingsLocation=/PATH/TO/OAuthSettings.txt")")

sql = "SELECT FullPath, Permission FROM Resources WHERE Type = 'FILE'"

table1 = etl.fromdb(cnxn,sql)

table2 = etl.sort(table1,'Permission')

etl.tocsv(table2,'resources_data.csv')