Azure Cosmos DB - 기본 작업(Python)
목적
Azure SDK for Python를 사용하여 Azure Cosmos DB의 기본 작업 확인
 수수료
 공식
 
 
 
위에서,
처리량은 최소 400RU/s, 스토리지는 100GB로 했을 경우의 1개월(30일)의 요금은 이하
처리량: 0.896 * 4 * 24 * 30 = 2580 (엔)
스토리지: 28 * 100 = 2800(엔)
합계: 5380(엔)
 무료 테두리
 공식
 
Azure Cosmos DB 계정을 만들 때 무료 수준 할인을 적용하여,
첫 번째 400 RU/秒 및 5GB 스토리지를 무료로 사용할 수 있습니다.
하지만 무료 수준 할인을 적용할 수 있는 Azure Cosmos DB 계정은 Azure 구독당 하나씩
 
계정에 데이터베이스를 만들 때 최소 400RU/s의 처리량 설정이 필요하므로,
여러 데이터베이스를 만들면 두 번째 이후의 데이터베이스는 무료 범위를 벗어납니다.
 기본 조작
 전제
 공식
문서를 참조하여 Azure Cosmos DB 계정, 데이터베이스 및 컨테이너가 생성됨
이하 2개의 아이템을 컨테이너에 등록됨
{
  "id": "AndersenFamily",
  "category": "category1",
  "lastName": "Andersen",
  "address": {
      "state": "WA",
      "county": "King",
      "city": "Seattle"
      },
  "creationDate": 1431620472,
  "isRegistered": true
}
{
  "id": "WakefieldFamily",
  "category": "category1",
  "lastName": "Wakefield",
  "address": {
      "state": "NY",
      "county": "Manhattan",
      "city": "NY"
      },
  "creationDate": 1431610472,
  "isRegistered": false
}
 구현 코드
Azure Cosmos DB 인스턴스를 엔드포인트 URL+ 키 또는 연결 문자열을 사용하여 생성
DB 조작으로서 아이템 추가, 모든 아이템 리드, 1 아이템 리드, 쿼리, 아이템 삭제를 실시
from azure.cosmos import exceptions, CosmosClient, PartitionKey
endpoint = ''
key = ''
connectionString = ''
def main():
#  client = CosmosClient(endpoint, key)
  client = CosmosClient.from_connection_string(connectionString)
  databaseName = 'tasks'
  containerName = 'items'
  db = client.get_database_client(databaseName)
  container = db.get_container_client(containerName)
  newItem = {
    "id": "MillerFamily",
    "category": "category1",
    "lastName": "Miller",
    "address": {
        "state": "WA",
        "county": "King",
        "city": "Seattle"
        },
    "creationDate": 1431600472,
    "isRegistered": True
  }
  item = container.create_item(newItem)
  print(' --- create_item --- ')
  print(item)
  print()
  items = container.read_all_items(max_item_count=5)
  print(' --- read_all_items --- ')
  print(list(items))
  print()
  item = container.read_item('MillerFamily', 'category1')
  print(' --- read_item --- ')
  print(item)
  print()
  # クエリ条件: id = 'WakefieldFamily'
 # 取得プロパティ: 全て
  query = "SELECT * FROM items i WHERE i.id = 'WakefieldFamily'"
#  items = container.query_items(query, partition_key='category1')
  items = container.query_items(query, enable_cross_partition_query=True)
  print(' --- query_items 1 --- ')
  print(list(items))
  print()
  # クエリ条件: lastName = 'Andersen'
 # 取得プロパティ: 全て
  query = "SELECT * FROM items i WHERE i.lastName = 'Andersen'"
  items = container.query_items(query, enable_cross_partition_query=True)
  print(' --- query_items 2 --- ')
  print(list(items))
  print()
  # クエリ条件: address.state = 'WA'
 # 取得プロパティ: 全て
  query = "SELECT * FROM items i WHERE i.address.state = 'WA'"
  items = container.query_items(query, enable_cross_partition_query=True)
  print(' --- query_items 3 --- ')
  print(list(items))
  print()
  # クエリ条件: id = 'WakefieldFamily'
 # 取得プロパティ: address.state、address.city
  query = "SELECT i.address.state, i.address.city FROM items i WHERE i.id = 'WakefieldFamily'"
  items = container.query_items(query, enable_cross_partition_query=True)
  print(' --- query_items 4 --- ')
  print(list(items))
  print()
  container.delete_item('MillerFamily', 'category1')
