Skip to content

Instantly share code, notes, and snippets.

@dlbromen
Forked from peschkaj/test-client.cs
Last active December 18, 2015 16:29
Show Gist options
  • Save dlbromen/5811969 to your computer and use it in GitHub Desktop.
Save dlbromen/5811969 to your computer and use it in GitHub Desktop.
cqlsh:cassandratest> SELECT * FROM "sales";
order_number | customer | billing_address | line_items
| order_date | sales_person | ship_date |
shipping_address
--------------+------------------+----------------------------------------------------------+------------------------------------------------------------------------
--------------------------------------------------------------+------------------------------------------+--------------+------------------------------------------+-
--------------------------------------------------------------
OR00012345 | Jeremiah Peschka | {City: Springfield, Line1: 1234 Fake Street, State: Yup} | [{"Quantity": 1, "ProductNumber": "PN54321", "UnitPrice": 43.43 }, {"Qu
antity": 12, "ProductNumber": "PN12345", "UnitPrice": 5.00 }] | 2013-06-19 00:37:02Central Daylight Time | The Internet | 2013-06-21 00:37:02Central Daylight Time |
{City: Springfield, Line1: 742 Evergreen Terrace, State: Yup}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 DateTimeOffset OrderDate { get; set; }
[Column("ship_date")]
public DateTimeOffset 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; }
[Column("line_items")]
public List<string> LineItems = new List<string>();
[Column("shipping_address")]
public Dictionary<string, string> ShippingAddress { get; set; }
[Column("billing_address")]
public Dictionary<string, string> 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 }
//}
ShippingAddress = new Dictionary<string, string>()
{
{"Line1", "742 Evergreen Terrace"},
{"City", "Springfield"},
{"State", "Yup"}
},
BillingAddress = new Dictionary<string, string>()
{
{"Line1", "1234 Fake Street"},
{"City", "Springfield"},
{"State", "Yup"}
},
LineItems = new List<string>()
{
"{\"Quantity\": 1, \"ProductNumber\": \"PN54321\", \"UnitPrice\": 43.43 }",
"{\"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