Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Threading.Tasks;
using System.Configuration;
using System.Collections.Generic;
using System.Net;
using Microsoft.Azure.Cosmos;
using cosmos;
using System.Timers;
public class Program
{
public static readonly string EndpointUri = "";
public static readonly string PrimaryKey = "";
public static CosmosClient cosmosClient;
public static Database database;
public static Container container;
static async Task Main(string[] args)
{
await GetStartedDemoAsync();
await QueryItemsAsync();
// insert test data with a timer.
//while(true)
//{
// await AddItemsToContainerAsync();
// Thread.Sleep(5000);
//}
}
private static async Task GetStartedDemoAsync()
{
var options = new CosmosClientOptions() { ConnectionMode = ConnectionMode.Gateway };
cosmosClient = new CosmosClient(EndpointUri, PrimaryKey,options);
database = cosmosClient.GetDatabase("Data");
container = database.GetContainer("Locations");
}
private static async Task AddItemsToContainerAsync()
{
cosmos.Location loc = new cosmos.Location
{
Latitude = 12.222222,
Longtitude = 15.555555,
Id = Guid.NewGuid().ToString()
};
ItemResponse<Location> loc_response = await container.CreateItemAsync<Location>( loc, new PartitionKey(loc.Id ));
Console.WriteLine("Created item in database with id: {0} Operation consumed {1} RUs.\n", loc_response.Resource, loc_response.RequestCharge);
}
private static async Task QueryItemsAsync()
{
var sqlQueryText = "SELECT * FROM Items";
QueryDefinition queryDefinition = new QueryDefinition(sqlQueryText);
FeedIterator<Location> queryResultSetIterator = container.GetItemQueryIterator<Location>(queryDefinition);
List<Location> mylist = new List<Location>();
while (queryResultSetIterator.HasMoreResults)
{
FeedResponse<Location> currentResultSet = await queryResultSetIterator.ReadNextAsync();
foreach (Location x in currentResultSet)
{
mylist.Add(x);
Console.WriteLine("\tRead {0}\n", x);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment