use of org.apache.thrift.transport.TSocket in project heron by twitter.
the class ScribeSink method open.
// Open the TTransport connection and client to scribe server
private boolean open() {
try {
TSocket socket = new TSocket((String) config.get(KEY_SCRIBE_HOST), TypeUtils.getInteger(config.get(KEY_SCRIBE_PORT)), TypeUtils.getInteger(config.get(KEY_SCRIBE_TIMEOUT_MS)));
transport = new TFramedTransport(socket);
transport.open();
} catch (TException tx) {
LOG.log(Level.SEVERE, "Failed to open connection to scribe server " + connectionString(), tx);
return false;
}
LOG.info("Opened connection to scribe server " + connectionString());
TProtocol protocol = new TBinaryProtocol(transport);
client = new scribe.Client(protocol);
return true;
}
use of org.apache.thrift.transport.TSocket in project eiger by wlloyd.
the class WordCountSetup method createConnection.
private static Cassandra.Client createConnection(String host, Integer port, boolean framed) throws TTransportException {
TSocket socket = new TSocket(host, port);
TTransport trans = framed ? new TFramedTransport(socket) : socket;
trans.open();
TProtocol protocol = new TBinaryProtocol(trans);
return new Cassandra.Client(protocol);
}
use of org.apache.thrift.transport.TSocket 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.TSocket in project eiger by wlloyd.
the class ColumnFamilyRecordReader method initialize.
@Override
public void initialize(InputSplit split, TaskAttemptContext context) throws IOException {
this.split = (ColumnFamilySplit) split;
Configuration conf = context.getConfiguration();
predicate = ConfigHelper.getInputSlicePredicate(conf);
isEmptyPredicate = isEmptyPredicate(predicate);
totalRowCount = ConfigHelper.getInputSplitSize(conf);
batchRowCount = ConfigHelper.getRangeBatchSize(conf);
cfName = ConfigHelper.getInputColumnFamily(conf);
consistencyLevel = ConsistencyLevel.valueOf(ConfigHelper.getReadConsistencyLevel(conf));
keyspace = ConfigHelper.getInputKeyspace(conf);
try {
// only need to connect once
if (socket != null && socket.isOpen())
return;
// create connection using thrift
String location = getLocation();
socket = new TSocket(location, ConfigHelper.getInputRpcPort(conf));
TBinaryProtocol binaryProtocol = new TBinaryProtocol(new TFramedTransport(socket));
client = new Cassandra.Client(binaryProtocol);
socket.open();
// log in
client.set_keyspace(keyspace, LamportClock.COPS_UNSUPPORTED);
if (ConfigHelper.getInputKeyspaceUserName(conf) != null) {
Map<String, String> creds = new HashMap<String, String>();
creds.put(IAuthenticator.USERNAME_KEY, ConfigHelper.getInputKeyspaceUserName(conf));
creds.put(IAuthenticator.PASSWORD_KEY, ConfigHelper.getInputKeyspacePassword(conf));
AuthenticationRequest authRequest = new AuthenticationRequest(creds);
client.login(authRequest, LamportClock.COPS_UNSUPPORTED);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
iter = new RowIterator();
}
use of org.apache.thrift.transport.TSocket in project eiger by wlloyd.
the class TCustomServerSocket method acceptImpl.
@Override
protected TCustomSocket acceptImpl() throws TTransportException {
if (serverSocket_ == null)
throw new TTransportException(TTransportException.NOT_OPEN, "No underlying server socket.");
TCustomSocket tsocket = null;
Socket socket = null;
try {
socket = serverSocket_.accept();
tsocket = new TCustomSocket(socket);
tsocket.setTimeout(0);
} catch (IOException iox) {
throw new TTransportException(iox);
}
try {
socket.setKeepAlive(this.keepAlive);
} catch (SocketException se) {
logger.warn("Failed to set keep-alive on Thrift socket.", se);
}
if (this.sendBufferSize != null) {
try {
socket.setSendBufferSize(this.sendBufferSize.intValue());
} catch (SocketException se) {
logger.warn("Failed to set send buffer size on Thrift socket.", se);
}
}
if (this.recvBufferSize != null) {
try {
socket.setReceiveBufferSize(this.recvBufferSize.intValue());
} catch (SocketException se) {
logger.warn("Failed to set receive buffer size on Thrift socket.", se);
}
}
return tsocket;
}
Aggregations