本記事では CData サポート担当からこんなことを聞かれたらどこを確認すべきか?という観点で、よく頂くお問合せ内容をご紹介します。
記事はこちら →Python エコシステムには多くのモジュールがあり、システム構築を素早く効率的に行うのに役立ちます。CData Python Connector for AAS は、pandas、Matplotlib モジュール、SQLAlchemy ツールキットから使用することで Azure Analysis Services にデータ連携するPython アプリケーションを構築し、Azure Analysis Services をビジュアライズできます。本記事では、pandas、SQLAlchemy、およびMatplotlib のビルトイン機能でAzure Analysis Services にリアルタイムアクセスし、クエリを実行し、結果をビジュアライズする方法を説明します。
CData Python Connector は、ビルトインされた効率的なデータプロセスにより、リアルタイムAzure Analysis Services データにPython からアクセスし、高いパフォーマンスと接続性を発揮します。Azure Analysis Services に複雑なクエリを投げる際に、ドライバーはフィルタリング、集計などがサポートされている場合、SQL 処理を直接Azure Analysis Services 側に行わせ、サポートされていないSQL 処理については、組み込まれたSQL エンジンによりクライアント側で処理を行います(特にJOIN やSQL 関数など)。
Azure Analysis Services への連携は、RDB ソースへのアクセスと同感覚で行うことができます。必要な接続プロパティを使って接続文字列を作成します。本記事では、接続文字列をcreate_engine 関数のパラメータとして送ります。
To connect to Azure Analysis Services, set the Url property to a valid server, for instance, asazure://southcentralus.asazure.windows.net/server, in addition to authenticating. Optionally, set Database to distinguish which Azure database on the server to connect to.
Azure Analysis Services uses the OAuth authentication standard. OAuth requires the authenticating user to interact with Azure Analysis Services using the browser. You can connect without setting any connection properties for your user credentials. See the Help documentation for more information.
以下の手順に従い、必要なモジュールをインストールし、Python オブジェクト経由でAzure Analysis Services にアクセスします。
pip で、pandas & Matplotlib モジュールおよび、SQLAlchemy ツールキットをインストールします:
pip install pandas pip install matplotlib pip install sqlalchemy
以下のようにモジュールをインポートします:
import pandas import matplotlib.pyplot as plt from sqlalchemy import create_engine
次は、接続文字列で接続を確立します。create_engine 関数を使って、Azure Analysis Services に連携するEngne を作成します。.
engine = create_engine("aas:///?URL=asazure://REGION.asazure.windows.net/server&InitiateOAuth=GETANDREFRESH&OAuthSettingsLocation=/PATH/TO/OAuthSettings.txt")
pandas のread_sql 関数を使って好きなSQL を発行して、DataFrame にデータを格納します。
df = pandas.read_sql("""SELECT Country, Education FROM Customer WHERE Country = 'Australia'""", engine)
DataFrame に格納されたクエリ結果に対して、plot 関数をつかって、Azure Analysis Services data をグラフで表現してみます。show メソッドはグラフを新しいウィンドウに表示します。
df.plot(kind="bar", x="Country", y="Education") plt.show()
Azure Analysis Services Python Connector の30日の無償トライアル をぜひダウンロードして、Azure Analysis Services への接続をPython アプリやスクリプトから簡単に作成しましょう。
import pandas import matplotlib.pyplot as plt from sqlalchemy import create_engin engine = create_engine("aas:///?URL=asazure://REGION.asazure.windows.net/server&InitiateOAuth=GETANDREFRESH&OAuthSettingsLocation=/PATH/TO/OAuthSettings.txt") df = pandas.read_sql("""SELECT Country, Education FROM Customer WHERE Country = 'Australia'""", engine) df.plot(kind="bar", x="Country", y="Education") plt.show()