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

ちゃちゃっとcosmosDBのコンテナの中を抽出する

Posted at

はじめに

アイテム登録した後にちゃんと取れたかを確認する術を忘れてたのでこちらもちゃちゃっとつくる

仕上がりはこちら

import argparse
import csv
import json
from azure.cosmos import CosmosClient

# パラメータの取得
parser = argparse.ArgumentParser(description="Cosmos DB からアイテムを取得するスクリプト")
parser.add_argument("-u", "--url", type=str, help="Cosmos DB アカウントURL")
parser.add_argument("-k", "--key", type=str, help="Cosmos DB アカウントキー")
parser.add_argument("-q", "--query", type=str, help="Cosmos DB クエリ(省略可)")

args = parser.parse_args()

# Cosmos DB への接続
cosmos_url = args.url
cosmos_key = args.key
cosmos_query = args.query
client = CosmosClient(cosmos_url, cosmos_key)

# デフォルトクエリの設定
if not cosmos_query:
    cosmos_query = "SELECT * FROM c"

# データベースとコンテナの取得
database_name = "mediator"
container_name = "chat_history"
database = client.get_database_client(database_name)
container = database.get_container_client(container_name)

# アイテムの取得と出力
print(cosmos_query)

for item in container.query_items(query=cosmos_query,enable_cross_partition_query=True):
    print(json.dumps(item, indent=4))

print("アイテムの取得が完了しました。")

実行結果

コマンド実行

とれましたー
image.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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?