org.apache.cassandra.thrift.Cassandra Java Examples

The following examples show how to use org.apache.cassandra.thrift.Cassandra. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AstyanaxBlockedDataReaderDAO.java    From emodb with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the topology for a Cassandra keyspace as a Multimap, where the keys identify a rack (or availability zone
 * in Amazon) and the values are the token ranges for each host in that rack.  For example, for a well distributed
 * ring of 12 hosts and a replication factor of 3 this method would return a Multimap with 3 keys and each key would
 * contain 4 token ranges.
 */
private Multimap<String, TokenRange> describeCassandraTopology(final Keyspace keyspace) {
    try {
        @SuppressWarnings ("unchecked")
        ConnectionPool<Cassandra.Client> connectionPool = (ConnectionPool<Cassandra.Client>) keyspace.getConnectionPool();

        return connectionPool.executeWithFailover(
                new AbstractKeyspaceOperationImpl<Multimap<String, TokenRange>>(EmptyKeyspaceTracerFactory.getInstance().newTracer(CassandraOperationType.DESCRIBE_RING), keyspace.getKeyspaceName()) {
                    @Override
                    protected Multimap<String, TokenRange> internalExecute(Cassandra.Client client, ConnectionContext state)
                            throws Exception {
                        Multimap<String, TokenRange> racks = ArrayListMultimap.create();
                        for (org.apache.cassandra.thrift.TokenRange tokenRange : client.describe_local_ring(getKeyspace())) {
                            // The final local endpoint "owns" the token range, the rest are for replication
                            EndpointDetails endpointDetails = Iterables.getLast(tokenRange.getEndpoint_details());
                            racks.put(endpointDetails.getRack(),
                                    new TokenRangeImpl(tokenRange.getStart_token(), tokenRange.getEnd_token(), tokenRange.getEndpoints()));
                        }
                        return Multimaps.unmodifiableMultimap(racks);
                    }
                },
                keyspace.getConfig().getRetryPolicy().duplicate()).getResult();
    } catch (ConnectionException e) {
        throw Throwables.propagate(e);
    }
}
 
Example #2
Source File: CassandraConnection.java    From learning-hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Construct an CassandaraConnection with optional authentication.
 * 
 * @param host the host to connect to
 * @param port the port to use
 * @param username the username to authenticate with (may be null
 * for no authentication)
 * @param password the password to authenticate with (may be null
 * for no authentication)
 * @throws Exception if the connection fails
 */
public CassandraConnection(String host, int port,
    String username, String password, int timeout) throws Exception {
  TSocket socket = new TSocket(host, port);
  if (timeout > 0) {
    socket.setTimeout(timeout);
  }
  
  m_transport = new TFramedTransport(socket);
  TProtocol protocol = new TBinaryProtocol(m_transport);
  m_client = new Cassandra.Client(protocol);      
  m_transport.open();
  
  if (!Const.isEmpty(username) && !Const.isEmpty(password)) {
    Map<String, String> creds = new HashMap<String, String>();
    creds.put("username", username);
    creds.put("password", password);
    m_client.login(new AuthenticationRequest(creds));
  }
}
 
Example #3
Source File: TestRingCache.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private void setup(String server, int port) throws Exception
{
    /* Establish a thrift connection to the cassandra instance */
    TSocket socket = new TSocket(server, port);
    System.out.println(" connected to " + server + ":" + port + ".");
    TBinaryProtocol binaryProtocol = new TBinaryProtocol(new TFramedTransport(socket));
    Cassandra.Client cassandraClient = new Cassandra.Client(binaryProtocol);
    socket.open();
    thriftClient = cassandraClient;
    String seed = DatabaseDescriptor.getSeeds().iterator().next().getHostAddress();
    conf = new Configuration();
    ConfigHelper.setOutputPartitioner(conf, DatabaseDescriptor.getPartitioner().getClass().getName());
    ConfigHelper.setOutputInitialAddress(conf, seed);
    ConfigHelper.setOutputRpcPort(conf, Integer.toString(DatabaseDescriptor.getRpcPort()));

}
 
