Halaman

Tampilkan postingan dengan label Cassandra. Tampilkan semua postingan
Tampilkan postingan dengan label Cassandra. Tampilkan semua postingan

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!


Cassandra Installation Using Docker On Windows

1. Make sure you have installed Docker Desktop for Windows.

2. Open Windows PowerShell

3. Download Cassandra image from the Docker Hub registry. 
Let’s pull Cassandra 4.01 image by type this command: docker pull cassandra:4.0.1


4. Start Cassandra

To start Cassandra, use docker run command that first creates a writeable container layer over the specified image, and then starts it using the specified command in a new container 

docker run --rm --name fercassandra -dp 9042:9042 cassandra:4.0.1


The --rm flag automatically removes the container (fercassandra) when it exits,
--name flag assign a name to the container
--detach , -d flag run container in background and print container ID
--publish , -p flag publish a container's port(s) to the host

5. Cassandra is running


6. CQL Shell

a.  Click CLI button to open window for interacting with Cassandra using CQL,

 then type this query: 

SELECT cluster_name, listen_address, listen_port, data_center, rpc_address, rpc_port FROM system.local;


b. Use docker exec command to run CQL Shell in a running container, then type the query:

c. Use docker exec command to run CQL shell cqlsh for interacting with Cassandra using CQL (the Cassandra Query Language). It is shipped with every Cassandra package, and can be found in the bin/ directory alongside the cassandra executable.


7.  Show more..
a. Use docker ps command to shows running containers:


b. Use docker port command to list port mappings or a specific mapping for the container:


That's enough for now, hope it helps someone.