Esempio 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.
            CustomerEntity customer = new CustomerEntity("Harp", "Walter")
            {
                Email       = "*****@*****.**",
                PhoneNumber = "425-555-0101"
            };

            // 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();
        }
Esempio n. 2
0
        private static async Task CarDataOpertaions(CloudTable table)
        {
            PermissionDetails permissionDetails = new PermissionDetails("Harp", "Avinava")
            {
                Access = "Granted"
            };

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

            Console.WriteLine("Reading the Entity.");
            permissionDetails = await SamplesUtils.RetrievePermissionUsingPointQueryAsync(table, "Harp", "Avinava");

            Console.WriteLine();
        }
        public async Task RunSamples(string tableName)
        {
            Console.WriteLine("Azure Cosmos DB Table - Advanced Samples\n");
            Console.WriteLine();

            tableName += 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();
            }
        }
        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();
            }
        }
Esempio n. 5
0
        private static async Task UserDataOpertaions(CloudTable table)
        {
            User permissionDetails = new User("Avinava", "Internal")
            {
                Value = "Granted"
            };
            User userDetails = new User("Avinava", "External")
            {
                Value = "Avinava Basu"
            };

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

            userDetails = await SamplesUtils.InsertOrMergeUserAsync(table, userDetails);

            Console.WriteLine("Reading the Entity.");
            permissionDetails = await SamplesUtils.RetrieveUserUsingPointQueryAsync(table, "Avinava", "External");

            Console.WriteLine();
        }
        /// <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();
        }
        private static async Task BasicDataOperationsAsync(CloudTable table)
        {
            Console.WriteLine("Enter the count of entries you will make");
            int count = Convert.ToInt32(Console.ReadLine());

            for (int i = 0; i < count; i++)
            {
                Console.WriteLine("Enter details of user " + (i + 1));
                Console.WriteLine();
                Console.WriteLine("Enter your first name");
                string str1 = Console.ReadLine();
                Console.WriteLine("Enter your last name");
                string str2 = Console.ReadLine();
                Console.WriteLine("Enter your email Address");
                string str3 = Console.ReadLine();
                Console.WriteLine("Enter your Phone number");
                string str4 = Console.ReadLine();
                // Create an instance of a customer entity. See the Model\CustomerEntity.cs for a description of the entity.
                CustomerEntity customer = new CustomerEntity(str1, str2)
                {
                    Email       = str3,
                    PhoneNumber = str4
                };

                // Demonstrate how to insert the entity
                Console.WriteLine("Insert an Entity.");
                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 = "9810738851";
            //await SamplesUtils.InsertOrMergeEntityAsync(table, customer);
            //Console.WriteLine();

            // Demonstrate how to Read the updated entity using a point query
            Console.WriteLine("Retrieving data from cloud");
            Console.WriteLine("Enter The first name");
            string strFname = Console.ReadLine();

            Console.WriteLine("Enter your last name");
            string strLname = Console.ReadLine();
            //Console.WriteLine("Reading the updated Entity.");
            await SamplesUtils.RetrieveEntityUsingPointQueryAsync(table, strFname, strLname);

            Console.WriteLine();

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



            //TableQuery<CustomerEntity> query = new TableQuery<CustomerEntity>()
            //    .Where(
            //        TableQuery.CombineFilters(
            //            TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Aman"),
            //            TableOperators.And,
            //            TableQuery.GenerateFilterCondition("Email", QueryComparisons.Equal, "*****@*****.**")
            //    ));

            //await table.ExecuteQuerySegmentedAsync<CustomerEntity>(query, null);
            //Console.WriteLine("AmanPrint");
        }