main()
이하, 실행 결과
※Azure Cosmos DB에서 자동으로 추가되는 속성은 출력 내용에서 삭제
실행 결과 --- create_item ---
{'id': 'MillerFamily', 'category': 'category1', 'lastName': 'Miller', 'address': {'state': 'WA', 'county': 'King', 'city': 'Seattle'}, 'creationDate': 1431600472, 'isRegistered': True}
 --- read_all_items ---
[{'id': 'AndersenFamily', 'category': 'category1', 'lastName': 'Andersen', 'address': {'state': 'WA', 'county': 'King', 'city': 'Seattle'}, 'creationDate': 1431620472, 'isRegistered': True}, {'id': 'WakefieldFamily', 'category': 'category1', 'lastName': 'Wakefield', 'address': {'state': 'NY', 'county': 'Manhattan', 'city': 'NY'}, 'creationDate': 1431610472, 'isRegistered': False}, {'id': 'MillerFamily', 'category': 'category1', 'lastName': 'Miller', 'address': {'state': 'WA', 'county': 'King', 'city': 'Seattle'}, 'creationDate': 1431600472, 'isRegistered': True}]
 --- read_item ---
{'id': 'MillerFamily', 'category': 'category1', 'lastName': 'Miller', 'address': {'state': 'WA', 'county': 'King', 'city': 'Seattle'}, 'creationDate': 1431600472, 'isRegistered': True}
 --- query_items 1 ---
[{'id': 'WakefieldFamily', 'category': 'category1', 'lastName': 'Wakefield', 'address': {'state': 'NY', 'county': 'Manhattan', 'city': 'NY'}, 'creationDate': 1431610472, 'isRegistered': False}]
 --- query_items 2 ---
[{'id': 'AndersenFamily', 'category': 'category1', 'lastName': 'Andersen', 'address': {'state': 'WA', 'county': 'King', 'city': 'Seattle'}, 'creationDate': 1431620472, 'isRegistered': True}]
 --- query_items 3 ---
[{'id': 'AndersenFamily', 'category': 'category1', 'lastName': 'Andersen', 'address': {'state': 'WA', 'county': 'King', 'city': 'Seattle'}, 'creationDate': 1431620472, 'isRegistered': True}, {'id': 'MillerFamily', 'category': 'category1', 'lastName': 'Miller', 'address': {'state': 'WA', 'county': 'King', 'city': 'Seattle'}, 'creationDate': 1431600472, 'isRegistered': True}]
 --- query_items 4 ---
[{'state': 'NY', 'city': 'NY'}]
 사용 클래스
공식



위에서,
처리량은 최소 400RU/s, 스토리지는 100GB로 했을 경우의 1개월(30일)의 요금은 이하
처리량: 0.896 * 4 * 24 * 30 = 2580 (엔)
스토리지: 28 * 100 = 2800(엔)
합계: 5380(엔)
무료 테두리
 공식
 
Azure Cosmos DB 계정을 만들 때 무료 수준 할인을 적용하여,
첫 번째 400 RU/秒 및 5GB 스토리지를 무료로 사용할 수 있습니다.
하지만 무료 수준 할인을 적용할 수 있는 Azure Cosmos DB 계정은 Azure 구독당 하나씩
 
계정에 데이터베이스를 만들 때 최소 400RU/s의 처리량 설정이 필요하므로,
여러 데이터베이스를 만들면 두 번째 이후의 데이터베이스는 무료 범위를 벗어납니다.
 기본 조작
 전제
 공식
