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?

BigQueryEmulatorで遊んでみる

Posted at

ドキュメント型DBとしてmongo DBがメジャーですが、データ分析にはあまり向いていないので、Big Queryを試してみようと思いました。
手元でサクッと試したくてBig Query emulatorを使ってローカルで立ててみました。

docker-compose.yaml
services:
  bigquery-emulator:
    image: ghcr.io/goccy/bigquery-emulator:latest
    container_name: bigquery-emulator
    ports:
      - "9050:9050"
    volumes:
      - ./testdata:/testdata
    command: [
      "--project=test-project",
      "--data-from-yaml=/testdata/data.yaml"
    ]
    restart: unless-stopped

データセットの構成と初期データ

data.yaml
projects:
  - id: test-project
    datasets:
      - id: my_dataset
        tables:
          - id: users
            columns:
              - name: id
                type: INTEGER
              - name: name
                type: STRING
              - name: profile
                type: STRING
              - name: history
                type: STRING
            data:
              - id: 1
                name: "taro"
                profile: |
                  {
                    "age": 30,
                    "address": {
                      "city": "Tokyo",
                      "zip": "1110000"
                    },
                    "skills": ["Docker", "Jenkins", "Unity"]
                  }
                history: |
                  [
                    {
                      "date": "2024-01-01",
                      "action": "login"
                    },
                    {
                      "date": "2024-01-03",
                      "action": "deploy"
                    }
                  ]
docker-compose up -d

で立ち上げます。
呼び出しには以下のスクリプトを使います。

commandPromptBQ.py
from google.cloud import bigquery
from google.auth.credentials import AnonymousCredentials

client = bigquery.Client(
    project="test-project",
    credentials=AnonymousCredentials(),
    client_options={"api_endpoint": "http://localhost:9050"}
)

while True:
    query = input("bq-emulator> ")
    if query.lower() in ["exit", "quit"]:
        break
    try:
        for row in client.query(query):
            print(row)
    except Exception as e:
        print("Error:", e)

googleパッケージが無ければ以下でインストールしておきます。

pip install google-cloud-bigquery

例えば以下のようにしてデータセットにクエリを投げることができます。

user> python3 commandPromptBQ.py  
bq-emulator> SELECT * FROM my_dataset.users;
Row((1, 'taro', '\n{\r\n  "age": 30,\r\n  "address": {\r\n    "city": "Tokyo",\r\n    "zip": "1110000"\r\n  },\r\n  "skills": ["Docker", "Jenkins", "Unity"]\r\n}\r\n', '\n[\r\n  {\r\n    "date": "2024-01-01",\r\n    "action": "login"\r\n  },\r\n  {\r\n    "date": "2024-01-03",\r\n    "action": "deploy"\r\n  }\r\n]'), {'id': 0, 'name': 1, 'profile': 2, 'history': 3})
bq-emulator> exit
user>
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?