當前位置: 首頁>>代碼示例>>Java>>正文


Java Cluster類代碼示例

本文整理匯總了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;
}
 
開發者ID:papyrusglobal,項目名稱:state-channels,代碼行數:18,代碼來源:CassandraConfiguration.java

示例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();
}
 
開發者ID:apache,項目名稱:beam,代碼行數:28,代碼來源:CassandraServiceImpl.java

示例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();
}
 
開發者ID:HydAu,項目名稱:Camel,代碼行數:23,代碼來源:CassandraComponentProducerTest.java

示例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);
  }
}
 
開發者ID:datastax,項目名稱:simulacron,代碼行數:23,代碼來源:PeerMetadataIntegrationTest.java

示例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);
  }
}
 
開發者ID:datastax,項目名稱:simulacron,代碼行數:25,代碼來源:Driver3xIntegrationTest.java

示例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;
    }
}
 
開發者ID:mcfongtw,項目名稱:flink-cassandra-connector-examples,代碼行數:22,代碼來源:ClientSessionProvider.java

示例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;
}
 
開發者ID:nielsutrecht,項目名稱:cassandra-it,代碼行數:17,代碼來源:ApplicationConfiguration.java

示例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());
  }
}
 
開發者ID:mnemonic-no,項目名稱:act-platform,代碼行數:24,代碼來源:ClusterManager.java

示例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);
	}
 
開發者ID:dbiir,項目名稱:ts-benchmark,代碼行數:23,代碼來源:CassandraDB.java

示例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);
	}
 
開發者ID:dbiir,項目名稱:ts-benchmark,代碼行數:24,代碼來源:CassandraDB.java

示例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);
	}
 
開發者ID:dbiir,項目名稱:ts-benchmark,代碼行數:25,代碼來源:CassandraDB.java

示例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);
	}
 
開發者ID:dbiir,項目名稱:ts-benchmark,代碼行數:23,代碼來源:CassandraDB.java

示例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);
	}
 
開發者ID:dbiir,項目名稱:ts-benchmark,代碼行數:23,代碼來源:CassandraDB.java

示例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);
	}
 
開發者ID:dbiir,項目名稱:ts-benchmark,代碼行數:23,代碼來源:CassandraDB.java

示例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;
}
 
開發者ID:roscisz,項目名稱:KernelHive,代碼行數:20,代碼來源:CassandraManager.java


注:本文中的com.datastax.driver.core.Cluster類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。