문서를 참조하여 Azure Cosmos DB 계정, 데이터베이스 및 컨테이너가 생성됨
이하 2개의 아이템을 컨테이너에 등록됨
{
  "id": "AndersenFamily",
  "category": "category1",
  "lastName": "Andersen",
  "address": {
      "state": "WA",
      "county": "King",
      "city": "Seattle"
      },
  "creationDate": 1431620472,
  "isRegistered": true
}
{
  "id": "WakefieldFamily",
  "category": "category1",
  "lastName": "Wakefield",
  "address": {
      "state": "NY",
      "county": "Manhattan",
      "city": "NY"
      },
  "creationDate": 1431610472,
  "isRegistered": false
}
 구현 코드
Azure Cosmos DB 인스턴스를 엔드포인트 URL+ 키 또는 연결 문자열을 사용하여 생성
DB 조작으로서 아이템 추가, 모든 아이템 리드, 1 아이템 리드, 쿼리, 아이템 삭제를 실시
from azure.cosmos import exceptions, CosmosClient, PartitionKey
endpoint = ''
key = ''
connectionString = ''
def main():
#  client = CosmosClient(endpoint, key)
  client = CosmosClient.from_connection_string(connectionString)
  databaseName = 'tasks'
  containerName = 'items'
  db = client.get_database_client(databaseName)
  container = db.get_container_client(containerName)
  newItem = {
    "id": "MillerFamily",
    "category": "category1",
    "lastName": "Miller",
    "address": {
        "state": "WA",
        "county": "King",
        "city": "Seattle"
        },
    "creationDate": 1431600472,
    "isRegistered": True
  }
  item = container.create_item(newItem)
  print(' --- create_item --- ')
  print(item)
  print()
  items = container.read_all_items(max_item_count=5)
  print(' --- read_all_items --- ')
  print(list(items))
  print()
  item = container.read_item('MillerFamily', 'category1')
  print(' --- read_item --- ')
  print(item)
  print()
  # クエリ条件: id = 'WakefieldFamily'
 # 取得プロパティ: 全て
  query = "SELECT * FROM items i WHERE i.id = 'WakefieldFamily'"
#  items = container.query_items(query, partition_key='category1')
  items = container.query_items(query, enable_cross_partition_query=True)
  print(' --- query_items 1 --- ')
  print(list(items))
  print()
  # クエリ条件: lastName = 'Andersen'
 # 取得プロパティ: 全て
  query = "SELECT * FROM items i WHERE i.lastName = 'Andersen'"
  items = container.query_items(query, enable_cross_partition_query=True)
  print(' --- query_items 2 --- ')
  print(list(items))
  print()
  # クエリ条件: address.state = 'WA'
 # 取得プロパティ: 全て
  query = "SELECT * FROM items i WHERE i.address.state = 'WA'"
  items = container.query_items(query, enable_cross_partition_query=True)
  print(' --- query_items 3 --- ')
  print(list(items))
  print()
  # クエリ条件: id = 'WakefieldFamily'
 # 取得プロパティ: address.state、address.city
  query = "SELECT i.address.state, i.address.city FROM items i WHERE i.id = 'WakefieldFamily'"
  items = container.query_items(query, enable_cross_partition_query=True)
  print(' --- query_items 4 --- ')
  print(list(items))
  print()
  container.delete_item('MillerFamily', 'category1')
main()
이하, 실행 결과
※Azure Cosmos DB에서 자동으로 추가되는 속성은 출력 내용에서 삭제
실행 결과 --- create_item ---
{'id': 'MillerFamily', 'category': 'category1', 'lastName': 'Miller', 'address': {'state': 'WA', 'county': 'King', 'city': 'Seattle'}, 'creationDate': 1431600472, 'isRegistered': True}
 --- read_all_items ---
