Run Cassandra on a single node on Windows

Source: Internet
Author: User
Tags cassandra

Cassandra can be installed on many systems. I installed it on Windows Server 2008 R2. The installation is quite simple. You just need to extract the downloaded compressed package to a directory, here we will mainly record the user experience:

Cassandra Official Website: http://cassandra.apache.org/, download page http://cassandra.apache.org/download/

Cassandra is developed in Java and requires JVM 1.6 or above. We recommend version 6 Update 23 to download idea from the Java official website:

    • Java_home: C: \ Program Files \ Java \ jre6
    • Cassandra_home: Check that you extract to that location to write that, My is D: \ apache-cassandra-0.7.0-rc4 \

Run Cassandra. bat in the bin of Cassandra to start it, which is probably like this:

On Windows, Cassandra does not know how to set it to run in the Windows service mode, so it opens another command line to operate.

You can use nodetool. BAT to confirm whether Cassandra is running again.

Because there is only one node, you don't need to configure anything. You can simply use the default keyspace. Cassandra provides a tool called Cassandra CLI that can directly input commands, run cassadnra-cli.bat. Let's take a look at this. The Common commands of Cassandra CLI include set get show count, set and get for example, and quit/exit is to leave Cassandra CLI, you can also use help to query available commands, remember to run the cassandra-cli.bat to add a parameter -- host to specify the location of Cassandra node, or else it will play.

Then, we can refer to the example provided in the readme.txt file for testing. Cassandra 0.7.0 RC3 does not have the default keyspace (ex: keyspace1), which must be created before use.The syntax of set is roughly as follows:

 
01. Set keyspace1.standard1 ['geffzhang '] ['blog'] = 'HTTP: // www.dotnetting.cn'

 
02 .\\\\\

 
03. \\\_ key \\_ Value

 
04. \ _ Column

05. \ _ keyspace \ _ column family

 
Add a few pieces of data according to the above syntax, and then use get to pull the data.
 
 

Cassandra is designed to support thrift, which means we can use multiple languages for development. For Cassandra development itself, this is the benefit of using thrift: multi-language support. The downside is that thrift APIs are too simple to be used in a production environment.

Cassandra recommends that youProgramIn C #, such as fluentcassandra or Aquiles. However, you can also use the official minimum-level API-thrift to communicate with Cassandra.

Thrift is the simplest API provided by Cassandra. This file is included in Apache-Cassandra-0.5.1. It can be used directly. We can also install Thrift on our own and then generate it automatically through the Cassandra. Thrift file. If you want to use Cassandra, you must understand thrift API. After all, all other more advanced APIs are packaged based on this.

Step 1: Download Thrift

To thrift download page get the latest stable release, for me, that isThrift-0.5.0.tar.gz(Use for making thrift. dll) andThrift compiler for Windows (thrift-0.5.0.exe)(For making Apache. Cassandra. dll ).

Step 2: gnerate thrift DLL

UnzipThrift-0.5.0.tar.gz(Or new version), findThrift. csproj (or thrift. sln)In \ thrift \ Lib \ CSHARP \ SRC \

In Visual Studio, right click on [thrift]-> [Build], then you will getThrift. dllIn \ bin \ debug \ (if no error)

Step 3: Generate Apache. Cassandra DLL

CopyThrift compiler for Windows (thrift-0.5.0.exe)To Cassandra Folder \ interface \, execute following command
Thrift-0.5.0.exe -- Gen CSHARP Cassandra. Thrift

In principle, it shoshould be generate a new folder called"Gen-CSHARP", But dunno why, I had an error when I running it with version 0.6.8, So I change Cassandra version to 0.7.0 RC3, then it passed.

After get C # source code, using Visual Studio create a newClass libaryProject, then add all files in gen-CSHARP \ apache \ Cassandra \ Folder. Now build the project, maybe you will got 19 ~ 26 error, but don't worry, just some word-casing problem, do some upper case job can fixed it.
(Ex:. Count->. Count)

Step 4: Connecting Cassandra with C #

Add thrift. dll and Apache. Cassandra. DLL to your refence, you can use Windows Forms/console application/Web application/or other, it doesn' t matter.

 
UsingSystem;

UsingSystem. Collections. Generic;

 
UsingSystem. LINQ;

 
UsingSystem. text;

 
UsingApache. Cassandra;

 
UsingThrift. Transport;

 
UsingThrift. Protocol;

 
 

 
NamespaceCassandra_console

 
{

ClassProgram

 
{

 
Static VoidMain (String[] ARGs)

 
{

 
TTransport frametransport =NewTframedtransport (NewTsocket ("Localhost", 9160 ));

 
Tprotocol protocol =NewTbinaryprotocol (transport );

Tprotocol frameprotocol =NewTbinaryprotocol (frametransport );

 
 

 
Cassandra. Client client =NewCassandra. Client (frameprotocol, frameprotocol );

 
 

 
Frametransport. open ();

 
Console. writeline ("Opening connection");

 
 

 
// For 0.7.0, need select keyspace at first

Client. set_keyspace ("Keyspace1");

 
Console. writeline ("Keyspace set to keyspace1");

 
 

 
System. Text. Encoding utf8encoding = system. Text. encoding. utf8;

 
 

 
LongTimestamp = datetime. Now. millisecond;

 
 

 
Columnpath namecolumnpath =NewColumnpath ()

 
{

 
Column_family ="Standard2",

 
Column = utf8encoding. getbytes ("Name")

 
};

 
 

 
Columnparent namecolumnparent =NewColumnparent ()

 
{

 
Column_family ="Standard2"

 
};

 
 

Column namecolume =NewColumn ()

 
{

 
Name = utf8encoding. getbytes ("Name"),

 
Value = utf8encoding. getbytes ("Geff Zhang"),

 
Timestamp = Timestamp

 
};

 
 

 
Console. writeline ("Inserting name columns");

 
 

// Insert data

 
Client. insert (utf8encoding. getbytes ("Geff"),

 
Namecolumnparent,

 
Namecolume,

 
Consistencylevel. One );

 
 

 
// Using the key to get column 'name'

 
Columnorsupercolumn returnedcolumn = client. Get (utf8encoding. getbytes ("Geff"), Namecolumnpath, consistencylevel. One );

 
Console. writeline ("Column data in keyspace1/standard1: Name: {0}, value: {1 }",

 
Utf8encoding. getstring (returnedcolumn. Column. Name ),

 
Utf8encoding. getstring (returnedcolumn. Column. Value ));

 
 

 
Console. writeline ("Closing connection");

 
Frametransport. Close ();

 
 

 
Console. Readline ();

 
 

 
}

 
}

 
}

 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.