Last active
December 18, 2015 14:08
-
-
Save peschkaj/5794713 to your computer and use it in GitHub Desktop.
A test of the Cassandra .NET client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Cassandra.Data.Linq; | |
using Cassandra; | |
namespace CassandraTest | |
{ | |
[AllowFiltering] | |
[Table("sales")] | |
public class SalesOrder { | |
[PartitionKey] | |
[Column("order_number")] | |
public string OrderNumber { get; set; } | |
[ClusteringKey(1)] | |
[Column("customer")] | |
public string Customer { get; set; } | |
[Column("sales_person")] | |
public string SalesPerson { get; set; } | |
[Column("order_date")] | |
public DateTime OrderDate { get; set; } | |
[Column("ship_date")] | |
public DateTime ShipDate { get; set; } | |
[Column("line_items")] | |
public List<LineItem> LineItems = new List<LineItem> (); | |
[Column("shipping_address")] | |
public Address ShippingAddress { get; set; } | |
[Column("billing_address")] | |
public Address BillingAddress { get; set; } | |
} | |
public class LineItem { | |
public int Quantity { get; set; } | |
public string ProductNumber { get; set; } | |
public string ProductDescription { get; set; } | |
public double UnitPrice { get; set; } | |
} | |
public class Address { | |
public string Line1 { get; set; } | |
public string Line2 { get; set; } | |
public string City { get; set ; } | |
public string State { get; set; } | |
public string PostalCode { get; set; } | |
} | |
class MainClass | |
{ | |
public static void Main(string[] args) | |
{ | |
Console.WriteLine("Hello World!"); | |
Cluster cluster = Cluster.Builder().WithConnectionString("Contact Points=localhost;Port=9042").Build(); | |
string keyspaceName = "cassandratest"; | |
using (var session = cluster.Connect()) { | |
try | |
{ | |
session.ChangeKeyspace(keyspaceName); | |
} | |
catch (InvalidQueryException) | |
{ | |
session.CreateKeyspaceIfNotExists(keyspaceName); | |
session.ChangeKeyspace(keyspaceName); | |
} | |
var table = session.GetTable<SalesOrder>(); | |
table.CreateIfNotExists(); | |
{ | |
var batch = session.CreateBatch(); | |
var order = new SalesOrder() { | |
OrderNumber = "OR00012345", | |
Customer = "Jeremiah Peschka", | |
SalesPerson = "The Internet", | |
OrderDate = DateTime.Now, | |
ShipDate = DateTime.Now.AddDays(2), | |
ShippingAddress = new Address() | |
{ | |
Line1 = "742 Evergreen Terrace", | |
City = "Springfield", | |
State = "Yup" | |
}, | |
BillingAddress = new Address() | |
{ | |
Line1 = "1234 Fake Street", | |
City = "Springfield", | |
State = "Yup" | |
}, | |
LineItems = new List<LineItem> () | |
{ | |
new LineItem() { Quantity = 1, ProductNumber = "PN54321", UnitPrice = 43.43 }, | |
new LineItem() { Quantity = 12, ProductNumber = "PN12345", UnitPrice = 5.00 } | |
} | |
}; | |
batch.Append(table.Insert(order)); | |
batch.Execute(); | |
} | |
} | |
Console.WriteLine("done!"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment