本文整理汇总了Java中org.apache.cassandra.thrift.Cassandra类的典型用法代码示例。如果您正苦于以下问题:Java Cassandra类的具体用法?Java Cassandra怎么用?Java Cassandra使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Cassandra类属于org.apache.cassandra.thrift包,在下文中一共展示了Cassandra类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: describeCassandraTopology
import org.apache.cassandra.thrift.Cassandra; //导入依赖的package包/类
/**
* 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);
}
}
示例2: setup
import org.apache.cassandra.thrift.Cassandra; //导入依赖的package包/类
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()));
}
示例3: createAuthenticatedClient
import org.apache.cassandra.thrift.Cassandra; //导入依赖的package包/类
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;
}
示例4: CassandraClient
import org.apache.cassandra.thrift.Cassandra; //导入依赖的package包/类
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);
}
}
示例5: getClientFromAddressList
import org.apache.cassandra.thrift.Cassandra; //导入依赖的package包/类
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);
}
示例6: connectToKeyspace
import org.apache.cassandra.thrift.Cassandra; //导入依赖的package包/类
@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();
}
}
示例7: connectToKeyspace
import org.apache.cassandra.thrift.Cassandra; //导入依赖的package包/类
@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();
}
}
示例8: getColumnFamily
import org.apache.cassandra.thrift.Cassandra; //导入依赖的package包/类
/**
* 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;
}
示例9: getColumn
import org.apache.cassandra.thrift.Cassandra; //导入依赖的package包/类
/**
* 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;
}
示例10: getKeyspace
import org.apache.cassandra.thrift.Cassandra; //导入依赖的package包/类
@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);
}
示例11: CassandraConnection
import org.apache.cassandra.thrift.Cassandra; //导入依赖的package包/类
/**
* 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));
}
}
示例12: createAuthenticatedClient
import org.apache.cassandra.thrift.Cassandra; //导入依赖的package包/类
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;
}
示例13: createAuthenticatedClient
import org.apache.cassandra.thrift.Cassandra; //导入依赖的package包/类
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;
}
示例14: getColumnValue
import org.apache.cassandra.thrift.Cassandra; //导入依赖的package包/类
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);
}
示例15: open
import org.apache.cassandra.thrift.Cassandra; //导入依赖的package包/类
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);
}
}