Example #4
Source File: CassandraThriftFacade.java    From emodb with Apache License 2.0 5 votes vote down vote up
public CassandraThriftFacade(TFramedTransport transport) {
    _transport = transport;
    _client = new Cassandra.Client(new TBinaryProtocol(_transport));
    try {
        _transport.open();
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
 
Example #5
Source File: ExternalSSTableLoaderClient.java    From hdfs2cass with Apache License 2.0 5 votes vote down vote up
private static Cassandra.Client createThriftClient(String host, int port) throws TTransportException {
  TSocket socket = new TSocket(host, port);
  TTransport trans = new TFramedTransport(socket);
  trans.open();
  TProtocol protocol = new org.apache.thrift.protocol.TBinaryProtocol(trans);
  return new Cassandra.Client(protocol);
}
 
Example #6
Source File: CassandraClientHolder.java    From Hive-Cassandra with Apache License 2.0 5 votes vote down vote up
private void initClient() throws CassandraException
{
    try
    {
        transport.open();
    } catch (TTransportException e)
    {
        throw new CassandraException("unable to connect to server", e);
    }

    client = new Cassandra.Client(new TBinaryProtocol(transport));

    // connect to last known keyspace
    setKeyspace(keyspace);
}
 
Example #7
Source File: ThriftColumnFamilyTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private String getColumnValue(String ks, String cf, String colName, String key, String validator)
throws AuthenticationException, AuthorizationException, InvalidRequestException, UnavailableException, TimedOutException, TException, NotFoundException, IOException
{
    Cassandra.Client client = getClient();
    client.set_keyspace(ks);

    ByteBuffer key_user_id = ByteBufferUtil.bytes(key);
    ColumnPath cp = new ColumnPath(cf);
    cp.column = ByteBufferUtil.bytes(colName);

    // read
    ColumnOrSuperColumn got = client.get(key_user_id, cp, ConsistencyLevel.ONE);
    return parseType(validator).getString(got.getColumn().value);
}
 
Example #8
Source File: PigTestBase.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
protected static Cassandra.Client getClient() throws TTransportException
{
    TTransport tr = new TFramedTransport(new TSocket("localhost", 9170));
    TProtocol proto = new TBinaryProtocol(tr);
    Cassandra.Client client = new Cassandra.Client(proto);
    tr.open();
    return client;
}
 
Example #9
Source File: AbstractColumnFamilyInputFormat.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public static Cassandra.Client createAuthenticatedClient(String location, int port, Configuration conf) throws Exception
{
    logger.debug("Creating authenticated client for CF input format");
    TTransport transport;
    try
    {
        transport = ConfigHelper.getClientTransportFactory(conf).openTransport(location, port);
    }
    catch (Exception e)
    {
        throw new TTransportException("Failed to open a transport to " + location + ":" + port + ".", e);
    }
    TProtocol binaryProtocol = new TBinaryProtocol(transport, true, true);
    Cassandra.Client client = new Cassandra.Client(binaryProtocol);

    // log in
    client.set_keyspace(ConfigHelper.getInputKeyspace(conf));
    if ((ConfigHelper.getInputKeyspaceUserName(conf) != null) && (ConfigHelper.getInputKeyspacePassword(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);
    }
    logger.debug("Authenticated client for CF input format created successfully");
    return client;
}
 
Example #10
Source File: AbstractCassandraInputAdapter.java    From OpenRate with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to connect to Cassandra.
 *
 * @throws OpenRate.exception.InitializationException
 */
public void initCassandraConnection() throws InitializationException {
  tr = new TFramedTransport(new TSocket(cassandraIPAddr, cassandraPort));
  TProtocol proto = new TBinaryProtocol(tr);
  client = new Cassandra.Client(proto);

  try {
    tr.open();
  } catch (TTransportException ex) {
    message = "Transport exception opening Cassandra transport";
    throw new InitializationException(message, ex, getSymbolicName());
  }
}
 
Example #11
Source File: AbstractCassandraInputAdapter.java    From OpenRate with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to connect to Cassandra.
 *
 * @throws OpenRate.exception.ProcessingException
 */
public void openCassandraConnection() throws ProcessingException {
  tr = new TFramedTransport(new TSocket(cassandraIPAddr, 9160));
  TProtocol proto = new TBinaryProtocol(tr);
  client = new Cassandra.Client(proto);

  try {
    tr.open();
  } catch (TTransportException ex) {
    message = "Transport exception opening Cassandra transport";
    throw new ProcessingException(message, ex, getSymbolicName());
  }
}
 
Example #12
Source File: AbstractCassandraOutputAdapter.java    From OpenRate with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to connect to Cassandra.
 *
 * @throws OpenRate.exception.ProcessingException
 */
public void openCassandraConnection() throws ProcessingException {
  tr = new TFramedTransport(new TSocket(cassandraIPAddr, 9160));
  TProtocol proto = new TBinaryProtocol(tr);
  client = new Cassandra.Client(proto);

  try {
    tr.open();
  } catch (TTransportException ex) {
    message = "Transport exception opening Cassandra transport";
    throw new ProcessingException(message, ex, getSymbolicName());
  }
}
 
Example #13
Source File: AbstractCassandraOutputAdapter.java    From OpenRate with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to connect to Cassandra.
 *
 * @throws OpenRate.exception.InitializationException
 */
public void initCassandraConnection() throws InitializationException {
  tr = new TFramedTransport(new TSocket(cassandraIPAddr, cassandraPort));
  TProtocol proto = new TBinaryProtocol(tr);
  client = new Cassandra.Client(proto);

  try {
    tr.open();
  } catch (TTransportException ex) {
    message = "Transport exception opening Cassandra transport";
    throw new InitializationException(message, ex, getSymbolicName());
  }
}
 
Example #14
Source File: Schema.java    From janusgraph_tutorial with Apache License 2.0 5 votes vote down vote up
private void dropOldKeyspace() throws InvalidRequestException, SchemaDisagreementException, TException {
  TTransport tr = new TFramedTransport(new TSocket("localhost", 9160));
  TProtocol proto = new TBinaryProtocol(tr);
  Cassandra.Client client = new Cassandra.Client(proto);
  tr.open();

  client.system_drop_keyspace(JANUSGRAPH);
  LOGGER.info("DROPPED keyspace janusgraph");
  tr.close();
}
 
Example #15
Source File: AbstractCassandraOutputAdapter.java    From OpenRate with Apache License 2.0 4 votes vote down vote up
/**
 * @param client the client to set
 */
public void setClient(Cassandra.Client client) {
  this.client = client;
}
 
Example #16
Source File: AbstractCassandraInputAdapter.java    From OpenRate with Apache License 2.0 4 votes vote down vote up
/**
 * @return the client
 */
public Cassandra.Client getClient() {
  return client;
}
 
Example #17
Source File: AbstractCassandraInputAdapter.java    From OpenRate with Apache License 2.0 4 votes vote down vote up
/**
 * @param client the client to set
 */
public void setClient(Cassandra.Client client) {
  this.client = client;
}
 
Example #18
Source File: AbstractCassandraOutputAdapter.java    From OpenRate with Apache License 2.0 4 votes vote down vote up
/**
 * @return the client
 */
public Cassandra.Client getClient() {
  return client;
}
 
Example #19
Source File: StressSettings.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public Cassandra.Client getRawThriftClient(boolean setKeyspace)
{
    return getRawThriftClient(node.randomNode(), setKeyspace);
}
 
Example #20
Source File: StressSettings.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public Cassandra.Client getRawThriftClient(String host)
{
    return getRawThriftClient(host, true);
}
 
Example #21
Source File: CTConnection.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
public Cassandra.Client getClient() {
    return client;
}
 
Example #22
Source File: KeyspaceUtil.java    From emodb with Apache License 2.0 4 votes vote down vote up
private Keyspace pinToVerifiedHost(Host host) throws ConnectionException {
    //noinspection unchecked
    PinnedConnectionPool<Cassandra.Client> pinnedPool = new PinnedConnectionPool(_keyspace.getConnectionPool(), host);
    return new ThriftKeyspaceImpl(
            _keyspace.getKeyspaceName(), pinnedPool, _keyspace.getConfig(), EmptyKeyspaceTracerFactory.getInstance());
}
 
Example #23
Source File: CassandraClientHolder.java    From Hive-Cassandra with Apache License 2.0 4 votes vote down vote up
public Cassandra.Client getClient()
{
    return client;
}
 
Example #24
Source File: CassandraProxyClient.java    From Hive-Cassandra with Apache License 2.0 4 votes vote down vote up
/**
 * Return a handle to the proxied connection
 * @return
 */
public Cassandra.Iface getProxyConnection() {
    return (Cassandra.Iface) java.lang.reflect.Proxy.newProxyInstance(Cassandra.Client.class
            .getClassLoader(),Cassandra.Client.class.getInterfaces(), this);
}
 
Example #25
Source File: CassandraConnection.java    From learning-hadoop with Apache License 2.0 2 votes vote down vote up
/**
 * Get the encapsulated Cassandra.Client object
 * 
 * @return the encapsulated Cassandra.Client object
 */
public Cassandra.Client getClient() {
  return m_client;
}
 
Example #26
Source File: DBConn.java    From Doradus with Apache License 2.0 2 votes vote down vote up
/**
 * Get this DBConn's Cassandra.Client session. This should only be used temporarily
 * since it is shared with this DBConn object.
 * 
 * @return  This DBConn's Cassandra.Client session, which will be a keyspace or a
 *          no-keyspace session depending on how this object was constructed.
 */
public Cassandra.Client getClientSession() {
    return m_client;
}