0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

TableauのビューとデータソースをDatabricksのダッシュボードに表示してみた

Last updated at Posted at 2025-05-05

前提

・利用環境:Databricks、Tableau Cloud

参考ドキュメント

参考ブログ

手順

1.Tableau Cloudの個人アクセストークンを作成

参考:以下のブログの手順2

2.TableauビューのイメージをDatabricksのダウンロード

①Databricksのホーム画面で、「新規」→「ノートブック」をクリック
image.png
②必要なライブラリをインストール

#ライブラリをインストール
!pip install requests

image.png
③DatabricksからTableau Cloudに認証して、Tokenを取得
以下の値は実際の値に置き換える
TABLEAU_TOKEN_VALUE
TABLEAU_TOKEN_NAME
TABLEAU_SERVER
TABLEAU_VERSION
site_url_id

# -*- coding: utf-8 -*-
import requests, json

TABLEAU_TOKEN_VALUE = '手順1で作成したTableauトークンのシークレット'  #TODO Tableauの情報
TABLEAU_TOKEN_NAME = '手順1で作成したTableauトークン名'  #TODO  個人用アクセストークンの「トークン名」
TABLEAU_SERVER = '例:prod-apnortheast-a.online.tableau.com'  #TODO Tableauサーバのアドレス
TABLEAU_VERSION = '3.19'  #TODO Tabeleau ServerのAPI version

#Tableauサーバーへサインインする
server_name = TABLEAU_SERVER
version = TABLEAU_VERSION
site_url_id = "Tableau Cloudのサイト名"    # Site URLのパス、空白の場合はDefaultになる

# Personal Access Token を指定する
personal_access_token_name = TABLEAU_TOKEN_NAME      # Personal access tokenの名前
personal_access_token_secret = TABLEAU_TOKEN_VALUE   # Personal access token

signin_url = "https://{server}/api/{version}/auth/signin".format(server=server_name, version=version)   
payload = { "credentials": { "personalAccessTokenName": personal_access_token_name, "personalAccessTokenSecret": personal_access_token_secret, "site": {"contentUrl": site_url_id }}}

headers = {
	'accept': 'application/json',
	'content-type': 'application/json'
}
    
# サーバにRequestを送信する
req = requests.post(signin_url, json=payload, headers=headers, verify=False)
req.raise_for_status()

#HTTP Responseを取得
response = json.loads(req.content)

#credentialsのエレメントからTokenを取得する
token = response["credentials"]["token"]

#Site IDエレメントからSite IDを取得する
site_id = response["credentials"]["site"]["id"]

print('Sign in successful!')
print('Token: {token}'.format(token=token))
print('Site ID: {site_id}'.format(site_id=site_id))

image.png
④TableauのビューIDを検索
VIEW_URL_NAMEは、実際のビュー名に変更

VIEW_URL_NAME = 'Overview' #TODO viewUrlNameの値(シート名)を入れる
#view idを探す
query_value=  "filter=viewUrlName:eq:{viewUrlName}".format(viewUrlName=VIEW_URL_NAME)
headers = {'X-tableau-auth':token,
           'accept': 'application/json'}

#GET /api/api-version/sites/site-id/views?filter=viewUrlName:eq:view-name
view_get_url="https://{server}/api/{version}/sites/{siteid}/views?{query}".format(
        server=TABLEAU_SERVER, version=TABLEAU_VERSION, siteid=site_id,  query= query_value)

outDataOrig = requests.get(view_get_url,headers=headers).content
outDataDecode= outDataOrig.decode()
json_out = json.loads(outDataDecode)
view_id = json_out['views']['view'][0]['id']

print('view id: {view_id}'.format(view_id=view_id))

image.png
⑤Tableauビューをボリュームにダウンロードする
以下の値は実際の値に置き換える
catalog
schema
volume
file_name
table_name

catalog =  "aibi"
schema =  "default"
volume =  "vol"

file_name ="overview.png"
table_name ="overview"
path_volume = "/Volumes/" + catalog + "/" + schema + "/" + volume + "/" + file_name
path_table = catalog + "." + schema

query_value = "resolution=high" 
headers = {'X-tableau-auth':token}
  
