incubator-cassandra-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thorvaldsson Justus <justus.thorvalds...@svenskaspel.se>
Subject Cassandra 0.7 example
Date Thu, 02 Sep 2010 07:06:38 GMT
<<<<<<<<<<<
Courtney Robinson [sabco@live.co.uk]

Hello everyone,

I'm sorry if that has been asked already, i've just joined the list.



can anyone provide a quick java example of connecting to cassandra and setting up a  keyspace
and a column family using thrift.

I know my way around 0.6 and i'm trying to get ready to migrate to 0.7 when its stable.



Please and thank you.
<<<<<<<<<<

There is a way to search the email list, and I must add your question is more a user@cassandra
than dev.
Here can you search the email list
http://cassandra-user-incubator-apache-org.3065146.n2.nabble.com/

And I have done some experiments with 0.7 and Java so I could provide you with some help.
The most interesting news I think is the secondary indices.

I also post some info a blog on www.justus.st<http://www.justus.st> where I format the
text a little better than this =)

New about opening the connection, here the important thing is to set the framed transport,
this is important and is a change that will be the only option in later versions.

     /**
     * Open up a new connection to the Cassandra Database.
     *
     * @return the Cassandra Client
     */

    private static Cassandra.Client newCassandraConnection(String address) throws TTransportException
{
            TTransport tr = new TSocket(adress, 9160);
            TFramedTransport tf = new TFramedTransport(tr);
            TProtocol proto = new TBinaryProtocol(tf);
            Cassandra.Client client = new Cassandra.Client(proto);
            tr.open();
            return client;
    }



In this part it is possible to set all CF definitions when creating the keyspace, although
it is not necessary so I did it afterwards.

/**
 * @param keyspacename
 * @param replicafactor
 * @throws InvalidRequestException
 * @throws TException
 */
public static void createKeySpace(String keyspacename, int replicafactor ) throws InvalidRequestException,
TException
{
                            Cassandra.Client c = Connection.lendConnection();
                            try
                            {
                                                         c.describe_keyspace(keyspacename);
                                                         System.out.println("Keyspace already
exists");
                            }
                            catch (NotFoundException e)
                            {
                                                         System.out.println("Keyspace not
found, creating new "+ keyspacename);
                                                         KsDef k = new KsDef();
                                                         k.setName(keyspacename);
                                                         k.setReplication_factor(replicafactor);
                                                         k.setStrategy_class("org.apache.cassandra.locator.RackUnawareStrategy");
                                                         List<CfDef> cfDefs = new ArrayList<CfDef>();
                                                         k.setCf_defs(cfDefs);
                                                         c.system_add_keyspace(k);
                                                         }
                                                         Connection.returnConnection(c);
                            }

/**
 * @param keyspacename
 * @param columnname
 * @param columntype
 * @param comparatortype
 * @param subcomparatortype
 * @param row_cache_size
 * @param key_cache_size
 * @param comment
 * @throws InvalidRequestException
 * @throws TException
 * @throws NumberFormatException
 * @throws IOException
 */
public static void createColumnFamily(String keyspacename, String columnname, String columntype,
String comparatortype,                                                                   
                                                                                         
                                                             String subcomparatortype, double
row_cache_size, double key_cache_size, String comment)
throws InvalidRequestException, TException, NumberFormatException, IOException
{
                            CfDef cdef = new CfDef();
                            cdef.setColumn_type(columntype);
                            cdef.setComment(comment);
                            cdef.setComparator_type(comparatortype);
                            cdef.setKey_cache_size(key_cache_size);
                            cdef.setRow_cache_size(row_cache_size);
                            cdef.setSubcomparator_type(subcomparatortype);
                            cdef.setKeyspace(keyspacename);
                            cdef.setName(columnname);

                            Cassandra.Client c = Connection.lendConnection();
                            c.set_keyspace(keyspacename);

                            c.system_add_column_family(cdef);

                            Connection.returnConnection(c);
}

/**
 * @param keyspacename
 * @param columnname
 * @param columntype
 * @param comparatortype
 * @param subcomparatortype
 * @param row_cache_size
 * @param key_cache_size
 * @param comment
 * @param listIndexes
 * @throws InvalidRequestException
 * @throws TException
 * @throws NumberFormatException
 * @throws IOException
 */
public static void createColumnFamilyWithSecondaryIndices(String keyspacename, String columnname,
String columntype,
                                                                                         
                     String comparatortype, String subcomparatortype, double row_cache_size,
                                                                                         
                     double key_cache_size, String comment, ArrayList<SecondaryIndices>
listIndexes  )
                                                                                         
                     throws InvalidRequestException, TException, NumberFormatException, IOException
{
                            CfDef cdef = new CfDef();
                            cdef.setColumn_type(columntype);
                            cdef.setComment(comment);
                            cdef.setComparator_type(comparatortype);
                            cdef.setKey_cache_size(key_cache_size);
                            cdef.setRow_cache_size(row_cache_size);
                            cdef.setSubcomparator_type(subcomparatortype);
                            cdef.setKeyspace(keyspacename);
                            cdef.setName(columnname);

                            for(SecondaryIndices si : listIndexes)
                            {
                                                         cdef.addToColumn_metadata(si.getCdef());
                            }
                            Cassandra.Client c = Connection.lendConnection();

                            c.set_keyspace(keyspacename);

                            c.system_add_column_family(cdef);

                            Connection.returnConnection(c);
}

public class SecondaryIndices
{
                            private ColumnDef cdef;

                            /**
                             * @param columnkey
                             * @param indexname
                             */
                            public SecondaryIndices(byte[] columnkey, String indexname, String
validationclass)
                            {
                                                         cdef = new ColumnDef();
                                                         cdef.setIndex_name(indexname); //borde
vara indexnamnet
                                                         cdef.setName(columnkey);//borde vara
columnnamnet
                                                         cdef.setIndex_type(IndexType.KEYS);
                                                         cdef.setValidation_class(validationclass);
                            }

                            public ColumnDef getCdef() {
                            return cdef;
                            }
}

I haven't tested the secondary indices yet but I think it is this way it works to create,
to get there is a method called get_indexed_slices(columnParent, clause, slicePredicate, consistency);

//this is fairly easy to understand but IndexOperator.. I leave that for now
IndexExpression expr = new IndexExpression(columnkey, IndexOperator.EQ, columnvalue);
//expr made into a fixed list why I do not know, new byte == startkey and 100 is count. I
dont realy know what parameters do yet
IndexClause clause = new IndexClause(Arrays.asList(expr), new byte[]{}, 100);

Hope this helps

Regards
/Justus



AB SVENSKA SPEL
106 10 Stockholm
Sturegatan 11, Sundbyberg
Växel +46 8 757 77 00
http://svenskaspel.se

Mime
View raw message