use of org.apache.thrift.transport.TFramedTransport in project eiger by wlloyd.
the class CliMain method connect.
/**
* Establish a thrift connection to cassandra instance
*
* @param server - hostname or IP of the server
* @param port - Thrift port number
*/
public static void connect(String server, int port) {
TSocket socket = new TSocket(server, port);
if (transport != null)
transport.close();
if (sessionState.framed) {
transport = new TFramedTransport(socket);
} else {
transport = socket;
}
TBinaryProtocol binaryProtocol = new TBinaryProtocol(transport, true, true);
Cassandra.Client cassandraClient = new Cassandra.Client(binaryProtocol);
try {
transport.open();
} catch (Exception e) {
e.printStackTrace(sessionState.err);
String error = (e.getCause() == null) ? e.getMessage() : e.getCause().getMessage();
throw new RuntimeException("Exception connecting to " + server + "/" + port + ". Reason: " + error + ".");
}
thriftClient = cassandraClient;
cliClient = new CliClient(sessionState, thriftClient);
if ((sessionState.username != null) && (sessionState.password != null)) {
// Authenticate
Map<String, String> credentials = new HashMap<String, String>();
credentials.put(IAuthenticator.USERNAME_KEY, sessionState.username);
credentials.put(IAuthenticator.PASSWORD_KEY, sessionState.password);
AuthenticationRequest authRequest = new AuthenticationRequest(credentials);
try {
thriftClient.login(authRequest, LamportClock.sendTimestamp());
cliClient.setUsername(sessionState.username);
} catch (AuthenticationException e) {
thriftClient = null;
sessionState.err.println("Exception during authentication to the cassandra node, " + "Verify the keyspace exists, and that you are using the correct credentials.");
return;
} catch (AuthorizationException e) {
thriftClient = null;
sessionState.err.println("You are not authorized to use keyspace: " + sessionState.keyspace);
return;
} catch (TException e) {
thriftClient = null;
sessionState.err.println("Login failure. Did you specify 'keyspace', 'username' and 'password'?");
return;
}
}
if (sessionState.keyspace != null) {
try {
sessionState.keyspace = CliCompiler.getKeySpace(sessionState.keyspace, thriftClient.describe_keyspaces());
;
thriftClient.set_keyspace(sessionState.keyspace, LamportClock.sendTimestamp());
cliClient.setKeySpace(sessionState.keyspace);
updateCompletor(CliUtils.getCfNamesByKeySpace(cliClient.getKSMetaData(sessionState.keyspace)));
} catch (InvalidRequestException e) {
sessionState.err.println("Keyspace " + sessionState.keyspace + " not found");
return;
} catch (TException e) {
sessionState.err.println("Did you specify 'keyspace'?");
return;
} catch (NotFoundException e) {
sessionState.err.println("Keyspace " + sessionState.keyspace + " not found");
return;
}
}
// Lookup the cluster name, this is to make it clear which cluster the user is connected to
String clusterName;
try {
clusterName = thriftClient.describe_cluster_name();
} catch (Exception e) {
sessionState.err.println("Exception retrieving information about the cassandra node, check you have connected to the thrift port.");
e.printStackTrace(sessionState.err);
return;
}
sessionState.out.printf("Connected to: \"%s\" on %s/%d%n", clusterName, server, port);
}
use of org.apache.thrift.transport.TFramedTransport in project eiger by wlloyd.
the class Session method getClient.
/**
* Thrift client connection
* @param setKeyspace - should we set keyspace for client or not
* @return cassandra client connection
*/
public Cassandra.Client getClient(boolean setKeyspace) {
// random node selection for fake load balancing
String currentNode = nodes[Stress.randomizer.nextInt(nodes.length)];
TSocket socket = new TSocket(currentNode, port);
TTransport transport = (isUnframed()) ? socket : new TFramedTransport(socket);
Cassandra.Client client = new Cassandra.Client(new TBinaryProtocol(transport));
try {
transport.open();
if (setKeyspace) {
client.set_keyspace("Keyspace1", LamportClock.sendTimestamp());
}
} catch (InvalidRequestException e) {
throw new RuntimeException(e.getWhy());
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
return client;
}
use of org.apache.thrift.transport.TFramedTransport in project brisk by riptano.
the class CassandraStorage method createConnection.
private static Cassandra.Client createConnection(String host, Integer port, boolean framed) throws IOException {
TSocket socket = new TSocket(host, port);
TTransport trans = framed ? new TFramedTransport(socket) : socket;
try {
trans.open();
} catch (TTransportException e) {
throw new IOException("unable to connect to server", e);
}
return new Cassandra.Client(new TBinaryProtocol(trans));
}
use of org.apache.thrift.transport.TFramedTransport in project brisk by riptano.
the class Session method getClient.
/**
* Thrift client connection
* @param setKeyspace - should we set keyspace for client or not
* @return cassandra client connection
*/
public Cassandra.Client getClient(boolean setKeyspace) {
// random node selection for fake load balancing
String currentNode = nodes[Pricer.randomizer.nextInt(nodes.length)];
TSocket socket = new TSocket(currentNode, port);
TTransport transport = (isUnframed()) ? socket : new TFramedTransport(socket);
Cassandra.Client client = new Cassandra.Client(new TBinaryProtocol(transport));
try {
transport.open();
if (setKeyspace) {
client.set_keyspace("PortfolioDemo");
}
} catch (InvalidRequestException e) {
throw new RuntimeException(e.getWhy());
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
return client;
}
use of org.apache.thrift.transport.TFramedTransport in project titan by thinkaurelius.
the class CTConnectionFactory method makeRawConnection.
/**
* Create a Cassandra-Thrift connection, but do not attempt to
* set a keyspace on the connection.
*
* @return A CTConnection ready to talk to a Cassandra cluster
* @throws TTransportException on any Thrift transport failure
*/
public CTConnection makeRawConnection() throws TTransportException {
final Config cfg = cfgRef.get();
String hostname = cfg.getRandomHost();
if (log.isDebugEnabled())
log.debug("Creating TSocket({}, {}, {}, {}, {})", hostname, cfg.port, cfg.username, cfg.password, cfg.timeoutMS);
TTransport transport = new TFramedTransport(new TSocket(hostname, cfg.port, cfg.timeoutMS), cfg.frameSize);
TBinaryProtocol protocol = new TBinaryProtocol(transport);
Cassandra.Client client = new Cassandra.Client(protocol);
transport.open();
if (cfg.username != null) {
Map<String, String> credentials = new HashMap<String, String>() {
{
put(IAuthenticator.USERNAME_KEY, cfg.username);
put(IAuthenticator.PASSWORD_KEY, cfg.password);
}
};
try {
client.login(new AuthenticationRequest(credentials));
} catch (Exception e) {
// TTransportException will propagate authentication/authorization failure
throw new TTransportException(e);
}
}
return new CTConnection(transport, client, cfg);
}
Aggregations