[{'id': 'AndersenFamily', 'category': 'category1', 'lastName': 'Andersen', 'address': {'state': 'WA', 'county': 'King', 'city': 'Seattle'}, 'creationDate': 1431620472, 'isRegistered': True}, {'id': 'WakefieldFamily', 'category': 'category1', 'lastName': 'Wakefield', 'address': {'state': 'NY', 'county': 'Manhattan', 'city': 'NY'}, 'creationDate': 1431610472, 'isRegistered': False}, {'id': 'MillerFamily', 'category': 'category1', 'lastName': 'Miller', 'address': {'state': 'WA', 'county': 'King', 'city': 'Seattle'}, 'creationDate': 1431600472, 'isRegistered': True}]
 --- read_item ---
{'id': 'MillerFamily', 'category': 'category1', 'lastName': 'Miller', 'address': {'state': 'WA', 'county': 'King', 'city': 'Seattle'}, 'creationDate': 1431600472, 'isRegistered': True}
 --- query_items 1 ---
[{'id': 'WakefieldFamily', 'category': 'category1', 'lastName': 'Wakefield', 'address': {'state': 'NY', 'county': 'Manhattan', 'city': 'NY'}, 'creationDate': 1431610472, 'isRegistered': False}]
 --- query_items 2 ---
[{'id': 'AndersenFamily', 'category': 'category1', 'lastName': 'Andersen', 'address': {'state': 'WA', 'county': 'King', 'city': 'Seattle'}, 'creationDate': 1431620472, 'isRegistered': True}]
 --- query_items 3 ---
[{'id': 'AndersenFamily', 'category': 'category1', 'lastName': 'Andersen', 'address': {'state': 'WA', 'county': 'King', 'city': 'Seattle'}, 'creationDate': 1431620472, 'isRegistered': True}, {'id': 'MillerFamily', 'category': 'category1', 'lastName': 'Miller', 'address': {'state': 'WA', 'county': 'King', 'city': 'Seattle'}, 'creationDate': 1431600472, 'isRegistered': True}]
 --- query_items 4 ---
[{'state': 'NY', 'city': 'NY'}]
 사용 클래스
전제
공식
문서를 참조하여 Azure Cosmos DB 계정, 데이터베이스 및 컨테이너가 생성됨
이하 2개의 아이템을 컨테이너에 등록됨
{
  "id": "AndersenFamily",
  "category": "category1",
  "lastName": "Andersen",
  "address": {
      "state": "WA",
      "county": "King",
      "city": "Seattle"
      },
  "creationDate": 1431620472,
  "isRegistered": true
}
{
  "id": "WakefieldFamily",
  "category": "category1",
  "lastName": "Wakefield",
  "address": {
      "state": "NY",
      "county": "Manhattan",
      "city": "NY"
      },
  "creationDate": 1431610472,
  "isRegistered": false
}
구현 코드
Azure Cosmos DB 인스턴스를 엔드포인트 URL+ 키 또는 연결 문자열을 사용하여 생성
DB 조작으로서 아이템 추가, 모든 아이템 리드, 1 아이템 리드, 쿼리, 아이템 삭제를 실시
from azure.cosmos import exceptions, CosmosClient, PartitionKey
endpoint = ''
key = ''
connectionString = ''
def main():
#  client = CosmosClient(endpoint, key)
  client = CosmosClient.from_connection_string(connectionString)
  databaseName = 'tasks'
  containerName = 'items'
  db = client.get_database_client(databaseName)
  container = db.get_container_client(containerName)
  newItem = {
    "id": "MillerFamily",
    "category": "category1",
    "lastName": "Miller",
    "address": {
        "state": "WA",
        "county": "King",
        "city": "Seattle"
        },
    "creationDate": 1431600472,
    "isRegistered": True
  }
  item = container.create_item(newItem)
  print(' --- create_item --- ')
  print(item)
  print()
  items = container.read_all_items(max_item_count=5)
  print(' --- read_all_items --- ')
  print(list(items))
  print()
  item = container.read_item('MillerFamily', 'category1')
  print(' --- read_item --- ')
  print(item)
  print()
  # クエリ条件: id = 'WakefieldFamily'
 # 取得プロパティ: 全て
  query = "SELECT * FROM items i WHERE i.id = 'WakefieldFamily'"
