PythonによるTableデータ抽出のリフレッシュ

11004 ワード

背景:現在、Tableauレポートはタイミングタスクによってリフレッシュされていますが、ETLタスクの実行が完了すると、タイミングまで待つのではなく、Tableauリフレッシュタスクが自動的に開始され、待ち時間が節約されます.
シナリオ:tableauデータ抽出はtabcmdコマンドにより端末で行うことができ、それに応じてpythonのosを通過することができる.system/os.popenメソッドはcmdコマンドを実行してデータをリフレッシュします.すなわち、
  • pythonでosを使用する.System(cmd_command)はcmd_を実行するcommandコマンド.
  • そしてこのcmd_commandはtabcmdコマンドです.

  • tabcmdコマンドヘルプドキュメントはこちらを参照
    ≪プロセス|Process|emdw≫:ここでは、プロセスの3つのステップに対応する3つのコマンドのみを使用します.
  • tableauサーバへのログイン:F"tabcmd login-s{server_public}-u{user}-p{pwd}"
  • リフレッシュデータ抽出:F’tabcmd refreshextracts--project"{project}--workbook"{workbook}'
  • refreshextractsコマンドは、データ抽出を開始するだけで、データリフレッシュ完了計算の完了ではなく、起動に成功すると完了します.
  • tableauサーバを終了:tabcmd logout
  • **
    前提条件:
    ローカルコンピュータではtabcmdの開発環境を構成しておく必要があります.
    Tableauサーバをインストールする場合、tabcmd環境を一緒にインストールするのがデフォルトですが、tabcmd環境がない場合は、他のマシンにTableauサーバをリモートで接続する必要がある場合があります.例えば、Tableauサーバはxxxです.xxx.xxx.140上、私は今xxxにいます.xxx.xxx.160サーバが接続され、160サーバにtabcmd環境がない場合は、tabcmdコマンドを使用するには、まず160にtabcmd環境を構成する必要があります.インストールおよび使用手順については、次のドキュメントを参照してください.
    次に正式な内容に入ります.
    1.外部からプロファイルを読み込む
    セキュリティのため、ユーザー名のパスワードなどの情報は、すべてプログラムに直接書かず、外部から読み込まれます.
    # encoding=utf-8
    import os, sys
    import time
    
    CurrentFile = os.path.abspath(__file__)
    CurrentPath = os.path.dirname(CurrentFile)
    FatherPath = os.path.dirname(CurrentPath)
    sys.path.insert(0, FatherPath)
    
    import configparser
    
    cf = configparser.ConfigParser()
    cf.read(os.path.join(FatherPath, 'doc/config.ini'))
    tabcmd_dict = dict(cf.items('tabcmd-config'))
    #     
    tabcmd_path = tabcmd_dict['tabcmd_path']
    server_private = tabcmd_dict['server_private'] #   ip,   tableau server       
    server_public = tabcmd_dict['server_public'] #   ip,    
    user = tabcmd_dict['user']
    pwd = tabcmd_dict['pwd']
    

    2.リモートログインTableサーバ
    def tabcmd_logoin(server_public, user, pwd):
        # os.chdir(tabcmd_path) #      tabcmd        ,          
        tabcmd_login = F"tabcmd login -s {server_public} -u {user} -p {pwd}"
        # os.system('chcp 65001') #              utf-8
        status = os.system(tabcmd_login)
        return status
    

    3.リフレッシュデータ抽出の実行
    def tabcm_refresh(project, workbook):
        tabcmd_refresh = F'tabcmd refreshextracts --project "{project}" --workbook "{workbook}"'
        status = os.system(tabcmd_refresh)  #   0    ,      
        if status == 0:
            status = 'success'
        else:
            print(tabcmd_refresh)
            status ='failed'
        now = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
        return {
         'project': project, 'workbook': workbook, 'statuas': status, 'update_start_time': now}
    
    

    4.ログインを終了する
    def tabcmd_logout():
        os.system('tabcmd logout')
    

    5.ループによる複数のデータ抽出
    def main(projects, workbooks):
        login_status = tabcmd_logoin(server_public, user, pwd)
        if login_status == 0:
            for project, workbook in zip(projects, workbooks):
                data = tabcm_refresh(project, workbook)
                print(data)
            tabcmd_logout()
        else:
            print('login failed!')
    

    ユニットテスト
    if __name__ == '__main__':
        projects = ['Default', ]
        workbooks = ['test', ]
        main(projects, workbooks)