Return-Path: Delivered-To: apmail-cassandra-user-archive@www.apache.org Received: (qmail 97751 invoked from network); 2 Sep 2010 07:07:12 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 2 Sep 2010 07:07:12 -0000 Received: (qmail 5680 invoked by uid 500); 2 Sep 2010 07:07:10 -0000 Delivered-To: apmail-cassandra-user-archive@cassandra.apache.org Received: (qmail 5443 invoked by uid 500); 2 Sep 2010 07:07:07 -0000 Mailing-List: contact user-help@cassandra.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@cassandra.apache.org Delivered-To: mailing list user@cassandra.apache.org Received: (qmail 5435 invoked by uid 99); 2 Sep 2010 07:07:06 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 02 Sep 2010 07:07:06 +0000 X-ASF-Spam-Status: No, hits=1.2 required=10.0 tests=HTML_MESSAGE,RCVD_IN_DNSWL_MED,SPF_PASS,URI_HEX X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of justus.thorvaldsson@svenskaspel.se designates 62.20.83.49 as permitted sender) Received: from [62.20.83.49] (HELO piff.svenskaspel.se) (62.20.83.49) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 02 Sep 2010 07:07:02 +0000 Received: from rsbgchs01.ad.spel.se (Not Verified[172.23.1.94]) by piff.svenskaspel.se with MailMarshal (v6,8,2,9371) (using TLS: SSLv23) id ; Thu, 02 Sep 2010 09:05:27 +0200 Received: from RSBGMBS01.ad.spel.se ([172.23.1.91]) by rsbgchs01.ad.spel.se ([172.23.1.94]) with mapi; Thu, 2 Sep 2010 09:06:39 +0200 From: Thorvaldsson Justus To: "user@cassandra.apache.org" CC: "'sabco@live.co.uk'" Date: Thu, 2 Sep 2010 09:06:38 +0200 Subject: Cassandra 0.7 example Thread-Topic: Cassandra 0.7 example Thread-Index: ActKbWLYZmPpOM5hQSW9jHjMb5sJCg== Message-ID: <0ABFAD1D323E3C4DA9A7D542DC99F714AA1C33351F@RSBGMBS01.ad.spel.se> Accept-Language: en-US, sv-SE Content-Language: sv-SE X-MS-Has-Attach: X-MS-TNEF-Correlator: acceptlanguage: en-US, sv-SE Content-Type: multipart/alternative; boundary="_000_0ABFAD1D323E3C4DA9A7D542DC99F714AA1C33351FRSBGMBS01adsp_" MIME-Version: 1.0 --_000_0ABFAD1D323E3C4DA9A7D542DC99F714AA1C33351FRSBGMBS01adsp_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable <<<<<<<<<<< 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 se= tting 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 wh= en 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= =20with some help. The most interesting news I think is the secondary ind= ices. I also post some info a blog on www.justus.st where= =20I format the text a little better than this =3D) 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= =20option in later versions. =20 /** =20 * Open up a new connection to the Cassandra Database. =20 * =20 * @return the Cassandra Client =20 */ =20 private static Cassandra.Client newCassandraConnection(String addre= ss) throws TTransportException { =20 TTransport tr =3D new TSocket(adress, 9160); =20 TFramedTransport tf =3D new TFramedTransport(tr); =20 TProtocol proto =3D new TBinaryProtocol(tf); =20 Cassandra.Client client =3D new Cassandra.Client(proto); =20 tr.open(); =20 return client; =20 } In this part it is possible to set all CF definitions when creating the k= eyspace, although it is not necessary so I did it afterwards. /** =20* @param keyspacename =20* @param replicafactor =20* @throws InvalidRequestException =20* @throws TException =20*/ public static void createKeySpace(String keyspacename, int replicafactor = ) throws InvalidRequestException, TException { =20 Cassandra.Client c =3D Connection.lendConne= ction(); =20 try =20 { =20 c.describe_key= space(keyspacename); =20 System.out.pri= ntln("Keyspace already exists"); =20 } =20 catch (NotFoundException e) =20 { =20 System.out.pri= ntln("Keyspace not found, creating new "+ keyspacename); =20 KsDef k =3D ne= w KsDef(); =20 k.setName(keys= pacename); =20 k.setReplicati= on_factor(replicafactor); =20 k.setStrategy_= class("org.apache.cassandra.locator.RackUnawareStrategy"); =20 List cf= Defs =3D new ArrayList(); =20 k.setCf_defs(c= fDefs); =20 c.system_add_k= eyspace(k); =20 } =20 Connection.ret= urnConnection(c); =20 } /** =20* @param keyspacename =20* @param columnname =20* @param columntype =20* @param comparatortype =20* @param subcomparatortype =20* @param row_cache_size =20* @param key_cache_size =20* @param comment =20* @throws InvalidRequestException =20* @throws TException =20* @throws NumberFormatException =20* @throws IOException =20*/ public static void createColumnFamily(String keyspacename, String columnn= ame, String columntype, String comparatortype, = =20 = =20 = =20 String subcomparatort= ype, double row_cache_size, double key_cache_size, String comment) throws InvalidRequestException, TException, NumberFormatException, IOExce= ption { =20 CfDef cdef =3D new CfDef(); =20 cdef.setColumn_type(columntype); =20 cdef.setComment(comment); =20 cdef.setComparator_type(comparatortype); =20 cdef.setKey_cache_size(key_cache_size); =20 cdef.setRow_cache_size(row_cache_size); =20 cdef.setSubcomparator_type(subcomparatortyp= e); =20 cdef.setKeyspace(keyspacename); =20 cdef.setName(columnname); =20 Cassandra.Client c =3D Connection.lendConne= ction(); =20 c.set_keyspace(keyspacename); =20 c.system_add_column_family(cdef); =20 Connection.returnConnection(c); } /** =20* @param keyspacename =20* @param columnname =20* @param columntype =20* @param comparatortype =20* @param subcomparatortype =20* @param row_cache_size =20* @param key_cache_size =20* @param comment =20* @param listIndexes =20* @throws InvalidRequestException =20* @throws TException =20* @throws NumberFormatException =20* @throws IOException =20*/ public static void createColumnFamilyWithSecondaryIndices(String keyspace= name, String columnname, String columntype, =20 = =20 String comparatortype, String s= ubcomparatortype, double row_cache_size, =20 = =20 double key_cache_size, String c= omment, ArrayList listIndexes ) =20 = =20 throws InvalidRequestException,= =20TException, NumberFormatException, IOException { =20 CfDef cdef =3D new CfDef(); =20 cdef.setColumn_type(columntype); =20 cdef.setComment(comment); =20 cdef.setComparator_type(comparatortype); =20 cdef.setKey_cache_size(key_cache_size); =20 cdef.setRow_cache_size(row_cache_size); =20 cdef.setSubcomparator_type(subcomparatortyp= e); =20 cdef.setKeyspace(keyspacename); =20 cdef.setName(columnname); =20 for(SecondaryIndices si : listIndexes) =20 { =20 cdef.addToColu= mn_metadata(si.getCdef()); =20 } =20 Cassandra.Client c =3D Connection.lendConne= ction(); =20 c.set_keyspace(keyspacename); =20 c.system_add_column_family(cdef); =20 Connection.returnConnection(c); } public class SecondaryIndices { =20 private ColumnDef cdef; =20 /** =20 * @param columnkey =20 * @param indexname =20 */ =20 public SecondaryIndices(byte[] columnkey, S= tring indexname, String validationclass) =20 { =20 cdef =3D new C= olumnDef(); =20 cdef.setIndex_= name(indexname); //borde vara indexnamnet =20 cdef.setName(c= olumnkey);//borde vara columnnamnet =20 cdef.setIndex_= type(IndexType.KEYS); =20 cdef.setValida= tion_class(validationclass); =20 } =20 public ColumnDef getCdef() { =20 return cdef; =20 } } 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(colum= nParent, clause, slicePredicate, consistency); //this is fairly easy to understand but IndexOperator.. I leave that for = now IndexExpression expr =3D new IndexExpression(columnkey, IndexOperator.EQ,= =20columnvalue); //expr made into a fixed list why I do not know, new byte =3D=3D startkey= =20and 100 is count. I dont realy know what parameters do yet IndexClause clause =3D new IndexClause(Arrays.asList(expr), new byte[]{},= =20100); Hope this helps Regards /Justus AB SVENSKA SPEL 106 10 Stockholm Sturegatan 11, Sundbyberg V=E4xel +46 8 757 77 00 http://svenskaspel.se --_000_0ABFAD1D323E3C4DA9A7D542DC99F714AA1C33351FRSBGMBS01adsp_ Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

<<<<<<<&l= t;<<<
Courtney Robinson [sabco@live.co.uk]

Hello everyone,

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

 

can anyone provide a quick jav= a example of connecting to cassandra and setting up a  keyspace and a column f= amily 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.

<<<<<<<<&= lt;< 

 

There is a way to search the emai= l list, and I must add your question is more a user@cassandra than dev.

Here can you search the email lis= t

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= =20I think is the secondary indices.

 

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

 

New about opening the connection,= =20here 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.<= /p>

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

    privat= e static Cassandra.Client newCassandraConnection(String address) throws TTransportException {
            TTransport tr =3D new TSocket(adress, 9160);
            TFramedTransport tf =3D ne= w TFramedTransport(tr);
            TProtocol proto =3D new TBinaryProtocol(tf);
            Cassandra.Client client =3D= =20new Cassandra.Client(proto);
            tr.open();
            return client;
    }

 <= /p>

In this part it is possi= ble to set all CF definitions when creating the keyspace, although it is not nec= essary so I did it afterwards.

/**
 * @param keyspacename
 * @param replicafactor
 * @throws InvalidRequestException
 * @throws TException
 */
public static void createKeySpace(String keyspacename, int replicafactor = ) throws InvalidRequestException, TException
{
            &= nbsp;           &n= bsp;   Cassandra.Client c =3D Connection.lendConnection();
            &= nbsp;           &n= bsp;   try
            &= nbsp;           &n= bsp;   {
            &= nbsp;           &n= bsp;           &nb= sp;           &nbs= p;        c.describe_keyspace(keyspace= name);
            &= nbsp;           &n= bsp;           &nb= sp;           &nbs= p;        System.out.println("Key= space already exists");
            &= nbsp;           &n= bsp;   }
            &= nbsp;           &n= bsp;   catch (NotFoundException e)
            &= nbsp;           &n= bsp;   {
            &= nbsp;           &n= bsp;           &nb= sp;           &nbs= p;        System.out.println("Key= space not found, creating new "+ keyspacename);
            &= nbsp;           &n= bsp;           &nb= sp;           &nbs= p;        KsDef k =3D new KsDef();
            &= nbsp;           &n= bsp;           &nb= sp;           &nbs= p;        k.setName(keyspacename);
=             &= nbsp;           &n= bsp;           &nb= sp;           &nbs= p;        k.setReplication_factor(repl= icafactor);
             =             &= nbsp;           &n= bsp;           &nb= sp;       k.setStrategy_class("or= g.apache.cassandra.locator.RackUnawareStrategy");
             =             &= nbsp;           &n= bsp;           &nb= sp;       List<CfDef> cfDefs =3D new ArrayList<CfDef>();
             =             &= nbsp;           &n= bsp;           &nb= sp;       k.setCf_defs(cfDefs);
             =             &= nbsp;           &n= bsp;           &nb= sp;       c.system_add_keyspace(k);             &= nbsp;           &n= bsp;           &nb= sp;           &nbs= p;        }
            &= nbsp;           &n= bsp;           &nb= sp;           &nbs= p;        Connection.returnConnection(= c);
            &= nbsp;           &n= bsp;   }

/**
 * @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 columnn= ame, String columntype, String comparatortype,      &= nbsp;           &n= bsp;           &nb= sp;           &nbs= p;            = ;            =             &= nbsp;           &n= bsp;           &nb= sp;           &nbs= p;            = ;            =             &= nbsp;           &n= bsp;           &nb= sp;           &nbs= p;            = ;             = ;     String subcomparatortype, double row_cache_size, double key_cache_s= ize, String comment)
throws InvalidRequestException, TException, NumberFormatException, IOExce= ption
{
             =             &= nbsp;  CfDef cdef =3D new CfDef();
            &= nbsp;           &n= bsp;   cdef.setColumn_type(columntype);
            &= nbsp;           &n= bsp;   cdef.setComment(comment);
            &= nbsp;           &n= bsp;   cdef.setComparator_type(comparatortype);
            &= nbsp;           &n= bsp;   cdef.setKey_cache_size(key_cache_size);
            &= nbsp;           &n= bsp;   cdef.setRow_cache_size(row_cache_size);
            &= nbsp;           &n= bsp;   cdef.setSubcomparator_type(subcomparatortype);
            &= nbsp;           &n= bsp;   cdef.setKeyspace(keyspacename);
            &= nbsp;           &n= bsp;   cdef.setName(columnname);
            &= nbsp;           &n= bsp;  
            &= nbsp;           &n= bsp;   Cassandra.Client c =3D Connection.lendConnection();
            &= nbsp;           &n= bsp;   c.set_keyspace(keyspacename);
            &= nbsp;           &n= bsp;  
            &= nbsp;           &n= bsp;   c.system_add_column_family(cdef);
            &= nbsp;           &n= bsp;           &nb= sp;           &nbs= p;       
            &= nbsp;           &n= bsp;   Connection.returnConnection(c);
}
            &= nbsp;           &n= bsp;  
/**
 * @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 keyspace= name, String columnname, String columntype,
            &= nbsp;           &n= bsp;           &nb= sp;           &nbs= p;            &nbs= p;            = ;            =             &= nbsp;            String comparatortype, String subcomparator= type, double row_cache_size,
            &= nbsp;           &n= bsp;           &nb= sp;           &nbs= p;            &nbs= p;            = ;            =             &= nbsp;            double key_cache_size, String comment, ArrayList<SecondaryIndices> listIndexes  )
            &= nbsp;           &n= bsp;           &nb= sp;           &nbs= p;            = ;            =             &= nbsp;           &n= bsp;           &nb= sp; throws InvalidRequestException, TException, NumberFormatException, IOExce= ption
{
            &= nbsp;           &n= bsp;   CfDef cdef =3D new CfDef();
            &= nbsp;           &n= bsp;   cdef.setColumn_type(columntype);
            &= nbsp;           &n= bsp;   cdef.setComment(comment);
            &= nbsp;           &n= bsp;   cdef.setComparator_type(comparatortype);
            &= nbsp;           &n= bsp;   cdef.setKey_cache_size(key_cache_size);
            &= nbsp;           &n= bsp;   cdef.setRow_cache_size(row_cache_size);
            &= nbsp;           &n= bsp;   cdef.setSubcomparator_type(subcomparatortype);
            &= nbsp;           &n= bsp;   cdef.setKeyspace(keyspacename);
            &= nbsp;           &n= bsp;   cdef.setName(columnname);
            &= nbsp;           &n= bsp;           &nb= sp;           &nbs= p;       
            &= nbsp;           &n= bsp;   for(SecondaryIndices si : listIndexes)
            &= nbsp;           &n= bsp;   {
            &= nbsp;           &n= bsp;            &n= bsp;           &nb= sp;       cdef.addToColumn_metadata(si.getC= def());
            &= nbsp;           &n= bsp;   }
            &= nbsp;           &n= bsp;   Cassandra.Client c =3D Connection.lendConnection();
            &= nbsp;           &n= bsp;  
            &= nbsp;           &n= bsp;   c.set_keyspace(keyspacename);
            &= nbsp;           &n= bsp;  
            &= nbsp;           &n= bsp;   c.system_add_column_family(cdef);
            &= nbsp;           &n= bsp;  
            &= nbsp;           &n= bsp;   Connection.returnConnection(c);
}

public class SecondaryInd= ices
{
            &= nbsp;           &n= bsp;   private ColumnDef cdef;
            &= nbsp;           &n= bsp;  
            &= nbsp;           &n= bsp;   /**
            &= nbsp;           &n= bsp;    * @param columnkey
            &= nbsp;           &n= bsp;    * @param indexname
            &= nbsp;           &n= bsp;    */
            &= nbsp;           &n= bsp;   public SecondaryIndices(byte[] columnkey, String indexname, String validationcla= ss)
            &= nbsp;           &n= bsp;   {
            &= nbsp;           &n= bsp;           &nb= sp;           &nbs= p;        cdef =3D new ColumnDef();
            &= nbsp;           &n= bsp;           &nb= sp;           &nbs= p;        cdef.setIndex_name(indexname= ); //borde vara indexnamnet
            &= nbsp;           &n= bsp;           &nb= sp;           &nbs= p;        cdef.setName(columnkey);//bo= rde vara columnnamnet
            &= nbsp;           &n= bsp;           &nb= sp;           &nbs= p;        cdef.setIndex_type(IndexType= .KEYS);
            &= nbsp;           &n= bsp;           &nb= sp;           &nbs= p;        cdef.setValidation_class(val= idationclass);
            &= nbsp;           &n= bsp;   }

    &= nbsp;           &n= bsp;           public ColumnDef getCdef() {
            &= nbsp;           &n= bsp;   return cdef;
            &= nbsp;           &n= bsp;   }
}

I haven’t tested= =20the secondary indices yet but I think it is this way it works to create, to g= et 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 =3D new IndexExpression(columnkey, IndexOperator.EQ,= columnvalue);
//expr made into a fixed list why I do not know, new byte =3D=3D startkey= =20and 100 is count. I dont realy know what parameters do yet
IndexClause clause =3D new IndexClause(Arrays.asList(expr), new byte[]{},= =20100);

Hope this helps

Regards
/Justus

 <= /p>

AB SVENSKA SPEL
106 10 Stockholm
Stu= regatan 11,=20 Sundbyberg
V=E4xel +46 8 757 77 00
http://svenskaspel.se

=20 --_000_0ABFAD1D323E3C4DA9A7D542DC99F714AA1C33351FRSBGMBS01adsp_--