Qtcassandra program 1

Source: Internet
Author: User

/*
* Text:
* Read_write_data.cpp
*
* Description:
* Create a context with a table, then try to read and write data
* The Cassandra cluster.
*
* Documentation:
* Run with no options.
* Fails if the test cannot create the context, create the table,
* Read or write the data.
*
* License:
* Copyright (c) 2011 made to order Software Corp.
*
* Http://snapwebsites.org/
* Contact@m2osw.com
*
* Permission is hereby granted, free of charge, to any person obtaining
* Copy of this software and associated documentation files (
* "Software"), to deal in the software without restriction, including
* Without limitation the rights to use, copy, modify, merge, publish,
* Distriense, sublicense, and/or duplicate copies of the software, and
* Permit persons to whom the software is furnished to do so, subject
* The following conditions:
*
* The above copyright notice and this permission notice shall be supported DED
* In all copies or substantial portions of the software.
*
* The software is provided "as is", without warranty of any kind, express
* Or implied, including but not limited to the warranties
* Merchantability, fitness for a particle purpose and noninfringement.
* In no event shall the authors or copyright holders be liable for any
* Claim, damages or other liability, whether in an action of contract,
* Tort or otherwise, arising from, out of or in connection with
* Software or the use or other dealings in the software.
*/

# Include <qtcassandra/qcassandra. h>
# Include <qtcore/qdebug>
# Include <thrift-gencpp-Cassandra/cassandra_types.h>

Int main (INT argc, char * argv [])
{
Qtcassandra: qcassandra Cassandra; // defines a database.

Cassandra. Connect (); // link to the database
Qdebug () <"working on Cassandra cluster named" <Cassandra. clustername ();
Qdebug () <"working on Cassandra Protocol version" <Cassandra. protocolversion ();

Qsharedpointer <qtcassandra: qcassandracontext> context (Cassandra. Context ("qt_cassandra_test_rw"); // create a keyspace named qt_cassandra_test_rw.
Context-> setstrategyclass ("simplestrategy"); // The default is localstrategy copy placement policy is a simple policy, where a copy is placed in a data center.
// Context-> setdurablewrites (false); // by default this is 'true'
Context-> setreplicationfactor (1); // by default this is undefined replica factor is 1. Data is stored only on the current node and not on other nodes in the cluster.

Qsharedpointer <qtcassandra: qcassandratable> table (context-> table ("qt_cassandra_test_table"); // create a column family named qt_cassandra_test_table
// Table-> setcomment ("our test table."); // comment information, important information in the column family definition, any human-readable comment language in string form
Table-> setcolumntype ("standard"); // standard or super normal columns or super Columns
Table-> setkeyvalidationclass ("bytestype ");
Table-> setdefavalivalidationclass ("bytestype ");
Table-> setcomparatortype ("bytestype"); // sort by bytetype
Table-> setkeycachesaveperiodinseconds (14400 );
Table-> setmemtableflushaftermins (60 );
// Table-> setmemtablethroughputinmb (247 );
// Table-> setmemtableoperationsinmillions (1.1578125 );
// Table-> setgcgraceseconds (864000); // 10 days (default)
Table-> setgcgraceseconds (3600); // 1 h.
Table-> setmincompactionthreshold (4 );
Table-> setmaxcompactionthreshold (22 );
Table-> setreplicateonwrite (1 );

// Column_type: Standard or super columnfamily

// Colck_type: the unique valid value is timestamp.

// Comparator: sort by type

// Subcomparator: the comparator used by the Child column when column is a super Column

// Recociler: name of the Coordinated class when the column version conflicts. Currently, the only valid value is timestamp.

// Rows_cached: number of rows to be cached

// Preload_row_cache: set this option to true to automatically load the row cache.

// Key_cache_size: number of key values in the cache

// Read_repair_chance: valid value range: 0.0-1.0

// Cluster-> keyspace-> columnfamily-> Column

// The corresponding QT library is:

// Cluster-> contexts-> tables-> columndefintions

// Qcassandra-> qcassandracontexts-> qcassandratable-> qcassandrarow-> qcassandracell

// Row is a row of data in the columnfamily, while cell refers to a data. This cell can be the key or value of column.

// Multiple cells constitute the row

Try {
Context-> Create (); // create a keyspace and a column family
Qdebug () <"context and its table were created! ";
}
Catch (Org: Apache: CASSANDRA: invalidrequestexception & E ){
Qdebug () <"exception is [" <E. Why. c_str () <"]";
}

// Try {// by default the rest shocould not generate an exception
// Now that it's created, we can access it with the [] Operator

Qtcassandra: qcassandravalue value1 (-55); // insert a data entry,
Cassandra ["qt_cassandra_test_rw"] ["qt_cassandra_test_table"] [qstring ("http://www.snapwebsites.org/page/3")] [qstring ("size")] = value1;

// Parameter: 1, keyspace 2, columnfamily 3, rowkey 4, columnkey 5, columnvalue
Qtcassandra: qcassandravalue value2 (1000000 );
Cassandra ["qt_cassandra_test_rw"] ["qt_cassandra_test_table"] [qstring ("http://www.snapwebsites.org/page/3")] [qstring ("million")] = value2;

// Binary
Qbytearray bin;
Bin. append (char) 1 );
Bin. append (char) 255 );
Bin. append ('G ');
Bin. append ('I ');
Bin. append ('F ');
Bin. append ('.');
Bin. append (char) 32 );
Bin. append (char) 7 );
Bin. append (char) 0xc0); // 192
// Bin. Clear (); // here you can test that empty values are acceptable by cassandra
Qtcassandra: qcassandravalue value3 (BIN );
Qbytearray row_key;
Row_key.append (char) 0 );
Row_key.append (char) 255 );
Row_key.append (char) 9 );
Row_key.append (char) 25 );
Row_key.append (char) 0 );
Qbytearray column_key;
Column_key.append (char) 0 );
Column_key.append (char) 1 );
Column_key.append (char) 15 );
Column_key.append (char) 0 );
Column_key.append (char) 255 );
Cassandra ["qt_cassandra_test_rw"] ["qt_cassandra_test_table"] [row_key] [column_key] = value3;

// Read this one from the memory cache
Qtcassandra: qcassandravalue V1 = Cassandra ["qt_cassandra_test_rw"] ["qt_cassandra_test_table"] [qstring ("http://www.snapwebsites.org/page/3")] [qstring ("size")];
If (v1.int32value ()! =-55 ){
Qdebug () <"reading the size value failed. Got" <v1.int32value () <"instead of-55 ";
}

// Clear the cache
Cassandra ["qt_cassandra_test_rw"] ["qt_cassandra_test_table"]. Revoke AchE ();
If (Cassandra ["qt_cassandra_test_rw"] ["qt_cassandra_test_table"] [qstring ("http://www.snapwebsites.org/page/3")]. exists (qstring ("size "))){
Qdebug () <"Yeah! Exists (\ "size \") worked! (From Cassandra )";
}
Else {
Qdebug () <"cocould not find \" size \ "which shocould be defined ";
}

// Clear the cache
Cassandra ["qt_cassandra_test_rw"] ["qt_cassandra_test_table"]. Revoke AchE ();
If (Cassandra ["qt_cassandra_test_rw"] ["qt_cassandra_test_table"]. exists (qstring ("http://www.snapwebsites.org/page/3 "))){
Qdebug () <"Yeah! Exists (\ "http://www.snapwebsites.org/page/3\") worked! (From Cassandra )";
}
Else {
Qdebug () <"cocould not find \" http://www.snapwebsites.org/page/3\ "which shocould be defined ";
}

If (Cassandra ["qt_cassandra_test_rw"] ["qt_cassandra_test_table"]. exists (qstring ("unknown row "))){
Qdebug () <"Hmmm... exists (\" unknown row \ ") worked... (from Cassandra )";
}
Else {
Qdebug () <"cocould not find \" unknown row \ "which was expected! ";
}

// Clear the cache
Cassandra ["qt_cassandra_test_rw"] ["qt_cassandra_test_table"]. Revoke AchE ();

// Re-read this one from Cassandra

// Read a piece of data
Qtcassandra: qcassandravalue v1b = Cassandra ["qt_cassandra_test_rw"] ["qt_cassandra_test_table"] [qstring ("http://www.snapwebsites.org/page/3")] [qstring ("size")];

If (v1b. int32value ()! =-55 ){
Qdebug () <"reading the size value failed. Got" <v1b. int32value () <"instead of-55 ";
}

