Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.nio.ByteBuffer;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Map;
- import org.apache.cassandra.auth.IAuthenticator;
- import org.apache.cassandra.dht.IPartitioner;
- import org.apache.cassandra.thrift.AuthenticationException;
- import org.apache.cassandra.thrift.AuthenticationRequest;
- import org.apache.cassandra.thrift.AuthorizationException;
- import org.apache.cassandra.thrift.Cassandra;
- import org.apache.cassandra.thrift.Cassandra.Client;
- import org.apache.cassandra.thrift.Compression;
- import org.apache.cassandra.thrift.ConsistencyLevel;
- import org.apache.cassandra.thrift.CqlPreparedResult;
- import org.apache.cassandra.thrift.CqlResult;
- import org.apache.cassandra.thrift.CqlRow;
- import org.apache.cassandra.thrift.TimedOutException;
- import org.apache.cassandra.thrift.UnavailableException;
- import org.apache.cassandra.utils.ByteBufferUtil;
- import org.apache.cassandra.utils.FBUtilities;
- import org.apache.thrift.protocol.TBinaryProtocol;
- import org.apache.thrift.transport.TFramedTransport;
- import org.apache.thrift.transport.TSocket;
- import org.apache.thrift.transport.TTransport;
- public class TestCqlPaging {
- public static void main(String args[]) throws Exception {
- if (args.length != 2) {
- System.out.println("2 arguments are needed");
- System.exit(0);
- }
- String bleft = args[0], bright = args[1];
- String host = "localhost", keyspace = "test";
- int port = 9160;
- Client client = null;
- try {
- TSocket socket = new TSocket(host, port);
- TTransport trans = new TFramedTransport(socket);
- trans.open();
- client = new Cassandra.Client(new TBinaryProtocol(trans));
- client.set_keyspace(keyspace);
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- Map<String, String> credentials = new HashMap<String, String>(2);
- String username = "admin", password = "xxxx";
- CqlResult cqlResult;
- credentials.put(IAuthenticator.USERNAME_KEY, username);
- credentials.put(IAuthenticator.PASSWORD_KEY, password);
- try {
- client.login(new AuthenticationRequest(credentials));
- System.out.println("connected");
- String query = "SELECT * FROM \"cf1\" WHERE token(\"col1\") > ? AND token(\"col1\") <= ? LIMIT 1000 ALLOW FILTERING";
- IPartitioner<?> partitioner = FBUtilities
- .newPartitioner("org.apache.cassandra.dht.Murmur3Partitioner");
- List<ByteBuffer> values = new LinkedList<ByteBuffer>();
- values.add(partitioner.getTokenValidator().fromString(bleft));
- values.add(partitioner.getTokenValidator().fromString(bright));
- System.out.println("Trying request :\n" + query
- + "\n with bindvalues " + bleft + " and " + bright);
- CqlPreparedResult cqlPreparedResult = client
- .prepare_cql3_query(ByteBufferUtil.bytes(query),
- Compression.NONE);
- Iterator<CqlRow> rows;
- int retries = 0;
- while (retries < 3) {
- try {
- System.out.println("use execute_prepared_cql3_query");
- cqlResult = client.execute_prepared_cql3_query(
- cqlPreparedResult.itemId, values,
- ConsistencyLevel.LOCAL_ONE);
- if (cqlResult != null && cqlResult.rows != null) {
- rows = cqlResult.rows.iterator();
- System.out.println(rows.toString());
- }
- return;
- } catch (TimedOutException e) {
- retries++;
- if (retries >= 3) {
- rows = null;
- RuntimeException rte = new RuntimeException(
- e.getMessage());
- rte.initCause(e);
- throw rte;
- }
- } catch (UnavailableException e) {
- retries++;
- if (retries >= 3) {
- rows = null;
- RuntimeException rte = new RuntimeException(
- e.getMessage());
- rte.initCause(e);
- throw rte;
- }
- } catch (Exception e) {
- rows = null;
- RuntimeException rte = new RuntimeException(e.getMessage());
- rte.initCause(e);
- throw rte;
- }
- }
- } catch (AuthenticationException e) {
- System.out
- .println("Authentication exception: invalid username and/or password");
- System.out.println("username = " + username + ", password = "
- + password);
- throw new IOException(e);
- } catch (AuthorizationException e) {
- System.out
- .println("AuthorizationException exception on connection");
- throw new AssertionError(e);
- } catch (Exception e) {
- System.out.println("Unknown exception");
- throw new AssertionError(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement