Exemplo n.º 1
0
        private static async Task BasicDataOperationsAsync(CloudTable table)
        {
            // Create an instance of a customer entity. See the Model\CustomerEntity.cs for a description of the entity.

            for (int i = 0; i < 10; i++)
            {
                CustomerEntity customer = Common.customers[i];

                // Demonstrate how to insert the entity
                Console.WriteLine("Insert an Entity.");
                customer = await SamplesUtils.InsertOrMergeEntityAsync(table, customer);

                // Demonstrate how to Update the entity by changing the phone number
                Console.WriteLine("Update an existing Entity using the InsertOrMerge Upsert Operation.");
                customer.PhoneNumber = "425-555-0105";
                await SamplesUtils.InsertOrMergeEntityAsync(table, customer);

                Console.WriteLine();

                // Demonstrate how to Read the updated entity using a point query
                Console.WriteLine("Reading the updated Entity.");
                customer = await SamplesUtils.RetrieveEntityUsingPointQueryAsync(table, "Harp", "Walter");

                Console.WriteLine();

                // Demonstrate how to Delete an entity
                Console.WriteLine("Delete the entity. ");
                await SamplesUtils.DeleteEntityAsync(table, customer);

                Console.WriteLine();
            }
        }
Exemplo n.º 2
0
        public async Task RunSamples()
        {
            Console.WriteLine("Azure Cosmos DB Table - Advanced Samples\n");
            Console.WriteLine();

            string tableName = "demo" + Guid.NewGuid().ToString().Substring(0, 5);

            // Create or reference an existing table
            CloudTable table = await Common.CreateTableAsync(tableName);

            CloudTableClient tableClient = table.ServiceClient;

            try
            {
                // Demonstrate advanced functionality such as batch operations and segmented multi-entity queries
                await AdvancedDataOperationsAsync(table);

                // List tables in the account
                await TableListingOperations(tableClient);

                if (!SamplesUtils.IsAzureCosmosdbTable())
                {
                    // Create a SAS and try CRUD operations with the SAS.
                    await AdvancedDataOperationsWithSasAsync(table);

                    // Service Properties
                    await ServicePropertiesSample(tableClient);

                    // CORS
                    await CorsSample(tableClient);

                    // Service Stats
                    await ServiceStatsSample(tableClient);

                    // Table Acl
                    await TableAclSample(table);

                    // Create a SAS and try CRUD operations with the SAS and shared access policy on the table.
                    await AdvancedDataOperationsWithSasAndSharedAccessPolicyOnTableAsync(table);
                }
            }
            finally
            {
                // Delete the table
                await table.DeleteIfExistsAsync();
            }
        }
Exemplo n.º 3
0
        private static async Task AdvancedDataOperationsAsync(CloudTable table)
        {
            // Demonstrate upsert and batch table operations
            Console.WriteLine("Inserting a batch of entities. ");
            await BatchInsertOfCustomerEntitiesAsync(table, "Smith");

            Console.WriteLine();

            // Query a range of data within a partition using a simple query
            Console.WriteLine("Retrieving entities with surname of Smith and first names >= 1 and <= 75");
            ExecuteSimpleQuery(table, "Smith", "0001", "0075");
            Console.WriteLine();

            // Query the same range of data within a partition and return result segments of 50 entities at a time
            Console.WriteLine("Retrieving entities with surname of Smith and first names >= 1 and <= 75");
            await PartitionRangeQueryAsync(table, "Smith", "0001", "0075");

            Console.WriteLine();

            // Query for all the data within a partition
            Console.WriteLine("Retrieve entities with surname of Smith.");
            await PartitionScanAsync(table, "Smith");

            Console.WriteLine();

            if (SamplesUtils.IsAzureCosmosdbTable())
            {
                // Demonstrate upsert and batch table operations
                Console.WriteLine("Inserting a batch of entities. ");
                await BatchInsertOfCustomerEntitiesAsync(table, "Dave");

                Console.WriteLine();

                // Demonstrate upsert and batch table operations
                Console.WriteLine("Inserting a batch of entities. ");
                await BatchInsertOfCustomerEntitiesAsync(table, "Shirly");

                Console.WriteLine();

                //Query for all the data cross partition with order by
                Console.WriteLine("Query with order by cross partition");
                await ExecuteCrossPartitionQueryWithOrderBy(table, "0001", "0025");

                Console.WriteLine();
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Tests a table SAS to determine which operations it allows.
        /// </summary>
        /// <param name="sasUri">A string containing a URI with a SAS appended.</param>
        /// <param name="customer">The customer entity.</param>
        /// <returns>A Task object</returns>
        private static async Task TestTableSAS(string sasUri, CustomerEntity customer)
        {
            // Try performing table operations with the SAS provided.
            // Note that the storage account credentials are not required here; the SAS provides the necessary
            // authentication information on the URI.

            // Return a reference to the table using the SAS URI.
            CloudTable table = new CloudTable(new Uri(sasUri));

            // Upsert (add/update) operations: insert an entity.
            // This operation requires both add and update permissions on the SAS.
            try
            {
                // Insert the new entity.
                customer = await SamplesUtils.InsertOrMergeEntityAsync(table, customer);

                Console.WriteLine("Add operation succeeded for SAS {0}", sasUri);
                Console.WriteLine();
            }
            catch (StorageException e)
            {
                if (e.RequestInformation.HttpStatusCode == 403)
                {
                    Console.WriteLine("Add operation failed for SAS {0}", sasUri);
                    Console.WriteLine("Additional error information: " + e.Message);
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine(e.Message);
                    Console.ReadLine();
                    throw;
                }
            }

            // Read operation: query an entity.
            // This operation requires read permissions on the SAS.
            CustomerEntity customerRead = null;

            try
            {
                TableOperation retrieveOperation = TableOperation.Retrieve <CustomerEntity>(customer.PartitionKey, customer.RowKey);
                TableResult    result            = await table.ExecuteAsync(retrieveOperation);

                customerRead = result.Result as CustomerEntity;
                if (customerRead != null)
                {
                    Console.WriteLine("\t{0}\t{1}\t{2}\t{3}", customerRead.PartitionKey, customerRead.RowKey, customerRead.Email, customerRead.PhoneNumber);
                }

                Console.WriteLine("Read operation succeeded for SAS {0}", sasUri);
                Console.WriteLine();
            }
            catch (StorageException e)
            {
                if (e.RequestInformation.HttpStatusCode == 403)
                {
                    Console.WriteLine("Read operation failed for SAS {0}", sasUri);
                    Console.WriteLine("Additional error information: " + e.Message);
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine(e.Message);
                    Console.ReadLine();
                    throw;
                }
            }

            // Delete operation: delete an entity.
            try
            {
                if (customerRead != null)
                {
                    await SamplesUtils.DeleteEntityAsync(table, customerRead);
                }

                Console.WriteLine("Delete operation succeeded for SAS {0}", sasUri);
                Console.WriteLine();
            }
            catch (StorageException e)
            {
                if (e.RequestInformation.HttpStatusCode == 403)
                {
                    Console.WriteLine("Delete operation failed for SAS {0}", sasUri);
                    Console.WriteLine("Additional error information: " + e.Message);
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine(e.Message);
                    Console.ReadLine();
                    throw;
                }
            }

            Console.WriteLine();
        }