Halaman

Senin, 18 Oktober 2021

Cassandra with C#

1. Using Microsoft Visual Studio, Create New Project for Console Application   


2. Configure the project 


3. My target Framework using NET 5.0 


4. Add Client Driver for Cassandra, click Tools menu ->NuGet Package Manager-> Manage NuGet Packages for Solution, then type CassandraCSharpDriver


so CassandraCSharpDriver package get installed



5. Create program 

using System;
using System.Collections;
using System.Collections.Generic;
using Cassandra;
using Cassandra.Mapping;

namespace Cassandra_con
{

    public class KeyspacesMain
    {
        public string Keyspace_name { get; set; }
        public bool Durable_writes { get; set; }
        public SortedDictionary<string string=""> Replication { get; set; }
    }

    class Program
    {
       const string MyDC = "datacenter1";
       const string MyIP = "localhost";
       const int MyPortNo = 9042;
       const string MyUID = "username";
       const string MyPass = "password";
       
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Cassandra!");

            var cluster = Cluster.Builder()
                .AddContactPoints(MyIP)
                .WithPort(MyPortNo)
//              .WithLoadBalancingPolicy(new DCAwareRoundRobinPolicy(MyDC))
//              .WithAuthProvider(new PlainTextAuthProvider(MyUID, MyPass))
                .Build();

            var session = cluster.Connect();
            Console.WriteLine("Connected to cluster: " + cluster.Metadata.ClusterName);

            IMapper mapper = new Mapper(session);
            IEnumerable<keyspacesmain> datax = mapper.Fetch<keyspacesmain>("SELECT keyspace_name, durable_writes, replication FROM system_schema.keyspaces");
            Console.WriteLine("Keyspace Name      Durable Writes  Replication Factor"); 

            foreach (var row in datax)
            {
                Console.Write("{0}", row.Keyspace_name.PadRight(19));
                Console.Write("{0}     ", row.Durable_writes);
                
SortedDictionary<string string=""> obj = (SortedDictionary<string string="">)row.Replication;
                
                IDictionaryEnumerator dictEnum = obj.GetEnumerator();
                int spaces = 12;
                while (dictEnum.MoveNext())
                {
                    Console.WriteLine("Key= ".PadLeft(spaces) + dictEnum.Key + ", Value= " + dictEnum.Value);
                    spaces += 28;
                }
            }
        }
     }
 }

5. Output

Hello Cassandra!
Connected to cluster: Test Cluster
Keyspace Name      Durable Writes  Replication Factor
system_auth          True                     Key= class, Value= org.apache.cassandra.locator.SimpleStrategy
                                                          Key= replication_factor, Value= 1
system_schema     True                    Key= class, Value= org.apache.cassandra.locator.LocalStrategy
system_distributed True                    Key= class, Value= org.apache.cassandra.locator.SimpleStrategy
                                                          Key= replication_factor, Value= 3
system                    True                    Key= class, Value= org.apache.cassandra.locator.LocalStrategy
system_traces        True                     Key= class, Value= org.apache.cassandra.locator.SimpleStrategy
                                                          Key= replication_factor, Value= 2

So simple!


Tidak ada komentar:

Posting Komentar