/** * Return a client based on the given socket that points to the configured keyspace, and is logged * in with the configured credentials. * * @param socket a socket pointing to a particular node, seed or otherwise * @param conf a job configuration * @return a cassandra client * @throws InvalidRequestException * @throws TException * @throws AuthenticationException * @throws AuthorizationException */ public static Cassandra.Client createAuthenticatedClient(TSocket socket, Configuration conf) throws InvalidRequestException, TException, AuthenticationException, AuthorizationException { TBinaryProtocol binaryProtocol = new TBinaryProtocol(new TFramedTransport(socket)); Cassandra.Client client = new Cassandra.Client(binaryProtocol); socket.open(); client.set_keyspace(ConfigHelper.getOutputKeyspace(conf)); if (ConfigHelper.getOutputKeyspaceUserName(conf) != null) { Map<String, String> creds = new HashMap<String, String>(); creds.put(SimpleAuthenticator.USERNAME_KEY, ConfigHelper.getOutputKeyspaceUserName(conf)); creds.put(SimpleAuthenticator.PASSWORD_KEY, ConfigHelper.getOutputKeyspacePassword(conf)); AuthenticationRequest authRequest = new AuthenticationRequest(creds); client.login(authRequest); } return client; }
private static Cassandra.Client createThriftClient( String host, int port, String user, String passwd, ITransportFactory transportFactory) throws Exception { TTransport trans = transportFactory.openTransport(host, port); TProtocol protocol = new TBinaryProtocol(trans); Cassandra.Client client = new Cassandra.Client(protocol); if (user != null && passwd != null) { Map<String, String> credentials = new HashMap<>(); credentials.put(IAuthenticator.USERNAME_KEY, user); credentials.put(IAuthenticator.PASSWORD_KEY, passwd); AuthenticationRequest authenticationRequest = new AuthenticationRequest(credentials); client.login(authenticationRequest); } return client; }
/** * Connects to the given server:port and returns a client based on the given socket that points to * the configured keyspace, and is logged in with the configured credentials. * * @param host fully qualified host name to connect to * @param port RPC port of the server * @param conf a job configuration * @return a cassandra client * @throws Exception set of thrown exceptions may be implementation defined, depending on the used * transport factory */ public static Cassandra.Client createAuthenticatedClient( String host, int port, Configuration conf) throws Exception { logger.debug("Creating authenticated client for CF output format"); TTransport transport = ConfigHelper.getClientTransportFactory(conf).openTransport(host, port); TProtocol binaryProtocol = new TBinaryProtocol(transport, true, true); Cassandra.Client client = new Cassandra.Client(binaryProtocol); client.set_keyspace(ConfigHelper.getOutputKeyspace(conf)); if (ConfigHelper.getOutputKeyspaceUserName(conf) != null) { Map<String, String> creds = new HashMap<String, String>(); creds.put(IAuthenticator.USERNAME_KEY, ConfigHelper.getOutputKeyspaceUserName(conf)); creds.put(IAuthenticator.PASSWORD_KEY, ConfigHelper.getOutputKeyspacePassword(conf)); AuthenticationRequest authRequest = new AuthenticationRequest(creds); client.login(authRequest); } logger.debug("Authenticated client for CF output format created successfully"); return client; }
/** * Initialize any state for this DB. Called once per DB instance; there is one DB instance per * client thread. */ public void init() throws DBException { String hosts = getProperties().getProperty("hosts"); if (hosts == null) { throw new DBException("Required property \"hosts\" missing for CassandraClient"); } column_family = getProperties().getProperty(COLUMN_FAMILY_PROPERTY, COLUMN_FAMILY_PROPERTY_DEFAULT); parent = new ColumnParent(column_family); ConnectionRetries = Integer.parseInt( getProperties() .getProperty(CONNECTION_RETRY_PROPERTY, CONNECTION_RETRY_PROPERTY_DEFAULT)); OperationRetries = Integer.parseInt( getProperties() .getProperty(OPERATION_RETRY_PROPERTY, OPERATION_RETRY_PROPERTY_DEFAULT)); String username = getProperties().getProperty(USERNAME_PROPERTY); String password = getProperties().getProperty(PASSWORD_PROPERTY); _debug = Boolean.parseBoolean(getProperties().getProperty("debug", "false")); String[] allhosts = hosts.split(","); String myhost = allhosts[random.nextInt(allhosts.length)]; Exception connectexception = null; for (int retry = 0; retry < ConnectionRetries; retry++) { tr = new TFramedTransport(new TSocket(myhost, 9160)); TProtocol proto = new TBinaryProtocol(tr); client = new Cassandra.Client(proto); try { tr.open(); connectexception = null; break; } catch (Exception e) { connectexception = e; } try { Thread.sleep(1000); } catch (InterruptedException e) { } } if (connectexception != null) { System.err.println( "Unable to connect to " + myhost + " after " + ConnectionRetries + " tries"); System.out.println( "Unable to connect to " + myhost + " after " + ConnectionRetries + " tries"); throw new DBException(connectexception); } if (username != null && password != null) { Map<String, String> cred = new HashMap<String, String>(); cred.put("username", username); cred.put("password", password); AuthenticationRequest req = new AuthenticationRequest(cred); try { client.login(req); } catch (Exception e) { throw new DBException(e); } } }