LoginSignup
0
1

More than 1 year has passed since last update.

Azure無料アカウントでCosmosDB for PostgreSQLを試してみました

Last updated at Posted at 2023-01-20

はじめに

前回はAzure CosmosDB for PostgreSQLの試用版を試しましたが、今回はAzure無料アカウントでCosmosDB for PostgreSQLのクラスタ作成してみました。

クラスタ作成

  • Azureポータルにサインインし、Cosmos DBの画面を開き、「Azure Cosmos DBアカウントの作成」をクリックする。

図1.png

  • APIオプションの選択画面で「Azure CosmosDB for PostgreSQL」の「作成」をクリックする。
    図2.png

  • 「Basics」タブで設定値を入力し、「次へ:ネットワーク」をクリックする。

    • 設定例
      • サブスクリプション:無料アカウントのサブスクリプションを選択
      • Resource group:リソースグループを選択※ない場合は新規作成をクリックして作成する。
      • Cluster name:任意のクラスタ名を入力
      • 場所:(Asia Pacific)Japan East
      • Scale:2 nodes, no High availability(HA)
      • PostgreSQL version:15
      • Admin username:citus(変更不可)
      • Password:任意のパスワードを入力
      • Confirm password:任意のパスワードを入力
        図3.png

Scaleは「Configure」をクリックすると以下の画面が表示され変更可能。
「Node count」を「2 nodes」に変更して保存をクリックする。

図4.png

  • 「ネットワーク」タブで設定値を以下のように入力し、「レビュー+作成」をクリックする。

    • 接続方法:Public access(allowed IP address)
    • 「Allow public access from Azure serviced and resourced within Azure to this cluster.」にチェックを入れる。
      図5.png
      図6.png
  • 「レビュー+作成」タブで内容を確認し、「作成」をクリックする。
    図7.png

  • デプロイが完了するまで数分待ち、以下の画面が表示されたら「リソースに移動」をクリックする。
    図8.png

  • クラスタが作成されたことを確認する。
    図9.png

クラスタ接続

  • 事前準備

    • 左ペインで「接続文字列」をクリックし、表示された画面で「psql」の文字列をコピーしてメモ帳などに張り付ける。
    • コピーした文字列の「{your_password}」の箇所をクラスタ作成で入力したパスワードに書き換える。
  • CloudShellからの接続

    • 画面上部のCloudShellのアイコンをクリックし、CloudShellを起動し、「PowerShell」をクリックする。
      図10.png

    • ストレージの作成をクリックする。
      図11.png

    • PowerShellが起動するので事前準備で用意した文字列を張り付けてEnterキーを押す。
      図12.png

    • 「citus=>」が表示されたら接続できている。

接続できない場合、左ペインのネットワークで「Allow public access from Azure serviced and resourced within Azure to this cluster.」にチェックが入っていることを確認する。
入っていない場合はチェックを入れて保存すると接続できる。

ローカルマシンからの接続をしたい場合、ネットワーク画面で「Add current client IP address」をクリックし、ローカルマシンのIPを許可するルールを作成することで接続可能になる。

クイックスタートを試す

前回同様クイックスタートを試してみました。
内容同じですが一応実行結果を載せます。

citus=> CREATE TABLE github_users
(
        user_id bigint,
        url text,
        login text,
        avatar_url text,
        gravatar_id text,
        display_login text
);
CREATE TABLE
citus=> CREATE TABLE github_events
(
        event_id bigint,
        event_type text,
        event_public boolean,
        repo_id bigint,
        payload jsonb,
        repo jsonb,
        user_id bigint,
        org jsonb,
        created_at timestamp
);
CREATE TABLE
citus=> CREATE INDEX event_type_index ON github_events (event_type);
CREATE INDEX
citus=> CREATE INDEX payload_index ON github_events USING GIN (payload jsonb_path_ops);
CREATE INDEX
citus=> SELECT create_distributed_table('github_users', 'user_id');
 create_distributed_table 
--------------------------
 
(1 row)

citus=> SELECT create_distributed_table('github_events', 'user_id');
 create_distributed_table 
--------------------------
 
(1 row)
citus=> SELECT * FROM create_extension('azure_storage');
 create_extension 
------------------
 
(1 row)

citus=> -- download users and store in table

COPY github_users FROM 'https://pgquickstart.blob.core.windows.net/github/users.csv.gz';

-- download events and store in table

COPY github_events FROM 'https://pgquickstart.blob.core.windows.net/github/events.csv.gz';
COPY 264308
COPY 126245

Cloud Shellから実施した場合は前回発生した文字コードの問題が発生しませんでした。

citus=> SELECT * FROM citus_tables;
  table_name   | citus_table_type | distribution_column | colocation_id | table_size | shard_count | table_owner | access_method 
---------------+------------------+---------------------+---------------+------------+-------------+-------------+---------------
 github_events | distributed      | user_id             |             1 | 260 MB     |          32 | citus       | heap
 github_users  | distributed      | user_id             |             1 | 39 MB      |          32 | citus       | heap
(2 rows)

citus=> -- count all rows (across shards)

SELECT count(*) FROM github_users;
 count  
--------
 264308
(1 row)
citus=> -- Find all events for a single user.
-- (A common transactional/operational query)

SELECT created_at, event_type, repo->>'name' AS repo_name
  FROM github_events
 WHERE user_id = 3861633;
     created_at      |  event_type  |              repo_name               
---------------------+--------------+--------------------------------------
 2016-12-01 06:28:44 | PushEvent    | sczhengyabin/Google-Image-Downloader
 2016-12-01 06:29:27 | CreateEvent  | sczhengyabin/Google-Image-Downloader
 2016-12-01 06:36:47 | ReleaseEvent | sczhengyabin/Google-Image-Downloader
 2016-12-01 06:42:35 | WatchEvent   | sczhengyabin/Google-Image-Downloader
 2016-12-01 07:45:58 | IssuesEvent  | sczhengyabin/Google-Image-Downloader
(5 rows)
citus=> -- Querying JSONB type. Query is parallelized across nodes.
-- Find the number of commits on the default branch per hour 

SELECT date_trunc('hour', created_at) AS hour,
       sum((payload->>'distinct_size')::int) AS num_commits
FROM   github_events
WHERE  event_type = 'PushEvent' AND
       payload @> '{"ref":"refs/heads/master"}'
GROUP BY hour
ORDER BY hour;
        hour         | num_commits 
---------------------+-------------
 2016-12-01 05:00:00 |       13051
 2016-12-01 06:00:00 |       43480
 2016-12-01 07:00:00 |       34254
 2016-12-01 08:00:00 |       29307
(4 rows)
citus=> -- DDL commands that are also parallelized

ALTER TABLE github_users ADD COLUMN dummy_column integer;
ALTER TABLE

(参考)クラスタの削除

コストが高く、無料枠を使い切りそうだったので最後にクラスタを削除しました。
削除をクリックして確認のチェックボックスにチェックを入れると削除できます。

図13.png

おわりに

無料アカウントでもクラスタの作成ができました!
最後までお読みいただき、ありがとうございました。

参考

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