Qtcassandra: qcassandravalue v2 = Cassandra ["qt_cassandra_test_rw"] ["qt_cassandra_test_table"] [qstring ("http://www.snapwebsites.org/page/3")] [qstring ("million")];
If (v2.int32value ()! = 1000000 ){
Qdebug () <"reading the size value failed. Got" <v2.int32value () <"instead of-55 ";
}

If (Cassandra ["qt_cassandra_test_rw"] ["qt_cassandra_test_table"] [qstring ("http://www.snapwebsites.org/page/3")]. exists (qstring ("million "))){
Qdebug () <"Yeah! Exists (\ "million \") worked! (From memory )";
}
Else {
Qdebug () <"cocould not find \" million \ "which shocould be defined ";
}

// Undefined cell...
If (Cassandra ["qt_cassandra_test_rw"] ["qt_cassandra_test_table"] [qstring ("http://www.snapwebsites.org/page/3")]. exists (qstring ("this one "))){
Qdebug () <"somehow \" this one \ "exists! ";
}
Else {
Qdebug () <"cocould not find \" this one \ "as expected ";
}

// Clear the cache, test that we can find all the cells
Cassandra ["qt_cassandra_test_rw"] ["qt_cassandra_test_table"]. Revoke AchE ();
Qtcassandra: qcassandracolumnpredicate column_predicate;
Cassandra ["qt_cassandra_test_rw"] ["qt_cassandra_test_table"] [qstring ("http://www.snapwebsites.org/page/3")]. readcells (column_predicate );

Const qtcassandra: qcassandracells & cells (Cassandra ["qt_cassandra_test_rw"] ["qt_cassandra_test_table"] [qstring ("http://www.snapwebsites.org/page/3")]. cells ());

// Read all the data in the keyspace qt_cassandra_test_rw and the column family qt_cassandra_test_table.
Qdebug () <"cells in 1st row" <cells. Size ();
For (qtcassandra: qcassandracells: const_iterator it = cells. Begin (); it! = Cells. End (); ++ it ){
Qdebug () <"name" <(* It)-> columnname ();
}

Qdebug () <"cellcount ()" <Cassandra ["qt_cassandra_test_rw"] ["qt_cassandra_test_table"] [qstring ("http://www.snapwebsites.org/page/3")]. cellcount ();

// Remove one of the cells
Cassandra ["qt_cassandra_test_rw"] ["qt_cassandra_test_table"] [qstring ("http://www.snapwebsites.org/page/3")]. dropcell (qstring ("million "));

// Clear the cache, test that we can find all the cells
Cassandra ["qt_cassandra_test_rw"] ["qt_cassandra_test_table"]. Revoke AchE ();
Cassandra ["qt_cassandra_test_rw"] ["qt_cassandra_test_table"] [qstring ("http://www.snapwebsites.org/page/3")]. readcells (column_predicate );
Const qtcassandra: qcassandracells & cells2 (Cassandra ["qt_cassandra_test_rw"] ["qt_cassandra_test_table"] [qstring ("http://www.snapwebsites.org/page/3")]. cells ());
Qdebug () <"After remove: Cells in 1st row" <cells. Size ();
For (qtcassandra: qcassandracells: const_iterator it = cells2.begin (); it! = Cells2.end (); ++ it ){
Qdebug () <"name" <(* It)-> columnname ();
}

Qdebug () <"cellcount ()" <Cassandra ["qt_cassandra_test_rw"] ["qt_cassandra_test_table"] [qstring ("http://www.snapwebsites.org/page/3")]. cellcount ();

Cassandra ["example"] ["qt_cassandra_test_table"]. droprow (qstring ("http://www.snapwebsites.org/page/3"), qtcassandra: qcassandravalue: example, qtcassandra: qcassandra: timeofday ()
+ 10000000, qtcassandra: consistency_level_one );
// If (Cassandra ["qt_cassandra_test_rw"] ["qt_cassandra_test_table"]. exists (qstring ("http://www.snapwebsites.org/page/3 "))){
// Qdebug () <"error: dropped Row still exists ...";
//}
// Else {
// Qdebug () <"dropped row does not exist anymore ";
//}

//}
// Catch (Org: Apache: CASSANDRA: invalidrequestexception & E ){
// Qdebug () <"while working: exception is [" <E. Why. c_str () <"]";
//}

// Context-> drop ();
}

// VIM: TS = 4 Sw = 4 et

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.