private static OrderSummary CreateOrderSummary(Customer customer, IEnumerable <Order> orders) { var orderSummary = new OrderSummary(customerId: customer.Id); foreach (Order order in orders) { orderSummary.AddSummaryItem(new OrderSummaryItem(order)); } return(orderSummary); }
static void Main(string[] args) { Console.WriteLine("Creating Data"); Customer customer = CreateCustomer(); IEnumerable <Order> orders = CreateOrders(customer); OrderSummary orderSummary = CreateOrderSummary(customer, orders); Console.WriteLine("Starting Client"); using (IRiakEndPoint endpoint = RiakCluster.FromConfig("riakConfig")) { IRiakClient client = endpoint.CreateClient(); Console.WriteLine("Storing Data"); client.Put(ToRiakObject(customer)); foreach (Order order in orders) { // NB: this adds secondary index data as well client.Put(ToRiakObject(order)); } client.Put(ToRiakObject(orderSummary)); Console.WriteLine("Fetching related data by shared key"); string key = "1"; var result = client.Get(customersBucketName, key); CheckResult(result); Console.WriteLine("Customer 1: {0}\n", GetValueAsString(result)); result = client.Get(orderSummariesBucketName, key); CheckResult(result); Console.WriteLine("OrderSummary 1: {0}\n", GetValueAsString(result)); Console.WriteLine("Index Queries"); // Query for order keys where the SalesPersonId index is set to 9000 var riakIndexId = new RiakIndexId(ordersBucketName, ordersSalesPersonIdIndexName); RiakResult <RiakIndexResult> indexRiakResult = client.GetSecondaryIndex(riakIndexId, 9000); // NB: *must* use 9000 as integer here. CheckResult(indexRiakResult); RiakIndexResult indexResult = indexRiakResult.Value; Console.WriteLine("Jane's orders (key values): {0}", string.Join(", ", indexResult.IndexKeyTerms.Select(ikt => ikt.Key))); // Query for orders where the OrderDate index is between 2013-10-01 and 2013-10-31 riakIndexId = new RiakIndexId(ordersBucketName, ordersOrderDateIndexName); indexRiakResult = client.GetSecondaryIndex(riakIndexId, "2013-10-01", "2013-10-31"); // NB: *must* use strings here. CheckResult(indexRiakResult); indexResult = indexRiakResult.Value; Console.WriteLine("October orders (key values): {0}", string.Join(", ", indexResult.IndexKeyTerms.Select(ikt => ikt.Key))); } }
private static RiakObject ToRiakObject(OrderSummary orderSummary) { var orderSummaryRiakObjectId = new RiakObjectId(orderSummariesBucketName, orderSummary.CustomerId.ToString()); return(new RiakObject(orderSummaryRiakObjectId, orderSummary)); }