1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Databricks SQL Statement Execution APIを使ってみる

Last updated at Posted at 2023-03-08

こちらのDatabricks SQL Statement Execution APIを試してみます。

要件

準備

  1. パーソナルアクセストークンを生成してメモしておきます。

  2. アクセスするDatabricks SQLウェアハウスがなければ作成します。

  3. SQLウェアハウスの画面にアクセスしてウェアハウスのIDをコピーしておきます。
    Screenshot 2023-03-08 at 10.14.42.png

  4. ワークスペースのホスト名をメモしておきます。https://や末尾の/は不要です。

  5. REST APIを呼び出すマシンで環境変数を設定します。以下の例はMacでのターミナルで実行しています。SQL文はSELECT * FROM hive_metastore.japan_covid_takaakiyayoidatabrickscom.covid_cases LIMIT 10としています。

    export DATABRICKS_TOKEN='<パーソナルアクセストークン>'
    export DATABRICKS_HOST='<Databricksワークスペースのホスト名>'
    export DATABRICKS_SQL_WAREHOUSE_ID='<SQLウェアハウスID>'
    
  6. curlを使ってREST APIを呼び出します。

    curl --request POST \
    https://${DATABRICKS_HOST}/api/2.0/sql/statements/ \
    --header "Authorization: Bearer ${DATABRICKS_TOKEN}" \
    --header "Content-Type: application/json" \
    --data '{
      "warehouse_id": "'"$DATABRICKS_SQL_WAREHOUSE_ID"'",
      "catalog": "samples",
      "schema": "tpch",
      "statement": "SELECT * FROM hive_metastore.japan_covid_takaakiyayoidatabrickscom.covid_cases LIMIT 10"
    }' | jq
    

以下のレスポンスが返ってきます。

JSON
{
  "statement_id": "01edbd4f-8398-1304-a164-c5f1d85fbb8a",
  "status": {
    "state": "SUCCEEDED"
  },
  "manifest": {
    "format": "JSON_ARRAY",
    "schema": {
      "column_count": 5,
      "columns": [
        {
          "name": "Prefecture",
          "type_text": "STRING",
          "type_name": "STRING",
          "position": 0
        },
        {
          "name": "Cases",
          "type_text": "BIGINT",
          "type_name": "LONG",
          "position": 1
        },
        {
          "name": "date_timestamp",
          "type_text": "TIMESTAMP",
          "type_name": "TIMESTAMP",
          "position": 2
        },
        {
          "name": "pref_no",
          "type_text": "INT",
          "type_name": "INT",
          "position": 3
        },
        {
          "name": "Area",
          "type_text": "STRING",
          "type_name": "STRING",
          "position": 4
        }
      ]
    },
    "total_chunk_count": 1,
    "chunks": [
      {
        "chunk_index": 0,
        "row_offset": 0,
        "row_count": 10
      }
    ],
    "total_row_count": 10
  },
  "result": {
    "chunk_index": 0,
    "row_offset": 0,
    "row_count": 10,
    "data_array": [
      [
        "Hokkaido",
        "24",
        "2022-01-01T00:00:00.000Z",
        "1",
        "北海道地方"
      ],
      [
        "Hokkaido",
        "6",
        "2022-01-02T00:00:00.000Z",
        "1",
        "北海道地方"
      ],
      [
        "Hokkaido",
        "15",
        "2022-01-03T00:00:00.000Z",
        "1",
        "北海道地方"
      ],
      [
        "Hokkaido",
        "30",
        "2022-01-04T00:00:00.000Z",
        "1",
        "北海道地方"
      ],
      [
        "Hokkaido",
        "60",
        "2022-01-05T00:00:00.000Z",
        "1",
        "北海道地方"
      ],
      [
        "Hokkaido",
        "62",
        "2022-01-06T00:00:00.000Z",
        "1",
        "北海道地方"
      ],
      [
        "Hokkaido",
        "98",
        "2022-01-07T00:00:00.000Z",
        "1",
        "北海道地方"
      ],
      [
        "Hokkaido",
        "148",
        "2022-01-08T00:00:00.000Z",
        "1",
        "北海道地方"
      ],
      [
        "Hokkaido",
        "99",
        "2022-01-09T00:00:00.000Z",
        "1",
        "北海道地方"
      ],
      [
        "Hokkaido",
        "95",
        "2022-01-10T00:00:00.000Z",
        "1",
        "北海道地方"
      ]
    ]
  }
}

REST APIを使ってアプリケーションを構築するケースは多いと思いますので、こちらのREST APIをご活用いただくことで柔軟にデータアプリケーションを構築いいただけるかと思います。ぜひご活用ください!

Databricks 無料トライアル

Databricks 無料トライアル

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?