Exemplo n.º 1
0
import os

url = os.environ["ACCOUNT_URI"]
key = os.environ["ACCOUNT_KEY"]
client = CosmosClient(url, key)
# [END create_client]

# Create a database in the account using the CosmosClient,
# specifying that the operation shouldn't throw an exception
# if a database with the given ID already exists.
# [START create_database]
database_name = 'testDatabase'
try:
    database = client.create_database(id=database_name)
except HTTPFailure:
    database = client.get_database(database=database_name)
# [END create_database]

# Create a container, handling the exception if a container with the
# same ID (name) already exists in the database.
# [START create_container]
container_name = 'products'
try:
    container = database.create_container(id=container_name)
except HTTPFailure as e:
    if e.status_code != 409:
        raise
    container = database.get_container(container_name)
# [END create_container]

# Create a container with custom settings. This example
Exemplo n.º 2
0
    db.create_container(id=test_container_name)
except HTTPFailure as e:
    if e.status_code != 409:
        raise
    db.get_container(test_container_name)

####

# In order to retreive a container where you know the database name and container name:
container = Container(client.client_context,
                      database=test_database_name,
                      id=test_container_name)
container.upsert_item({'id': 'something', 'value': 'else'})

# If you want to walk down the hierarchy, you can also get it from the client->databases->containers
database = client.get_database(test_database_name)
container = database.get_container(test_container_name)
container.upsert_item({'id': 'something', 'value': 'new'})

# Once you have a container, you can query items to your heart's content:
items = list(
    container.query_items(query='SELECT * FROM root r WHERE r.id="something"'))

# You can enumerate the items:
import json
for item in items:
    print(json.dumps(item, indent=True))

# It should be (almost) free to ask the length of a query
# if len(items) > 4711: # TODO: NYI (should use header to get this info to make it O(1))
#    print('Big number')
Exemplo n.º 3
0
client = CosmosClient(url=AUTH_URI, key=AUTH_KEY)


def do_basic_stuff():
    for database in client.list_databases():
        print(database)
        for container in database.list_containers():
            print(container)
            for item in container.list_items():
                print(item)


try:
    database = client.create_database("swaggers")
except HTTPFailure:
    database = client.get_database("swaggers")

try:
    database.delete_container('publicmaster')
except:
    pass

try:
    container = database.get_container(
        "publicmaster")  # TODO: Why not have a fail_if_exists on every create?
except HTTPFailure:  # TODO: What is the appropriate exception here?
    container = database.create_container(
        "publicmaster", partition_key=PartitionKey('/info/version'))

import time