Skip to content

Instantly share code, notes, and snippets.

@fcojperez
Created September 30, 2015 07:54
Show Gist options
  • Save fcojperez/a0ce13bb83b1d62a851f to your computer and use it in GitHub Desktop.
Save fcojperez/a0ce13bb83b1d62a851f to your computer and use it in GitHub Desktop.
A proof of concept about how to connect to Cassandra with C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cassandra;
/*
* 30/09/2015
*
* Prueba de concepto sobre como concertarse a Cassandra con C#, a continuación más información
*
* This is a proof of concept about how to connect to Cassandra with C#
* This are prerequisites you need to this proof:
* * A Cassandra local installation on Windows, more information here: http://www.datastax.com/2012/01/getting-started-with-apache-cassandra-on-windows-the-easy-way
* * Create table users executing this sentence:
* * * create keyspace hr with replication={'class':'SimpleStrategy', 'replication_factor':1};
* * * use hr;
* * * create table users
... (lastname varchar primary key,
... age int,
... city varchar,
... email varchar,
... firstname varchar);
* * A Cassandra Driver for C#, more information here: https://academy.datastax.com/demos/getting-started-apache-cassandra-and-c-net
*
*
* Francisco Pérez, [email protected]
*
*/
namespace CassandraGettingStarted
{
class CassandraGettingStarted
{
ISession session;
static void Main(string[] args)
{
CassandraGettingStarted prg = new CassandraGettingStarted();
//Creating BobJones
prg.createBobJones();
//Listing users Table
prg.listUsersTable();
//Update users Table
Console.WriteLine("Introduce el nombre de una ciudad para el usuario Perez");
string city = Console.ReadLine();
prg.updateCityByLastName("Perez", city);
}
public CassandraGettingStarted()
{
this.setSession();
}
private void setSession()
{
//Connect to the demo keyspace on our cluster running at 127.0.0.1
Cluster cluster = Cluster.Builder().AddContactPoint("127.0.0.1").Build();
this.session = cluster.Connect("hr");
}
public void createBobJones()
{
Console.WriteLine("##############################");
Console.WriteLine("# CREATE USER BOB JONES #");
Console.WriteLine("##############################");
//Isert Bob
this.session.Execute("insert into users (lastname, age, city, email, firstname) values('Jones', 35, 'Austin', '[email protected]', 'Bob')");
// Read Bob's information back and print to the console
Row result = session.Execute("select * from users where lastname='Jones'").First();
Console.WriteLine("{0} {1}", result["firstname"], result["age"]);
//Wait for enter key before existing
Console.ReadLine();
}
public void listUsersTable()
{
Console.WriteLine("##############################");
Console.WriteLine("# LIST USERS TABLE #");
Console.WriteLine("##############################");
this.session.Execute("select * from users;");
RowSet result = this.session.Execute("select * from users;");
Console.WriteLine("firstname\tAge\tcity");
foreach (Row r in result.GetRows())
{
Console.WriteLine("{0}\t{1}\t{2}", r["firstname"], r["age"], r["city"]);
}
}
public void updateCityByLastName(string lastnameKey, string city)
{
Console.WriteLine("#########################################");
Console.WriteLine("# UPDATE AND LIST USERS TABLE #");
Console.WriteLine("#########################################");
string sqlString = "update users set city = '" + city + "' where lastname = '" + lastnameKey + "'; ";
Console.WriteLine("Query: " + sqlString);
session.Execute(sqlString);
this.listUsersTable();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment