製品をチェック

無償トライアル:

無償トライアルへ

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

Azure Data Lake Storage Python Connector

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

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

お問い合わせ

Python のDash ライブラリを使って、Azure Data Lake Storage データ に連携するウェブアプリケーションを開発


CData Python Connector for ADLS を使って、Azure Data Lake Storage にデータ連携するPython ウェブアプリケーションを開発できます。pandas とDash を使って作成してみます。


azuredatalake ロゴ画像
python ロゴ画像

Python

Python ロゴ画像

Pythonエコシステムには、多くのモジュールがあり、システム構築を素早く効率的に行うことができます。CData Python Connector for ADLS を使うことで、pandas モジュールとDash フレームワークでAzure Data Lake Storage にデータ連携するアプリケーションを効率的に開発することができます。本記事では、pandas、Dash とCData Connector を使って、Azure Data Lake Storage に連携して、Azure Data Lake Storage data をビジュアライズするシンプルなウエブアプリを作ります。

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 への接続

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.

以下の手順に従い、必要なモジュールをインストールし、Python オブジェクト経由でAzure Data Lake Storage にアクセスします。

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

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

pip install pandas
pip install dash
pip install dash-daq

Python でAzure Data Lake Storage データ をビジュアライズ

必要なモジュールとフレームワークがインストールされたら、ウェブアプリを開発していきます。コードのスニペットは以下の通りです。フルコードは記事の末尾に付いています。

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

import os
import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import cdata.adls as mod
import plotly.graph_objs as go

接続文字列を使ってデータへの接続を確立します。connect 関数を使ってCData Azure Data Lake Storage Connector にAzure Data Lake Storage data との接続を確立します。

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

Azure Data Lake Storage にクエリを実行

read_sql 関数を使って、padas からSQL 文を発行し、DataFrame に結果を格納します。

df = pd.read_sql("""SELECT FullPath, Permission FROM Resources WHERE Type = 'FILE'""", cnxn)

ウェブアプリケーションの設定

DataFrame に格納されたクエリ結果を使って、ウェブアプリにname、stylesheet、title を設定していきます。

app_name = 'dash-adlsedataplot'

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.title = 'CData + Dash'

Layout 設定

次に、Azure Data Lake Storage data をベースにした棒グラフを作詞し、アプリのレイアウトを設定します。

trace = go.Bar(x=df.FullPath, y=df.Permission, name='FullPath')

app.layout = html.Div(children=[html.H1("CData Extention + Dash", style={'textAlign': 'center'}),
	dcc.Graph(
		id='example-graph',
		figure={
			'data': [trace],
			'layout':
			go.Layout(alt='Azure Data Lake Storage Resources Data', barmode='stack')
		})
], className="container")

アプリをセットアップして、実行n

接続、アプリ、レイアウトを定義したら、アプリを実行してみましょう。Python コードの最後はこのようです。

if __name__ == '__main__':
    app.run_server(debug=True)

では、Python でウェブアプリを稼働させて、ブラウザでAzure Data Lake Storage data を見てみましょう。

python adls-dash.py
Azure Data Lake Storage data in a Dash web app (Salesforce is shown).

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

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



import os
import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import cdata.adls as mod
import plotly.graph_objs as go

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

df = pd.read_sql("SELECT FullPath, Permission FROM Resources WHERE Type = 'FILE'", cnxn)
app_name = 'dash-adlsdataplot'

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.title = 'CData + Dash'
trace = go.Bar(x=df.FullPath, y=df.Permission, name='FullPath')

app.layout = html.Div(children=[html.H1("CData Extention + Dash", style={'textAlign': 'center'}),
	dcc.Graph(
		id='example-graph',
		figure={
			'data': [trace],
			'layout':
			go.Layout(alt='Azure Data Lake Storage Resources Data', barmode='stack')
		})
], className="container")

if __name__ == '__main__':
    app.run_server(debug=True)