Advertisement
Guest User

sample execute_prepared_cql3_query test code

a guest
Feb 13th, 2014
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.38 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.nio.ByteBuffer;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.LinkedList;
  6. import java.util.List;
  7. import java.util.Map;
  8.  
  9. import org.apache.cassandra.auth.IAuthenticator;
  10. import org.apache.cassandra.dht.IPartitioner;
  11. import org.apache.cassandra.thrift.AuthenticationException;
  12. import org.apache.cassandra.thrift.AuthenticationRequest;
  13. import org.apache.cassandra.thrift.AuthorizationException;
  14. import org.apache.cassandra.thrift.Cassandra;
  15. import org.apache.cassandra.thrift.Cassandra.Client;
  16. import org.apache.cassandra.thrift.Compression;
  17. import org.apache.cassandra.thrift.ConsistencyLevel;
  18. import org.apache.cassandra.thrift.CqlPreparedResult;
  19. import org.apache.cassandra.thrift.CqlResult;
  20. import org.apache.cassandra.thrift.CqlRow;
  21. import org.apache.cassandra.thrift.TimedOutException;
  22. import org.apache.cassandra.thrift.UnavailableException;
  23. import org.apache.cassandra.utils.ByteBufferUtil;
  24. import org.apache.cassandra.utils.FBUtilities;
  25. import org.apache.thrift.protocol.TBinaryProtocol;
  26. import org.apache.thrift.transport.TFramedTransport;
  27. import org.apache.thrift.transport.TSocket;
  28. import org.apache.thrift.transport.TTransport;
  29.  
  30. public class TestCqlPaging {
  31.  
  32.     public static void main(String args[]) throws Exception {
  33.         if (args.length != 2) {
  34.             System.out.println("2 arguments are needed");
  35.             System.exit(0);
  36.         }
  37.         String bleft = args[0], bright = args[1];
  38.         String host = "localhost", keyspace = "test";
  39.         int port = 9160;
  40.         Client client = null;
  41.         try {
  42.             TSocket socket = new TSocket(host, port);
  43.             TTransport trans = new TFramedTransport(socket);
  44.             trans.open();
  45.             client = new Cassandra.Client(new TBinaryProtocol(trans));
  46.             client.set_keyspace(keyspace);
  47.         }
  48.         catch(Exception e)
  49.         {
  50.             e.printStackTrace();
  51.         }
  52.  
  53.         Map<String, String> credentials = new HashMap<String, String>(2);
  54.         String username = "admin", password = "xxxx";
  55.         CqlResult cqlResult;
  56.         credentials.put(IAuthenticator.USERNAME_KEY, username);
  57.         credentials.put(IAuthenticator.PASSWORD_KEY, password);
  58.         try {
  59.             client.login(new AuthenticationRequest(credentials));
  60.             System.out.println("connected");
  61.             String query = "SELECT * FROM \"cf1\" WHERE token(\"col1\") > ? AND token(\"col1\") <= ? LIMIT 1000 ALLOW FILTERING";
  62.             IPartitioner<?> partitioner = FBUtilities
  63.                     .newPartitioner("org.apache.cassandra.dht.Murmur3Partitioner");
  64.             List<ByteBuffer> values = new LinkedList<ByteBuffer>();
  65.             values.add(partitioner.getTokenValidator().fromString(bleft));
  66.             values.add(partitioner.getTokenValidator().fromString(bright));
  67.             System.out.println("Trying request :\n" + query
  68.                     + "\n with bindvalues " + bleft + " and " + bright);
  69.             CqlPreparedResult cqlPreparedResult = client
  70.                     .prepare_cql3_query(ByteBufferUtil.bytes(query),
  71.                             Compression.NONE);
  72.             Iterator<CqlRow> rows;
  73.             int retries = 0;
  74.             while (retries < 3) {
  75.                 try {
  76.                     System.out.println("use execute_prepared_cql3_query");
  77.                     cqlResult = client.execute_prepared_cql3_query(
  78.                             cqlPreparedResult.itemId, values,
  79.                             ConsistencyLevel.LOCAL_ONE);
  80.                     if (cqlResult != null && cqlResult.rows != null) {
  81.                         rows = cqlResult.rows.iterator();
  82.                         System.out.println(rows.toString());
  83.                     }
  84.                     return;
  85.                 } catch (TimedOutException e) {
  86.                     retries++;
  87.                     if (retries >= 3) {
  88.                         rows = null;
  89.                         RuntimeException rte = new RuntimeException(
  90.                                 e.getMessage());
  91.                         rte.initCause(e);
  92.                         throw rte;
  93.                     }
  94.                 } catch (UnavailableException e) {
  95.                     retries++;
  96.                     if (retries >= 3) {
  97.                         rows = null;
  98.                         RuntimeException rte = new RuntimeException(
  99.                                 e.getMessage());
  100.                         rte.initCause(e);
  101.                         throw rte;
  102.                     }
  103.                 } catch (Exception e) {
  104.                     rows = null;
  105.                     RuntimeException rte = new RuntimeException(e.getMessage());
  106.                     rte.initCause(e);
  107.                     throw rte;
  108.                 }
  109.             }
  110.         } catch (AuthenticationException e) {
  111.             System.out
  112.                     .println("Authentication exception: invalid username and/or password");
  113.             System.out.println("username = " + username + ", password = "
  114.                     + password);
  115.             throw new IOException(e);
  116.         } catch (AuthorizationException e) {
  117.             System.out
  118.                     .println("AuthorizationException exception on connection");
  119.             throw new AssertionError(e);
  120.         } catch (Exception e) {
  121.             System.out.println("Unknown exception");
  122.             throw new AssertionError(e);
  123.         }
  124.     }
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement