Cassandra study Note 3

Source: Internet
Author: User
Tags cassandra

Here we start to use Java to operate the Cassandra database, not limited to client operations. For operations on the client Cassandra-CLI and nodetool, you can use the help command on the command line to get a lot of help.

I am using thrift to operate databases. This is a relatively low-level method that uses the most basic API directly. Although there are more advanced methods to operate, it is still necessary to be familiar with the API, so we should first use thrift.

First, import the package in Java, right-click the project name in myeclipse, select Properties, go to Java build path-> libraries, select Add external jars, and import:

Apache-cassandra-1.0.8.jar

Apache-cassandra-thrift-1.0.8.jar

Libthrift-0.6.jar

Log4j-1.2.16.jar

Slf4j-api-1.6.1.jar

Slf4j-log4j12-1.6.1.jar

These jar packages are all in the Lib folder under the canssandra directory.

The following is the test code:

Package Cassandra; import Java. io. unsupportedencodingexception; import Java. NIO. bytebuffer; import Java. util. list; import Org. apache. cassandra. thrift. cassandra; import Org. apache. cassandra. thrift. column; import Org. apache. cassandra. thrift. columnorsupercolumn; import Org. apache. cassandra. thrift. columnparent; import Org. apache. cassandra. thrift. columnpath; import Org. apache. cassandra. thrift. consistencylevel; Im Port Org. apache. cassandra. thrift. invalidrequestexception; import Org. apache. cassandra. thrift. notfoundexception; import Org. apache. cassandra. thrift. slicepredicate; import Org. apache. cassandra. thrift. slicerange; import Org. apache. cassandra. thrift. tbinaryprotocol; import Org. apache. cassandra. thrift. timedoutexception; import Org. apache. cassandra. thrift. unavailableexception; import Org. apache. thrift. texceptio N; import Org. apache. thrift. protocol. tprotocol; import Org. apache. thrift. transport. tframedtransport; import Org. apache. thrift. transport. tsocket; import Org. apache. thrift. transport. tTransport; public class testclient {public static void main (string [] ARGs) throws texception, invalidrequestexception, unavailableexception, expiration, notfoundexception, timedoutexception {// etttransp packaged Ort tr = new tframedtransport (New tsocket ("127.0.0.1", 9160); tprotocol proto = new tbinaryprotocol (TR); Cassandra. client client = new Cassandra. client (PROTO); tr. open (); If (! Tr. isopen () {system. Out. println ("failed to connect server! "); Return;} Long temp = system. currenttimemillis (); client. set_keyspace ("Demo"); // use demo keyspacecolumnparent parent = new columnparent ("student "); // column family/** here we insert 1 million pieces of data into student * each piece of data includes ID and name */string key_user_id = "A"; for (INT I = 0; I <1000000; I ++) {string K = key_user_id + I; // keylong timestamp = system. currenttimemillis (); // time stamp column idcolumn = new column (tobytebuffer ("ID"); // column nameidcolumn. setvalue (tobytebuffer (I + ""); // column valueidcolumn. settimestamp (timestamp); client. insert (tobytebuffer (K), parent, idcolumn, consistencylevel. one); column namecolumn = new column (tobytebuffer ("name"); namecolumn. setvalue (tobytebuffer ("student" + I); namecolumn. settimestamp (timestamp); client. insert (tobytebuffer (K), parent, namecolumn, consistencylevel. one);}/** read a single field of a data entry */columnpath Path = new columnpath ("student"); // set the data path for reading student. setcolumn (tobytebuffer ("ID"); // read the idstring key3 = "A1"; // read the system record whose key is A1. out. println (tostring (client. get (tobytebuffer (key3), path, consistencylevel. one ). column. value);/** read the entire data */slicepredicate predicate = new slicepredicate (); slicerange = new slicerange (tobytebuffer (""), tobytebuffer (""), false, 10); predicate. setslice_range (slicerange); List <columnorsupercolumn> Results = client. get_slice (tobytebuffer (key3), parent, predicate, consistencylevel. one); For (columnorsupercolumn result: Results) {Column column = result. column; system. out. println (tostring (column. name) + "->" + tostring (column. value);} Long temp2 = system. currenttimemillis (); system. out. println ("time:" + (temp2-Temp) + "Ms"); // time-consuming output tr. close ();}/** converts string to bytebuffer to insert Cassandra */public static bytebuffer tobytebuffer (string value) throws unsupportedencodingexception {return bytebuffer. wrap (value. getbytes ("UTF-8");}/** convert bytebuffer to string */public static string tostring (bytebuffer buffer) throws unsupportedencodingexception {byte [] bytes = new byte [buffer. remaining ()]; buffer. get (bytes); return new string (bytes, "UTF-8 ");}}

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.