本文整理汇总了Java中com.datastax.driver.core.Cluster类的典型用法代码示例。如果您正苦于以下问题:Java Cluster类的具体用法?Java Cluster怎么用?Java Cluster使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Cluster类属于com.datastax.driver.core包,在下文中一共展示了Cluster类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createSession
import com.datastax.driver.core.Cluster; //导入依赖的package包/类
@Bean
public Session createSession(CassandraProperties properties, Cluster cluster) throws Exception {
Session session = Retriable.wrap(cluster::connect)
.withErrorMessage("Cannot connect to cassandra cluster")
.retryOn(NoHostAvailableException.class)
.withDelaySec(properties.getConnectDelaySec())
.call();
initDb(properties, session);
if (!session.getCluster().getMetadata().checkSchemaAgreement()) {
log.warn("SCHEMA IS NOT IN AGREEMENT!!!");
}
return session;
}
示例2: getCluster
import com.datastax.driver.core.Cluster; //导入依赖的package包/类
/**
* Get a Cassandra cluster using hosts and port.
*/
private Cluster getCluster(List<String> hosts, int port, String username, String password,
String localDc, String consistencyLevel) {
Cluster.Builder builder = Cluster.builder()
.addContactPoints(hosts.toArray(new String[0]))
.withPort(port);
if (username != null) {
builder.withAuthProvider(new PlainTextAuthProvider(username, password));
}
if (localDc != null) {
builder.withLoadBalancingPolicy(
new TokenAwarePolicy(new DCAwareRoundRobinPolicy.Builder().withLocalDc(localDc).build()));
} else {
builder.withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy()));
}
if (consistencyLevel != null) {
builder.withQueryOptions(
new QueryOptions().setConsistencyLevel(ConsistencyLevel.valueOf(consistencyLevel)));
}
return builder.build();
}
示例3: testRequestMessageStatement
import com.datastax.driver.core.Cluster; //导入依赖的package包/类
/**
* Test with incoming message containing a header with RegularStatement.
*/
@Test
public void testRequestMessageStatement() throws Exception {
Update.Where update = update("camel_user")
.with(set("first_name", bindMarker()))
.and(set("last_name", bindMarker()))
.where(eq("login", bindMarker()));
Object response = producerTemplate.requestBodyAndHeader(new Object[]{"Claus 2", "Ibsen 2", "c_ibsen"},
CassandraConstants.CQL_QUERY, update);
Cluster cluster = CassandraUnitUtils.cassandraCluster();
Session session = cluster.connect(CassandraUnitUtils.KEYSPACE);
ResultSet resultSet = session.execute("select login, first_name, last_name from camel_user where login = ?", "c_ibsen");
Row row = resultSet.one();
assertNotNull(row);
assertEquals("Claus 2", row.getString("first_name"));
assertEquals("Ibsen 2", row.getString("last_name"));
session.close();
cluster.close();
}
示例4: testVnodeSupport
import com.datastax.driver.core.Cluster; //导入依赖的package包/类
@Test
public void testVnodeSupport() throws Exception {
// Validate that peers as appropriately discovered when connecting to a node and vnodes are
// assigned.
try (BoundCluster boundCluster =
server.register(ClusterSpec.builder().withNumberOfTokens(256).withNodes(3, 3, 3));
Cluster driverCluster = defaultBuilder(boundCluster).build()) {
driverCluster.init();
// Should be 9 hosts
assertThat(driverCluster.getMetadata().getAllHosts()).hasSize(9);
Set<Token> allTokens = new HashSet<>();
for (Host host : driverCluster.getMetadata().getAllHosts()) {
assertThat(host.getTokens()).hasSize(256);
allTokens.addAll(host.getTokens());
}
// Should be 256*9 unique tokens.
assertThat(allTokens).hasSize(256 * 9);
}
}
示例5: testShouldFailToConnectWithOlderProtocolVersion
import com.datastax.driver.core.Cluster; //导入依赖的package包/类
@Test
public void testShouldFailToConnectWithOlderProtocolVersion() {
try (BoundNode node = server.register(NodeSpec.builder().build());
Cluster cluster = defaultBuilder(node).withProtocolVersion(ProtocolVersion.V2).build()) {
// Since simulacron does not support < V3, an exception should be thrown if we try to force
// an older version.
try {
cluster.connect();
} catch (UnsupportedProtocolVersionException e) {
// expected
}
// Should get a query log indicating invalid protocol version was used.
assertThat(node.getLogs().getQueryLogs()).hasSize(1);
QueryLog log = node.getLogs().getQueryLogs().get(0);
Frame frame = log.getFrame();
assertThat(frame.protocolVersion).isEqualTo(2);
assertThat(frame.warnings).hasSize(1);
assertThat(frame.warnings.get(0))
.isEqualTo(
"This message contains a non-supported protocol version by this node. STARTUP is inferred, but may not reflect the actual message sent.");
assertThat(frame.message).isInstanceOf(Startup.class);
}
}
示例6: getClientSession
import com.datastax.driver.core.Cluster; //导入依赖的package包/类
public static Session getClientSession(String hostAddr) {
if(REGISTRY.containsKey(hostAddr)) {
return REGISTRY.get(hostAddr);
} else {
Cluster.Builder clientClusterBuilder = new Cluster.Builder()
.addContactPoint(hostAddr)
.withQueryOptions(new QueryOptions()
.setConsistencyLevel(ConsistencyLevel.ONE)
.setSerialConsistencyLevel(ConsistencyLevel.LOCAL_SERIAL))
.withoutJMXReporting()
.withoutMetrics()
.withReconnectionPolicy(new ConstantReconnectionPolicy(RECONNECT_DELAY_IN_MS));
long startTimeInMillis = System.currentTimeMillis();
Cluster clientCluster = clientClusterBuilder.build();
Session clientSession = clientCluster.connect();
LOG.info("Client session established after {} ms.", System.currentTimeMillis() - startTimeInMillis);
REGISTRY.putIfAbsent(hostAddr, clientSession);
return clientSession;
}
}
示例7: createSession
import com.datastax.driver.core.Cluster; //导入依赖的package包/类
public static Session createSession(String ip, int port) {
Cluster cluster;
cluster = Cluster.builder()
.addContactPoint(ip)
.withPort(port)
.build();
Session session = cluster.connect();
session.execute("CREATE KEYSPACE IF NOT EXISTS cassandrait WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }");
session.execute("DROP TABLE IF EXISTS cassandrait.counter");
session.execute("CREATE TABLE cassandrait.counter (key text, value counter, PRIMARY key(key));");
return session;
}
示例8: startComponent
import com.datastax.driver.core.Cluster; //导入依赖的package包/类
@Override
public void startComponent() {
if (cluster == null) {
// Configure and build up the Cassandra cluster.
cluster = Cluster.builder()
.withClusterName(clusterName)
.withPort(port)
.withRetryPolicy(DefaultRetryPolicy.INSTANCE)
// TokenAware requires query has routing info (e.g. BoundStatement with all PK value bound).
.withLoadBalancingPolicy(new TokenAwarePolicy(DCAwareRoundRobinPolicy.builder().build()))
.addContactPoints(contactPoints.toArray(new String[contactPoints.size()]))
.build();
// Register any codecs.
cluster.getConfiguration().getCodecRegistry()
.register(new CassandraEnumCodec<>(AccessMode.class, AccessMode.getValueMap()))
.register(new CassandraEnumCodec<>(Direction.class, Direction.getValueMap()))
.register(new CassandraEnumCodec<>(SourceEntity.Type.class, SourceEntity.Type.getValueMap()));
// Create a session.
manager = new MappingManager(cluster.connect());
}
}
示例9: selectByDevice
import com.datastax.driver.core.Cluster; //导入依赖的package包/类
@Override
public Status selectByDevice(TsPoint point, Date startTime, Date endTime) {
long costTime = 0L;
Cluster cluster = null;
try {
// cluster = Cluster.builder().addContactPoint(CASSADRA_URL).build();
// Session session = cluster.connect(KEY_SPACE_NAME);
Session session = SessionManager.getSession();
String selectCql = "SELECT * FROM point WHERE device_code='" + point.getDeviceCode() + "' and timestamp>="
+ startTime.getTime() + " and timestamp<=" + endTime.getTime() + " ALLOW FILTERING";
// System.out.println(selectCql);
long startTime1 = System.nanoTime();
ResultSet rs = session.execute(selectCql);
long endTime1 = System.nanoTime();
costTime = endTime1 - startTime1;
} finally {
if (cluster != null)
cluster.close();
}
// System.out.println("此次查询消耗时间[" + costTime / 1000 + "]s");
return Status.OK(costTime);
}
示例10: selectByDeviceAndSensor
import com.datastax.driver.core.Cluster; //导入依赖的package包/类
@Override
public Status selectByDeviceAndSensor(TsPoint point, Date startTime, Date endTime) {
long costTime = 0L;
Cluster cluster = null;
try {
// cluster = Cluster.builder().addContactPoint(CASSADRA_URL).build();
// Session session = cluster.connect(KEY_SPACE_NAME);
Session session = SessionManager.getSession();
String selectCql = "SELECT * FROM point WHERE device_code='" + point.getDeviceCode() + "' and sensor_code='"
+ point.getSensorCode() + "' and timestamp>=" + startTime.getTime() + " and timestamp<="
+ endTime.getTime() + " ALLOW FILTERING";
// System.out.println(selectCql);
long startTime1 = System.nanoTime();
ResultSet rs = session.execute(selectCql);
long endTime1 = System.nanoTime();
costTime = endTime1 - startTime1;
} finally {
if (cluster != null)
cluster.close();
}
// System.out.println("此次查询消耗时间[" + costTime / 1000 + "]s");
return Status.OK(costTime);
}
示例11: selectMaxByDeviceAndSensor
import com.datastax.driver.core.Cluster; //导入依赖的package包/类
@Override
public Status selectMaxByDeviceAndSensor(String deviceCode, String sensorCode, Date startTime, Date endTime) {
long costTime = 0L;
Cluster cluster = null;
try {
// cluster = Cluster.builder().addContactPoint(CASSADRA_URL).build();
// Session session = cluster.connect(KEY_SPACE_NAME);
Session session = SessionManager.getSession();
String selectCql = "SELECT MAX(value) FROM point WHERE device_code='" + deviceCode + "' and sensor_code='"
+ sensorCode + "' and timestamp>=" + startTime.getTime() + " and timestamp<=" + endTime.getTime()
+ " ALLOW FILTERING";
long startTime1 = System.nanoTime();
// System.out.println("aaa");
ResultSet rs = session.execute(selectCql);
// System.out.println("bbb");
long endTime1 = System.nanoTime();
costTime = endTime1 - startTime1;
} finally {
if (cluster != null)
cluster.close();
}
// System.out.println("此次查询消耗时间[" + costTime / 1000 + "]s");
return Status.OK(costTime);
}
示例12: selectMinByDeviceAndSensor
import com.datastax.driver.core.Cluster; //导入依赖的package包/类
@Override
public Status selectMinByDeviceAndSensor(String deviceCode, String sensorCode, Date startTime, Date endTime) {
long costTime = 0L;
Cluster cluster = null;
try {
cluster = Cluster.builder().addContactPoint(CASSADRA_URL).build();
Session session = cluster.connect(KEY_SPACE_NAME);
String selectCql = "SELECT MIN(value) FROM point WHERE device_code='" + deviceCode + "' and sensor_code='"
+ sensorCode + "' and timestamp>=" + startTime.getTime() + " and timestamp<=" + endTime.getTime()
+ " ALLOW FILTERING";
// System.out.println(selectCql);
long startTime1 = System.nanoTime();
ResultSet rs = session.execute(selectCql);
long endTime1 = System.nanoTime();
costTime = endTime1 - startTime1;
} finally {
if (cluster != null)
cluster.close();
}
// System.out.println("此次查询消耗时间[" + costTime / 1000 + "]s");
return Status.OK(costTime);
}
示例13: selectAvgByDeviceAndSensor
import com.datastax.driver.core.Cluster; //导入依赖的package包/类
@Override
public Status selectAvgByDeviceAndSensor(String deviceCode, String sensorCode, Date startTime, Date endTime) {
long costTime = 0L;
Cluster cluster = null;
try {
cluster = Cluster.builder().addContactPoint(CASSADRA_URL).build();
Session session = cluster.connect(KEY_SPACE_NAME);
String selectCql = "SELECT AVG(value) FROM point WHERE device_code='" + deviceCode + "' and sensor_code='"
+ sensorCode + "' and timestamp>=" + startTime.getTime() + " and timestamp<=" + endTime.getTime()
+ " ALLOW FILTERING";
// System.out.println(selectCql);
long startTime1 = System.nanoTime();
ResultSet rs = session.execute(selectCql);
long endTime1 = System.nanoTime();
costTime = endTime1 - startTime1;
} finally {
if (cluster != null)
cluster.close();
}
// System.out.println("此次查询消耗时间[" + costTime / 1000 + "]s");
return Status.OK(costTime);
}
示例14: selectCountByDeviceAndSensor
import com.datastax.driver.core.Cluster; //导入依赖的package包/类
@Override
public Status selectCountByDeviceAndSensor(String deviceCode, String sensorCode, Date startTime, Date endTime) {
long costTime = 0L;
Cluster cluster = null;
try {
cluster = Cluster.builder().addContactPoint(CASSADRA_URL).build();
Session session = cluster.connect(KEY_SPACE_NAME);
String selectCql = "SELECT COUNT(*) FROM point WHERE device_code='" + deviceCode + "' and sensor_code='"
+ sensorCode + "' and timestamp>=" + startTime.getTime() + " and timestamp<=" + endTime.getTime()
+ " ALLOW FILTERING";
// System.out.println(selectCql);
long startTime1 = System.nanoTime();
ResultSet rs = session.execute(selectCql);
long endTime1 = System.nanoTime();
costTime = endTime1 - startTime1;
} finally {
if (cluster != null)
cluster.close();
}
// System.out.println("此次查询消耗时间[" + costTime / 1000 + "]s");
return Status.OK(costTime);
}
示例15: uploadPackage
import com.datastax.driver.core.Cluster; //导入依赖的package包/类
@Override
public long uploadPackage(DataPackage dataPack) {
long time = System.currentTimeMillis();
try {
Session session;
Cluster cluster;
cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
session = cluster.connect();
ByteBuffer buffer = ByteBuffer.wrap(dataPack.getData());
Statement statement = QueryBuilder.insertInto(DATABASE, MAIN_TABLE)
.value(COL_ID, time)
.value(COL_DATA, buffer)
.value(COL_DESC, dataPack.getDescription());
session.execute(statement);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
return time;
}