Configuring Multi Node Cassandra Cluster on Windows Machine.

Hi All, These days we hear a lot of buzz in and around the IT industry regarding the BigData and NoSql. Here in this post i wanted to show you how to setup multi node cluster on single machine(Windows).

In Windows machine please navigate to “C:\Windows\System32\drivers\etc” and edit the “hosts.etc” file and add the following content as below.

#cassandra cluster nodes
127.0.0.1 127.0.0.2 
127.0.0.1 127.0.0.3 
127.0.0.1 127.0.0.4 
127.0.0.1 127.0.0.5 
127.0.0.1 127.0.0.6 

once we add the cassandra nodes to hosts.etc file, please download the latest version of Apache Cassandra from the following sitedownload cassandra once downloaded please extract that to one of the folders as “appache-cassandra-2.1.6-node1″(similarly, extract 3 different copies of the same zip file to 3 different folders).

once you are ready with Cassandra folders now its time to modify the cassandra config file located in /conf/cassandra.yaml file.
please edit the following values in the config file

cluster_name: 'DemoCluster'

Provide the seed nodes for each cassandra.yaml file as 127.0.0.1 and 127.0.0.2 etc.;

seed_provider: # Addresses of hosts that are deemed contact points. 
# Cassandra nodes use this list of hosts to find each other and learn 
# the topology of the ring. You must change this if you are running 
# multiple nodes! - class_name: org.apache.cassandra.locator.SimpleSeedProvider parameters: 
# seeds is actually a comma-delimited list of addresses. 
# Ex: "<ip1>,<ip2>,<ip3>" 
- seeds: "127.0.0.1,127.0.0.2"

Note: You need to ensure that the following values are changed for each node
directories where Cassandra should store data on disk

data_file_directories: - /training/node1/var/lib/cassandra/data 

# commit log 
commitlog_directory: /training/node1/var/lib/cassandra/commitlog 

# saved caches 
saved_caches_directory: /training/node1/var/lib/cassandra/saved_caches

make sure you also change the follwing listen address and rpc address for every node of cassandra.

listen_address: 127.0.0.1 
rpc_address: 127.0.0.1

finally your configuration should look as
Node 1 = 127.0.0.1
Node 2 = 127.0.0.2
Node 3 = 127.0.0.3
Node 4 = 127.0.0.4
Node 5 = 127.0.0.5

Thats it, we have configured the multi node cassandra cluster and its ready to run.
Please post me if you face any problem in the configuration and setup related issues.

Happy Configuring Cassandra. 🙂

Advertisements

Using Apache Cassandra with Maven

Hi guys, from last few days i was working on a new technology called Apache Cassandra and here is my guidance to install and use it.
Apache Cassandra is an open source distributed database management system. It is an Apache Software Foundation top-level project, designed to handle very large amounts of data spread out across many commodity servers while providing a highly available service with no single point of failure. It is a NoSQL solution that was initially developed by Facebook
Cassandra provides a structured key-value store with tunable consistency.Keys map to multiple values, which are grouped into column families. The column families are fixed when a Cassandra database is created, but columns can be added to a family at any time. Furthermore, columns are added only to specified keys, so different keys can have different numbers of columns in any given family. The values from a column family for each key are stored together.

You can download the Apache Cassandra here and select one of the mirror link. After downloading extract the zip in one of the drives and create “\var\lib\cassandra\commitlog”,”\var\lib\cassandra\data”,”\var\lib\cassandra\saved_caches” folders in the root folder as these folders are used by cassandra server these folders are configures in conf/cassandra.yaml file.

once you are done with the creation navigate to bin dir and start cassandra.bat file. once it has started without error or exception then start cassandra-cli.bat(cassandra-client)

once the above steps are done correctly, Open Eclipse IDE with Maven capability(Assuming maven is already installed)
copy the below content in the POM.xml file

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.spark.cassandra</groupId>
	<artifactId>CassandraMavenIntegration</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>CassandraMavenIntegration</name>
	<description>CassandraMavenIntegration</description>

	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>me.prettyprint</groupId>
			<artifactId>hector-core</artifactId>
			<version>1.0-2</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.6.1</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>exec-maven-plugin</artifactId>
				<executions>
					<execution>
						<goals>
							<goal>java</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
					<optimize>true</optimize>
					<showDeprecation>true</showDeprecation>
					<showWarnings>true</showWarnings>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>cassandra-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

Now to create a column family and insert data into it using java, follow the code as below.

/**
 * 
 */
package com.spark.cassandra.hector;

import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.beans.HColumn;
import me.prettyprint.hector.api.exceptions.HectorException;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.mutation.Mutator;
import me.prettyprint.hector.api.query.ColumnQuery;
import me.prettyprint.hector.api.query.QueryResult;

/**
 * @author Mantha Pavan Kumar
 * 
 */
public class CassandraHectorTest {

	private static StringSerializer stringSerializer = StringSerializer.get();

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		Cluster cluster = HFactory.getOrCreateCluster("TestCluster",
				"localhost:9160");

		Keyspace keyspaceOperator = HFactory.createKeyspace("DEMO",
				cluster);
		try {
			Mutator<String> mutator = HFactory.createMutator(keyspaceOperator,
					StringSerializer.get());
			mutator.insert("jsmith", "users",
					HFactory.createStringColumn("first", "John"));

			ColumnQuery<String, String, String> columnQuery = HFactory
					.createStringColumnQuery(keyspaceOperator);
			columnQuery.setColumnFamily("users").setKey("jsmith")
					.setName("first");
			QueryResult<HColumn<String, String>> result = columnQuery.execute();

			System.out.println("Read HColumn from cassandra: " + result.get());
			System.out
					.println("Verify on CLI with: get DEMO.users['jsmith'] ");

		} catch (HectorException e) {
			e.printStackTrace();
		}
		cluster.getConnectionManager().shutdown();
	}

}

Happy Coding 🙂