#  items = container.query_items(query, partition_key='category1')
  items = container.query_items(query, enable_cross_partition_query=True)
  print(' --- query_items 1 --- ')
  print(list(items))
  print()
  # クエリ条件: lastName = 'Andersen'
 # 取得プロパティ: 全て
  query = "SELECT * FROM items i WHERE i.lastName = 'Andersen'"
  items = container.query_items(query, enable_cross_partition_query=True)
  print(' --- query_items 2 --- ')
  print(list(items))
  print()
  # クエリ条件: address.state = 'WA'
 # 取得プロパティ: 全て
  query = "SELECT * FROM items i WHERE i.address.state = 'WA'"
  items = container.query_items(query, enable_cross_partition_query=True)
  print(' --- query_items 3 --- ')
  print(list(items))
  print()
  # クエリ条件: id = 'WakefieldFamily'
 # 取得プロパティ: address.state、address.city
  query = "SELECT i.address.state, i.address.city FROM items i WHERE i.id = 'WakefieldFamily'"
  items = container.query_items(query, enable_cross_partition_query=True)
  print(' --- query_items 4 --- ')
  print(list(items))
  print()
  container.delete_item('MillerFamily', 'category1')
main()
이하, 실행 결과
※Azure Cosmos DB에서 자동으로 추가되는 속성은 출력 내용에서 삭제
실행 결과
 --- create_item ---
{'id': 'MillerFamily', 'category': 'category1', 'lastName': 'Miller', 'address': {'state': 'WA', 'county': 'King', 'city': 'Seattle'}, 'creationDate': 1431600472, 'isRegistered': True}
 --- read_all_items ---
[{'id': 'AndersenFamily', 'category': 'category1', 'lastName': 'Andersen', 'address': {'state': 'WA', 'county': 'King', 'city': 'Seattle'}, 'creationDate': 1431620472, 'isRegistered': True}, {'id': 'WakefieldFamily', 'category': 'category1', 'lastName': 'Wakefield', 'address': {'state': 'NY', 'county': 'Manhattan', 'city': 'NY'}, 'creationDate': 1431610472, 'isRegistered': False}, {'id': 'MillerFamily', 'category': 'category1', 'lastName': 'Miller', 'address': {'state': 'WA', 'county': 'King', 'city': 'Seattle'}, 'creationDate': 1431600472, 'isRegistered': True}]
 --- read_item ---
{'id': 'MillerFamily', 'category': 'category1', 'lastName': 'Miller', 'address': {'state': 'WA', 'county': 'King', 'city': 'Seattle'}, 'creationDate': 1431600472, 'isRegistered': True}
 --- query_items 1 ---
[{'id': 'WakefieldFamily', 'category': 'category1', 'lastName': 'Wakefield', 'address': {'state': 'NY', 'county': 'Manhattan', 'city': 'NY'}, 'creationDate': 1431610472, 'isRegistered': False}]
 --- query_items 2 ---
[{'id': 'AndersenFamily', 'category': 'category1', 'lastName': 'Andersen', 'address': {'state': 'WA', 'county': 'King', 'city': 'Seattle'}, 'creationDate': 1431620472, 'isRegistered': True}]
 --- query_items 3 ---
[{'id': 'AndersenFamily', 'category': 'category1', 'lastName': 'Andersen', 'address': {'state': 'WA', 'county': 'King', 'city': 'Seattle'}, 'creationDate': 1431620472, 'isRegistered': True}, {'id': 'MillerFamily', 'category': 'category1', 'lastName': 'Miller', 'address': {'state': 'WA', 'county': 'King', 'city': 'Seattle'}, 'creationDate': 1431600472, 'isRegistered': True}]
 --- query_items 4 ---
[{'state': 'NY', 'city': 'NY'}]
사용 클래스
색인 정보
Azure Cosmos DB 인덱싱 - 개요
모든 항목의 모든 속성에 자동으로 색인이 생성되므로 기본적으로 색인을 만들 필요가 없습니다.
Azure Cosmos DB에서 JSON 사용
중첩된 속성을 포함하여 모든 속성에서 쿼리 가능
Reference
이 문제에 관하여(Azure Cosmos DB - 기본 작업(Python)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/takmot/items/f8cfe499b9b98bec4031텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)