/** * 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); } }
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())); }
public static Cassandra.Client createAuthenticatedClient(String location, int port, Configuration conf) throws Exception { logger.debug("Creating authenticated client for CF input format"); TTransport transport = ConfigHelper.getClientTransportFactory(conf).openTransport(location, port); 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) { 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; }
CassandraClient(String hostName, int port, boolean framed) throws TTransportException { TSocket socket = new TSocket(hostName, port); transport = (framed) ? socket : new TFastFramedTransport(socket); transport.open(); client = new Cassandra.Client(new TBinaryProtocol(transport)); try { client.set_cql_version("3.0.0"); } catch (Exception e) { throw new RuntimeException(e); } }
private Cassandra.Client getClientFromAddressList() throws IOException { List<IOException> exceptions = new ArrayList<IOException>(); // randomly select host to connect to List<String> addresses = new ArrayList<>(this.addresses); Collections.shuffle(addresses); for (String address : addresses) { try { return createConnection(address, port, factoryClassName); } catch (IOException ioe) { exceptions.add(ioe); } } log.error("failed to connect to any initial addresses"); for (IOException exception : exceptions) { log.error(exception); } throw exceptions.get(exceptions.size() - 1); }
@Test public void connectToKeyspace() throws Exception { TTransport tr = new TFramedTransport(new TSocket("localhost", Integer.getInteger( "rpcPort", 9160 ))); TProtocol proto = new TBinaryProtocol(tr); Cassandra.Client client = new Cassandra.Client(proto); tr.open(); try { assertThat(client.describe_keyspace("testkeyspace").getStrategy_options().entrySet(), hasItem((Map.Entry<String, String>)new AbstractMap.SimpleEntry<String,String>("replication_factor","1"))); } finally { tr.close(); } }
@Test public void connectToKeyspace() throws Exception { TTransport tr = new TFramedTransport(new TSocket("localhost", Integer.getInteger( "rpcPort", 9160 ))); TProtocol proto = new TBinaryProtocol(tr); Cassandra.Client client = new Cassandra.Client(proto); tr.open(); try { assertThat(client.describe_keyspace("testkeyspacewithspace").getStrategy_options().entrySet(), hasItem((Map.Entry<String, String>)new AbstractMap.SimpleEntry<String,String>("replication_factor","1"))); } finally { tr.close(); } }
/** * getting a column family * * @param client * @param keyspace * @param columnFamily * @return */ protected static CfDef getColumnFamily(Cassandra.Client client, String keyspace, String columnFamily) { CfDef result = null; // try { KsDef kd = client.describe_keyspace(keyspace); for (CfDef entry : kd.getCf_defs()) { if (entry.getName().equals(columnFamily)) { result = entry; break; } } } catch (Exception ex) { ex.printStackTrace(); } return result; }
/** * getting a column * * @param client * @param keyspace * @param columnFamily * @return */ protected static ColumnDef getColumn(Cassandra.Client client, String keyspace, String columnFamily, String column) { ColumnDef result = null; // try { CfDef cd = getColumnFamily(client, keyspace, columnFamily); for (ColumnDef entry : cd.getColumn_metadata()) { if (new String(entry.getName()).equals(column)) { result = entry; break; } } } catch (Exception ex) { ex.printStackTrace(); } return result; }
@Test public void getKeyspace() throws Exception { TTransport ttransport = ctDataSource.getTTransport(); TProtocol tprotocol = new TBinaryProtocol(ttransport); Cassandra.Client client = new Cassandra.Client(tprotocol); // long beg = System.currentTimeMillis(); String KEYSPACE = "system"; // KsDef kd = client.describe_keyspace(KEYSPACE);// NotFoundException ttransport.close(); // long end = System.currentTimeMillis(); System.out.println((end - beg) + " at mills."); // System.out.println(kd); }
/** * 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)); } }
public static Cassandra.Client createAuthenticatedClient(String location, int port, Configuration conf) throws Exception { logger.debug("Creating authenticated client for CF input format"); TTransport transport = ConfigHelper.getClientTransportFactory(conf).openTransport(location, port, conf); 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) { 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; }
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, conf); } 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) { 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; }
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); long timestamp = System.currentTimeMillis(); ColumnPath cp = new ColumnPath(cf); ColumnParent par = new ColumnParent(cf); cp.column = ByteBufferUtil.bytes(colName); // read ColumnOrSuperColumn got = client.get(key_user_id, cp, ConsistencyLevel.ONE); return parseType(validator).getString(got.getColumn().value); }
public void open() { try { // FIXME: Is this the optimal code for thrift 0.5? This code seems to change // with every new Cassandra release and they never update the sample code. // Probably need to get the source package and look at the unit tests to verify. TSocket socket = new TSocket(this.host, this.port); transport = new TFramedTransport(socket); TProtocol protocol = new TBinaryProtocol(transport); client = new Cassandra.Client(protocol); transport.open(); } catch (TTransportException exc) { close(); throw new StorageException("Error opening Cassandra connection", exc); } }
public static Cassandra.Client createAuthenticatedClient(String location, int port, Configuration conf) throws Exception { logger.debug("Creating authenticated client for CF input format"); //TTransport transport = ConfigHelper.getClientTransportFactory(conf).openTransport(location, port, conf); logger.info(">>>>>>>>>>>> Connecting to host " + location + " and port " + port); TTransport transport = ConfigHelper.getClientTransportFactory(conf).openTransport(location, port); 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) { 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; }
private void waitForNodeInitialization(InetAddress addr) { while (true) { try { Cassandra.Client client = createClient(addr); client.describe_cluster_name(); break; } catch (TException e) { LOG.debug(e.toString()); try { Thread.sleep(1000); } catch (InterruptedException ie) { break; } } } }
/** * Connect cassandra and open Transport variable(tr) */ public Cassandra.Client connect(){ try{ TFramedTransport tf = new TFramedTransport(tr); TProtocol proto = new TBinaryProtocol(tf); Cassandra.Client client = new Cassandra.Client(proto); tr.open(); client.set_keyspace(KEYSPACE); return client; }catch(Exception e) { e.printStackTrace(); } return null; }
public CasTimeReader() { try { TTransport tr = new TFramedTransport(new TSocket("10.15.61.111", 9160)); TProtocol proto = new TBinaryProtocol(tr); client = new Cassandra.Client(proto); tr.open(); client.set_keyspace("CadalSecTest"); predicate = new SlicePredicate(); SliceRange range = new SliceRange(); range.setStart(new byte[0]); range.setFinish(new byte[0]); range.setCount(10000); predicate.setSlice_range(range); columnParent = new ColumnParent(); columnParent.setColumn_family("RecordMinute"); } catch (Exception e) { System.out.println(e); } }
public CasTimeBook() { try { TTransport tr = new TFramedTransport(new TSocket("10.15.61.111", 9160)); TProtocol proto = new TBinaryProtocol(tr); client = new Cassandra.Client(proto); tr.open(); client.set_keyspace("CadalSecTest"); predicate = new SlicePredicate(); SliceRange range = new SliceRange(); range.setStart(new byte[0]); range.setFinish(new byte[0]); range.setCount(10000); predicate.setSlice_range(range); columnParent = new ColumnParent(); columnParent.setColumn_family("RecordMinute"); } catch (Exception e) { System.out.println(e); } }