Python でShipStation データを変換・出力するETL 処理を作る方法

CData Python Connector for ShipStation とpetl モジュールを使って、ShipStation データを変換後にCSV ファイルに吐き出すETL 処理を実装します。

加藤龍彦
ウェブデベロッパー

最終更新日:2020-04-02
shipstation ロゴ

CData

python ロゴ画像
Python ロゴ

こんにちは!ウェブ担当の加藤です。マーケ関連のデータ分析や整備もやっています。

Pythonエコシステムには多くのモジュールがあり、システム構築を素早く効率的に行うことができます。本記事では、CData Python Connector for ShipStation とpetl フレームワークを使って、ShipStation データにPython から接続してデータを変換、CSV に出力するETL 変換を実装してみます。

CData Python Connector は効率的なデータ処理によりShipStation データ にPython から接続し、高いパフォーマンスを発揮します。ShipStation にデータをクエリする際、ドライバーはフィルタリング、集計などがサポートされている場合SQL 処理を直接ShipStation 側に行わせ、サポートされていないSQL 処理については、組み込みのSQL エンジンによりクライアント側で処理を行います(JOIN やSQL 関数など)。

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

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

pip install petl
pip install pandas

Python でShipStation データをETL 処理するアプリを構築

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

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

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

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

cnxn = mod.connect("APIKey='YourAPIKey';APISecret='YourAPISecret'")

Use the BASIC Authentication standard to connect.

  1. Login to your ShipStation account
  2. Click on the settings icon in the upper right corner. A column menu will show up on the left
  3. Click Account -> API Settings
  4. On the API Settings page, note the API Key and API Secret.

Authenticating to ShipStation

  • APIKey: Set this to the API key from the API settings page.
  • APISecret: Set this to the Secret key from the API settings page.

ShipStation をクエリするSQL 文の作成

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

sql = "SELECT Id, Color FROM Tags WHERE CustomerId = '1368175'"

ShipStation データ のETL 処理

DataFrame に格納されたクエリ結果を使って、petl でETL(抽出・変換・ロード)パイプラインを組みます。この例では、ShipStation データ を取得して、Color カラムでデータをソートして、CSV ファイルにデータをロードします。

table1 = etl.fromdb(cnxn,sql)

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

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

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

おわりに

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



フルソースコード

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

cnxn = mod.connect("APIKey='YourAPIKey';APISecret='YourAPISecret'")

sql = "SELECT Id, Color FROM Tags WHERE CustomerId = '1368175'"

table1 = etl.fromdb(cnxn,sql)

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

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

関連コンテンツ

トライアル・お問い合わせ

30日間無償トライアルで、CData のリアルタイムデータ連携をフルにお試しいただけます。記事や製品についてのご質問があればお気軽にお問い合わせください。