Configuration and calling of Cassandra, A nosql database of facebookde (Java & C #)

Source: Internet
Author: User
Tags cassandra node server

For more information about the installation, see steps and problems encountered when installing Cassandra on the Debian system of virtualbox virtual machine. Of course, it can also be used in windows, but you need to set the java_home parameter and then start Cassandra. bat in the bin directory. Edit Cassandra. BAT to see

If not defined cassandra_home set cassandra_home = % Cd %

Change

If not defined cassandra_home set cassandra_home = F: \ apache-Cassandra-0.5.1

"F: \ apache-Cassandra-0.5.1" is my installation directory.

 

I. Cassandra single-node Server Configuration

Let's talk about Cassandra configuration first, or Linux. There are three files to be configured. Of course, you can also use the default configuration.

The three files are:

Bin/Cassandra. In. Sh

Conf/log4j. Properties

Conf/storage-conf.xml

Among them, log4j. properties is the log configuration, and the other two are the configured runtime environment.

 

Cassandra. in. sh files do not need to be adjusted. Of course, you didn't use alternatives to adjust the default Java environment, and you need to use jre6.0. In this case, you can set Cassandra. in. sh

# Java_home =/usr/local/jdk6

Is

Java_home =/usr/local/jre6 # Here is the path to decompress your JRE

 

The configuration of log4j. properties is much mentioned on the Internet.

 

Storage-conf.xml configuration is the most important.

The first one is keyspaces. By default, only keyspace1 is set. You can add another keyspaces. This name is required for client calls.

The memory size of the key index set by keyscachedfraction in the keyspace node. The description is also written. If the number of keys is small and the length is long, you can add this value. If it is set to 0, it is disabled.

The next step is to set columnfamily. The configuration name here should also be available during client calls. The column type is also specified.

Replicationfactor sets the number of replicas, which is useful in distributed deployment and maintains data redundancy. As a result, several services are broken down to ensure data integrity.

Commitlogdirectory and the following lines are all set directories.

Seeds is also related to the method of deploying sub-deployed master-slave servers. This article will not describe this.

Thriftaddress is important. This is set for client access, while thriftport is set for access. The following sections are related to performance. Read these instructions carefully. The poor do not have a deep understanding of the settings below.

 

Ii. how to program access cassandra

Slave (Java ). This is a package for accessing Cassandra. Unfortunately, when I use this package for access, it takes 7 to 7 to read the value of a key ~ 8 seconds !!! Fainted. I thought it was the reason for the virtual machine. The result was the same if I deployed it on the other two Linux servers. Of course, these machines and my machines are not in the same network segment. I don't know if this will have a great impact on performance. Later, I put it on my machine and wroteProgramOn the target machine, the read speed is changed to 20 ms. The performance difference is too big. One is that the speed is the same as that of ant financial, and the second time it is on the turtle.

Access packaging for other languages can be found in http://wiki.apache.org/cassandra/ClientExamples. Of course, there is no C.

 

3. Access Cassandra using C # and Java

Cassandra uses another useful thing: thrift. This can be downloaded at http://www.thri?rpc.org.

Specifically in the http://www.thrift-rpc.org /? P = thrift. Git; A = mongolog; H = refs/MISC/instant. Generally, just click the first snapshot. This is the latest one. The version was updated several hours ago.

 

After downloading the package, download it to Linux and decompress it. Go to the directory and install it.

# Chmod + x * // set the execution permission

#./Bootstrap. Sh

#./Configure

# Make

# Make install

After the installation is complete, start the generation operation.

 

Switch to the Interface directory of Cassandra.

Then, use the/home/xieping/thrift/ompiler/CPP/thrift-gen CSHARP Cassandra. Thrift command. After running this command, the Gen-CSHARP directory is added to the Interface directory. Get it to your machine and switch to the/home/xieping/thrift/lib/CSHARP directory. Run the src directory. Open the thrift. csproj file, right-click the thrift project, and set the compilation symbol to net_2_0. Create a new C # project, add the items under the Gen-CSHARP directory, and then reference the thrift project, you can write the followingCodeCall:

 

  Using  System;
Using Thrift. Transport;
Using Thrift. Protocol;
Using Apache. Cassandra;

Namespace Testca {
Class Program {
Static Void Main ( String [] ARGs ){
TTransport = New Tsocket ( " 192.168.93.30 " , 9160 );
Tprotocol = New Tbinaryprotocol (transport );
Cassandra. Client client = New Cassandra. Client (Protocol );

Transport. open ();

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

Long Timestamp = Datetime. Now. millisecond;
Columnpath namecolumnpath = New Columnpath (){
Column_family = " Standard1 " ,
Column = Utf8encoding. getbytes ( " Name " )
};

Client. insert ( " Keyspace1 " ,
" 1 " ,
Namecolumnpath,
Utf8encoding. getbytes ( " Test input 1 " ),
Timestamp,
Consistencylevel. One );

Client. insert ( " Keyspace1 " ,
" 2 " ,
Namecolumnpath,
Utf8encoding. getbytes ( " Test input 2 " ),
Timestamp,
Consistencylevel. One );

Columnorsupercolumn returnedcolumn = Client. Get ( " Keyspace1 " , " 1 " , Namecolumnpath, consistencylevel. One );
Console. writeline ( " Keyspace1/standard1 column value: Key: {0}, value: {1} " ,
Utf8encoding. getstring (returnedcolumn. Column. Name ),
Utf8encoding. getstring (returnedcolumn. Column. Value ));
Transport. Close ();

Console. readkey ();
}

}
}

 

 

 

Java becomes

/Home/xieping/thrift/ompiler/CPP/thrift-gen Java Cassandra. Thrift

 

Java code

 

  Import     Static  Me. prettyprint. Cassandra. utils. stringutils. bytes;

Import Java. Io. unsupportedencodingexception;
Import Org. Apache. Cassandra. Service. Cassandra;
Import Org. Apache. Cassandra. Service. columnorsupercolumn;
Import Org. Apache. Cassandra. Service. columnpath;
Import Org. Apache. Cassandra. Service. consistencylevel;
Import Org. Apache. Cassandra. Service. invalidrequestexception;
Import Org. Apache. Cassandra. Service. notfoundexception;
Import Org. Apache. Cassandra. Service. timedoutexception;
Import Org. Apache. Cassandra. Service. unavailableexception;
Import Org. Apache. Thrift. texception;
Import Org. Apache. Thrift. Protocol. tbinaryprotocol;
Import Org. Apache. Thrift. Protocol. tprotocol;
Import Org. Apache. Thrift. Transport. * ;


Public Class Program {
Public Class S {

}

/**
* @ Param ARGs
* @ Throws Exception
*/
Public Static Void Main (string [] ARGs) Throws Exception {
Long starttime = System. currenttimemillis ();
For ( Int I = 0 ; I < 10000 ; I ++ ){
Run ();
}

Long endtime = System. currenttimemillis ();
System. Out. println ( " Number of current milliseconds when the program runs here " + Starttime );
System. Out. println ( " Total program running " + (Endtime - Starttime) + " Millisecond " );
}

Static Void Run () Throws Invalidrequestexception, unavailableexception, timedoutexception, texception, notfoundexception, unsupportedencodingexception {
TTransport = New Tsocket ( " 192.168.93.30 " , 9160 );
Tprotocol = New Tbinaryprotocol (transport );
Cassandra. Client client = New Cassandra. Client (Protocol );

Transport. open ();
Long Timestamp = System. currenttimemillis ();

Columnpath namecolumnpath = New Columnpath ( " Standard1 " , Null , Bytes ( " Name " ));


Client. insert ( " Keyspace1 " ,
" 1 " ,
Namecolumnpath,
Bytes ( " Test data 1 " ),
Timestamp,
Consistencylevel. One );

Client. insert ( " Keyspace1 " ,
" 2 " ,
Namecolumnpath,
Bytes ( " Test data 2 " ),
Timestamp,
Consistencylevel. One );


Columnorsupercolumn returnedcolumn = Client. Get ( " Keyspace1 " , " 1 " , Namecolumnpath, consistencylevel. One );

System. Out. println (string. Format ( " Key: % s; Value: % s " ,
New String (returnedcolumn. Column. Name ),
New String (returnedcolumn. Column. value, " UTF-8 " )));

Transport. Close ();
}

}

 

 

 

 

Related Article

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.