LoginSignup
0
2

More than 5 years have passed since last update.

Python で Domo のデータセットを作成する

Last updated at Posted at 2017-09-21

access_token の取得は
Python で Domo の アクセストークンを取得する

実行すると、dataset_id が表示されます。これは、データの挿入、データセットの削除に使うので、控えておいて下さい。

#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#   main_create_dataset.py
#
#                   Sep/21/2017
#
# ------------------------------------------------------------------
import  sys
import  requests
#
#
from get_access_token import get_access_token_proc
from create_dataset import create_dataset_proc
from domo_config import domo_config_proc
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
config = domo_config_proc()
access_token = get_access_token_proc(config)
#
file_json="schema.json"
json_str = create_dataset_proc(access_token,file_json)
print(json_str)
#
sys.stderr.write("*** 終了 ***\n")
#
# ------------------------------------------------------------------
create_dataset.py
# -*- coding: utf-8 -*-
#
#   create_dataset.py
#
#                   Sep/21/2017
#
# ------------------------------------------------------------------
import  sys
import  requests
#
from file_io import file_to_str_proc
# ------------------------------------------------------------------
def create_dataset_proc(access_token,file_json):
    sys.stderr.write("*** create_dataset_proc *** start ***\n")
    headers = {"Authorization": "Bearer " + access_token,
            "Content-Type": "application/json",
            "Accept": "application/json"
        }
    params = {
        "includeHeader": "true",
        }
#
    url_datasets="https://api.domo.com/v1/datasets"
#
    json_str = file_to_str_proc(file_json)
#
    try:
        rr=requests.post(url_datasets,params=params,data=json_str,headers=headers)
        print(rr.status_code)
        sys.stderr.write(str(rr.status_code) + "\n")
    except Exception as ee:
        sys.stderr.write(str(ee) + "\n")
#
    sys.stderr.write("*** create_dataset_proc *** end ***\n")
#
    return rr.text
# ------------------------------------------------------------------
file_io.py
# -*- coding: utf-8 -*-
#
#   python_common/file_io.py
#
#                   Jul/31/2017
#
#
import  sys
import  string
#
# --------------------------------------------------------------------
def file_to_str_proc(file_in):
    str_out = ""
    try:
        fp_in = open(file_in,encoding='utf-8')
        str_out = fp_in.read()
        fp_in.close()
    except Exception as ee:
        sys.stderr.write("*** error *** file_to_str_proc ***\n")
        sys.stderr.write(str (ee))
#
    return  str_out
# --------------------------------------------------------------------
schema.json
{
  "name": "User_Datasets",
  "description": "Cities Sep/2017 PM 15:14",
  "rows": 0,
  "schema": {
    "columns": [
      {
        "type": "STRING",
        "name": "Id"
      },
      {
        "type": "STRING",
        "name": "City Name"
      },
      {
        "type": "LONG",
        "name": "Population"
      },
      {
        "type": "DATE",
        "name": "Date_mod"
      }
    ]
  }
}

API の仕様はこちら
Create a DataSet

0
2
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
2