#GET /api/api-version/sites/site-id/views/view-id/image?vf_<fieldname>=filter-value
image_get_url="https://{server}/api/{version}/sites/{siteid}/views/{viewid}/image?{query}".format(
        server=TABLEAU_SERVER, version=TABLEAU_VERSION, siteid=site_id, viewid=view_id, query= query_value)

urlData = requests.get(image_get_url,headers=headers).content
   
with open(path_volume, mode='wb') as f:
    f.write(urlData)

print(path_volume) # Show the complete path
print(path_table) # Show the complete path

⑥TableauビューがDatabricksのボリュームにダウンロードされたことを確認
image.png

3.TableauビューのデータをDatabricksにダウンロードし、テーブルを作成

①Tableauビューのデータをボリュームにダウンロードする
以下の値は実際の値に置き換える
data_name

data_name ="overview.csv"   #TODO 保存先のファイル名
data_path_volume = "/Volumes/" + catalog + "/" + schema + "/" + volume + "/" + data_name
query_value = ""
    
headers = {'X-tableau-auth':token}
  
#GET /api/api-version/sites/site-id/views/view-id/data?vf_<fieldname>=filter-value
data_get_url="https://{server}/api/{version}/sites/{siteid}/views/{viewid}/data?{query}".format(
    server=TABLEAU_SERVER, version=TABLEAU_VERSION, siteid=site_id, viewid=view_id, query= query_value)
urlData = requests.get(data_get_url,headers=headers).content  
outtext = urlData.decode()

#取得したデータを出力してみる
print(outtext)

#ファイルに出力する
with open(data_path_volume, mode='w') as f:
    f.write(outtext)

image.png
②Tableauのデータがボリュームにダウンロードされたことを確認
image.png
③ダウンロードしたCSV データを データフレーム に読み込む

df = spark.read.csv(f"{data_path_volume}",
  header=True,
  inferSchema=True,
  sep=",")

image.png
④列名に日本語や制約記号がある場合は、列名を変更
例:

df = df.withColumnRenamed("Country/Region", "Country_Region")
df = df.withColumnRenamed("State/Province", "State_Province")
df = df.withColumnRenamed("経度 (生成)", "Longitude")
df = df.withColumnRenamed("緯度 (生成)", "Latitude")
df = df.withColumnRenamed("Profit Ratio", "Profit_Ratio")

image.png

⑤データフレームをテーブルに保存する

df.write.mode("overwrite").saveAsTable(f"{path_table}" + "." + f"{table_name}")

image.png
⑥テーブルが作成されてることを確認
image.png

4.DatabricksのダッシュボードにTableauビューとデータを設定

①「新規」→「ダッシュボード」をクリック
image.png
②「テキストボックスを追加」アイコンをクリックし、ダッシュボードに配置
image.png
③ボリュームにダウンロードしているTableauビューイメージを指定

例:

![Overview](https://<DatabricksのインスタンスURL>/ajax-api/2.0/fs/files/Volumes/aibi/default/vol/overview.png)

④外部にカーソルをクリックすると、Tableauビューのイメージがダッシュボードに表示される
image.png
⑤ダウンロードしたTableauのデータをDatabricksのダッシュボードに追加
「データ」タブをクリック
image.png
⑥「テーブルを選択」をクリックし、手順3で作成したテーブルを選択
image.png
⑦「Canvas」をクリックし、ダッシュボード画面に戻り、「ビジュアライゼーションを追加」アイコンをクリック
image.png
⑧自然言語を入力し、Enterを押すと、対応グラフが表示される、問題なければ承認をクリック
image.png
⑨「公開」をクリック
image.png
⑩「Genie」をOnにし、「公開」
image.png

5.DatabricksのGenieを使ってみる

①「Genieに聞く」をクリック
image.png
②ダッシュボードを見ながら、自然言語で質問すると、回答を得ることができる
image.png
③回答は日本語で設定することも可能
1)新規でGenieスペースを作成し、設定から日本語で回答するように指示を設定
image.png
2)「共有」→「リンクをコピー」をクリック
image.png
3)ダッシュボードの公開から「既存のGenie spaceをリンク」を選択し、コピーしたGenieリンクを貼り付ける
image.png
4)日本語で回答が返ってくることを確認できる
image.png
5)TableauのビューをDatabricksで見ながら、自然言語でドリルダウン分析が可能

参考手順(動画)

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?