com.datastax.driver.core.ProtocolVersion Java Examples

The following examples show how to use com.datastax.driver.core.ProtocolVersion. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PersistentActorUpdateEventProcessor.java    From elasticactors with Apache License 2.0 6 votes vote down vote up
/**
 * Helper methods to optimize for the batching INSERT statements (protocol V1 only)
 *
 * @param maxBatchSize
 */
private void prepareBatchIfNeeded(int maxBatchSize) {
    // check the protocol to see if BatchStatements are supported
    ProtocolVersion protocolVersion = cassandraSession.getCluster().getConfiguration().getProtocolOptions().getProtocolVersion();
    if(ProtocolVersion.V1.equals(protocolVersion)) {
        for (int batchSize = 2; batchSize <= maxBatchSize ; batchSize++) {
            // create a prepared statement (INSERT only)
            StringBuilder batchBuilder = new StringBuilder("BEGIN UNLOGGED BATCH ");
            for (int i = 0; i < batchSize; i++) {
                batchBuilder.append("   ").append(INSERT_QUERY).append("; ");
            }
            batchBuilder.append("APPLY BATCH");
            batchStatements.put(batchSize, cassandraSession.prepare(batchBuilder.toString()));
        }
    }
}
 
Example #2
Source File: DataAccessImpl.java    From hawkular-metrics with Apache License 2.0 6 votes vote down vote up
private Observable.Transformer<BoundStatement, Integer> applyMicroBatching() {
    return tObservable -> tObservable
            .groupBy(b -> {
                ByteBuffer routingKey = b.getRoutingKey(ProtocolVersion.NEWEST_SUPPORTED,
                        codecRegistry);
                Token token = metadata.newToken(routingKey);
                for (TokenRange tokenRange : session.getCluster().getMetadata().getTokenRanges()) {
                    if (tokenRange.contains(token)) {
                        return tokenRange;
                    }
                }
                log.warn("Unable to find any Cassandra node to insert token " + token.toString());
                return session.getCluster().getMetadata().getTokenRanges().iterator().next();
            })
            .flatMap(g -> g.compose(new BoundBatchStatementTransformer()))
            .flatMap(batch -> rxSession
                    .execute(batch)
                    .compose(applyInsertRetryPolicy())
                    .map(resultSet -> batch.size())
            );
}
 
Example #3
Source File: CassandraClient.java    From debezium-incubator with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
CassandraClient(CassandraConnectorConfig config, LoadBalancingPolicy lbPolicy) throws GeneralSecurityException, IOException {
    Cluster.Builder builder = Cluster.builder()
            .addContactPoints(config.cassandraHosts())
            .withPort(config.cassandraPort())
            .withProtocolVersion(ProtocolVersion.V4)
            .withLoadBalancingPolicy(lbPolicy)
            // See https://docs.datastax.com/en/developer/java-driver/3.5/manual/metrics/#metrics-4-compatibility.
            .withoutJMXReporting();

    if (config.cassandraUsername() != null && config.cassandraPassword() != null) {
        builder.withCredentials(config.cassandraUsername(), config.cassandraPassword());
    }

    if (config.cassandraSslEnabled()) {
        SslContext sslContext = createSslContext(config.cassandraSslConfigPath());
        SSLOptions sslOptions = new RemoteEndpointAwareNettySSLOptions(sslContext);
        builder.withSSL(sslOptions);
    }

    cluster = builder.build();
    session = cluster.connect();

    registerClusterMetrics(cluster.getClusterName());
}
 
Example #4
Source File: Driver3xIntegrationTest.java    From simulacron with Apache License 2.0 6 votes vote down vote up
@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);
  }
}
 
Example #5
Source File: DateTimeCodec.java    From cassandra-reaper with Apache License 2.0 6 votes vote down vote up
@Override
public long deserializeNoBoxing(ByteBuffer bytes, ProtocolVersion protocolVersion) {
  if (bytes == null || bytes.remaining() == 0) {
    return 0;
  }
  if (bytes.remaining() != 8) {
    throw new InvalidTypeException("Invalid 64-bits long value, expecting 8 bytes but got " + bytes.remaining());
  }

  return bytes.getLong(bytes.position());
}
 
Example #6
Source File: CassandraStore.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a cluster object.
 */
public void buildCluster()
{
  try {
    if (protocolVersion != null && protocolVersion.length() != 0) {
      ProtocolVersion version = getCassandraProtocolVersion();
      cluster = Cluster.builder().addContactPoint(node).withCredentials(userName, password).withProtocolVersion(version).build();
    } else {
      cluster = Cluster.builder().addContactPoint(node).withCredentials(userName, password).build();
    }
  } catch (DriverException ex) {
    throw new RuntimeException("closing database resource", ex);
  } catch (Throwable t) {
    DTThrowable.rethrow(t);
  }
}
 
Example #7
Source File: CassandraCell.java    From deep-spark with Apache License 2.0 5 votes vote down vote up
/**
 * Private constructor.
 */
private CassandraCell(Cell metadata, ByteBuffer cellValue) {
    this.cellName = metadata.getCellName();
    this.isClusterKey = ((CassandraCell) metadata).isClusterKey;
    this.isKey = ((CassandraCell) metadata).isPartitionKey();
    this.cellValidator = ((CassandraCell) metadata).cellValidator;
    if (cellValue != null) {
        if (((CassandraCell) metadata).getDataType() != null) {
            this.cellValue = ((CassandraCell) metadata).getDataType().deserialize(cellValue, ProtocolVersion.V2);
        } else {
            this.cellValue = ((CassandraCell) metadata).marshaller().compose(cellValue);
        }
    }
}
 
Example #8
Source File: BigDecimalToBigintCodec.java    From cassandra-jdbc-wrapper with Apache License 2.0 5 votes vote down vote up
@Override
public BigDecimal deserialize(ByteBuffer paramByteBuffer, ProtocolVersion paramProtocolVersion) throws InvalidTypeException {
	if (paramByteBuffer == null) {
		return null;

	}
	// always duplicate the ByteBuffer instance before consuming it!
	Long value = ByteBufferUtil.toLong(paramByteBuffer.duplicate());
	return new BigDecimal(value);
}
 
Example #9
Source File: IntToLongCodec.java    From cassandra-jdbc-wrapper with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuffer serialize(Long paramT, ProtocolVersion paramProtocolVersion) throws InvalidTypeException {
	if (paramT == null) {
		return null;
	}
	return ByteBufferUtil.bytes(paramT.intValue());
}
 
Example #10
Source File: DoubleToFloatCodec.java    From cassandra-jdbc-wrapper with Apache License 2.0 5 votes vote down vote up
@Override
public Double deserialize(ByteBuffer paramByteBuffer, ProtocolVersion paramProtocolVersion) throws InvalidTypeException {
	if (paramByteBuffer == null) {
		return null;

	}
	// always duplicate the ByteBuffer instance before consuming it!
	Float value = ByteBufferUtil.toFloat(paramByteBuffer.duplicate());		
	return value.doubleValue();
}
 
Example #11
Source File: LongToIntCodec.java    From cassandra-jdbc-wrapper with Apache License 2.0 5 votes vote down vote up
@Override
public Integer deserialize(ByteBuffer paramByteBuffer, ProtocolVersion paramProtocolVersion) throws InvalidTypeException {
	if (paramByteBuffer == null) {
		return null;

	}
	// always duplicate the ByteBuffer instance before consuming it!
	Long value = ByteBufferUtil.toLong(paramByteBuffer.duplicate());
	return value.intValue();
}
 
Example #12
Source File: TimestampToLongCodec.java    From cassandra-jdbc-wrapper with Apache License 2.0 5 votes vote down vote up
@Override
public Long deserialize(ByteBuffer paramByteBuffer, ProtocolVersion paramProtocolVersion) throws InvalidTypeException {
	if (paramByteBuffer == null) {
		return null;

	}
	// always duplicate the ByteBuffer instance before consuming it!
	return ByteBufferUtil.toLong(paramByteBuffer.duplicate());
}
 
Example #13
Source File: TestCassandraTarget.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test
public void testWriteEmptyBatch() throws InterruptedException, StageException {
  final String tableName = "test.trips";
  List<CassandraFieldMappingConfig> fieldMappings = ImmutableList.of(
      new CassandraFieldMappingConfig("[0]", "driver_id"),
      new CassandraFieldMappingConfig("[1]", "trip_id"),
      new CassandraFieldMappingConfig("[2]", "time"),
      new CassandraFieldMappingConfig("[3]", "x"),
      new CassandraFieldMappingConfig("[4]", "y"),
      new CassandraFieldMappingConfig("[5]", "time_id"),
      new CassandraFieldMappingConfig("[6]", "unique_id")
  );

  CassandraTargetConfig conf = new CassandraTargetConfig();
  conf.contactPoints.add(cassandra.getContainerIpAddress());
  conf.port = cassandra.getMappedPort(CASSANDRA_NATIVE_PORT);
  conf.protocolVersion = ProtocolVersion.V4;
  conf.authProviderOption = AuthProviderOption.NONE;
  conf.compression = CassandraCompressionCodec.NONE;
  conf.columnNames = fieldMappings;
  conf.qualifiedTableName = tableName;

  Target target = new CassandraTarget(conf);
  TargetRunner targetRunner = new TargetRunner.Builder(CassandraDTarget.class, target).build();

  List<Record> emptyBatch = ImmutableList.of();
  targetRunner.runInit();
  targetRunner.runWrite(emptyBatch);
  targetRunner.runDestroy();
}
 
Example #14
Source File: TestCassandraTarget.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test(expected = StageException.class)
public void testMalformedTableName() throws Exception {
  List<CassandraFieldMappingConfig> fieldMappings = ImmutableList.of(
      new CassandraFieldMappingConfig("/driver", "driver_id"),
      new CassandraFieldMappingConfig("/trip", "trip_id"),
      new CassandraFieldMappingConfig("/time", "time"),
      new CassandraFieldMappingConfig("/x", "x"),
      new CassandraFieldMappingConfig("/y", "y"),
      new CassandraFieldMappingConfig("/time_id", "time_id"),
      new CassandraFieldMappingConfig("/unique_id", "unique_id")
  );

  CassandraTargetConfig conf = new CassandraTargetConfig();
  conf.contactPoints.add(cassandra.getContainerIpAddress());
  conf.port = cassandra.getMappedPort(CASSANDRA_NATIVE_PORT);
  conf.protocolVersion = ProtocolVersion.V4;
  conf.authProviderOption = AuthProviderOption.NONE;
  conf.compression = CassandraCompressionCodec.NONE;
  conf.columnNames = fieldMappings;
  conf.qualifiedTableName = "tableName";

  Target target = new CassandraTarget(conf);
  TargetRunner targetRunner = new TargetRunner.Builder(CassandraDTarget.class, target).build();

  targetRunner.runInit();
  fail("should have thrown a StageException!");
}
 
Example #15
Source File: TimestampToLongCodec.java    From cassandra-jdbc-wrapper with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuffer serialize(Long paramT, ProtocolVersion paramProtocolVersion) throws InvalidTypeException {
	if (paramT == null) {
		return null;
	}
	return ByteBufferUtil.bytes(paramT);
}
 
Example #16
Source File: CassandraOperatorTest.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
@Test
public void testCassandraProtocolVersion()
{
  TestOutputOperator outputOperator = setupForOutputOperatorTest();
  outputOperator.getStore().setProtocolVersion("v2");

  outputOperator.setup(context);

  Configuration config = outputOperator.getStore().getCluster().getConfiguration();
  Assert.assertEquals("Procotol version was not set to V2.", ProtocolVersion.V2, config.getProtocolOptions().getProtocolVersion());
}
 
Example #17
Source File: BigDecimalToBigintCodec.java    From cassandra-jdbc-wrapper with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuffer serialize(BigDecimal paramT, ProtocolVersion paramProtocolVersion) throws InvalidTypeException {
	if (paramT == null) {
		return null;
	}
	return ByteBufferUtil.bytes(paramT.longValue());
}
 
Example #18
Source File: BytesBlobCodec.java    From cassandra-jdbc-driver with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException {
    if (bytes == null || bytes.remaining() == 0)
        return null;

    return bytes.duplicate().array();
}
 
Example #19
Source File: UniqueValueSerializationStrategyV2Impl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
private ByteBuffer serializeLogKey(UUID appUUID, String applicationType, UUID entityId, String entityType){

        List<Object> keys = new ArrayList<>(4);
        keys.add(appUUID);
        keys.add(applicationType);
        keys.add(entityId);
        keys.add(entityType);

        int size = 16+applicationType.getBytes().length+16+entityType.getBytes().length;

        // we always need to add length for the 2 byte short and 1 byte equality
        size += keys.size()*3;

        ByteBuffer stuff = ByteBuffer.allocate(size);

        for (Object key : keys) {

            ByteBuffer kb = DataType.serializeValue(key, ProtocolVersion.NEWEST_SUPPORTED);
            if (kb == null) {
                kb = ByteBuffer.allocate(0);
            }

            stuff.putShort((short) kb.remaining());
            stuff.put(kb.slice());
            stuff.put((byte) 0);


        }
        stuff.flip();
        return stuff;

    }
 
Example #20
Source File: CassandraClusterCreatorTest.java    From spring-cloud-connectors with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreateClusterWithConfig() throws Exception {

	CassandraServiceInfo info = new CassandraServiceInfo("local",
			Collections.singletonList("127.0.0.1"), 9142);

	CassandraClusterConfig config = new CassandraClusterConfig();
	config.setCompression(ProtocolOptions.Compression.NONE);
	config.setPoolingOptions(new PoolingOptions().setPoolTimeoutMillis(1234));
	config.setQueryOptions(new QueryOptions());
	config.setProtocolVersion(ProtocolVersion.NEWEST_SUPPORTED);
	config.setLoadBalancingPolicy(new RoundRobinPolicy());
	config.setReconnectionPolicy(new ConstantReconnectionPolicy(1));
	config.setRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE);
	config.setSocketOptions(new SocketOptions());

	Cluster cluster = creator.create(info, config);

	Configuration configuration = cluster.getConfiguration();

	assertThat(configuration.getProtocolOptions().getCompression(),
			is(config.getCompression()));
	assertThat(configuration.getQueryOptions(), is(config.getQueryOptions()));
	assertThat(configuration.getSocketOptions(), is(config.getSocketOptions()));

	Policies policies = configuration.getPolicies();
	assertThat(policies.getLoadBalancingPolicy(),
			is(config.getLoadBalancingPolicy()));
	assertThat(policies.getReconnectionPolicy(), is(config.getReconnectionPolicy()));
	assertThat(policies.getRetryPolicy(), is(config.getRetryPolicy()));
}
 
Example #21
Source File: LongToIntCodec.java    From cassandra-jdbc-wrapper with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuffer serialize(Integer paramT, ProtocolVersion paramProtocolVersion) throws InvalidTypeException {
	if (paramT == null) {
		return null;
	}
	return ByteBufferUtil.bytes(paramT);
}
 
Example #22
Source File: CassandraStore.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
private ProtocolVersion getCassandraProtocolVersion()
{
  switch (protocolVersion.toUpperCase()) {
    case "V1":
      return ProtocolVersion.V1;
    case "V2":
      return ProtocolVersion.V2;
    case "V3":
      return ProtocolVersion.V3;
    default:
      throw new RuntimeException("Unsupported Cassandra Protocol Version.");
  }
}
 
Example #23
Source File: StringTimestampCodec.java    From cassandra-jdbc-driver with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuffer serialize(String value, ProtocolVersion protocolVersion) {
    if (value == null) {
        return null;
    }

    if (value.indexOf(' ') == 10 && value.indexOf('Z') < 0) {
        StringBuilder builder = new StringBuilder(value).append('Z');
        builder.setCharAt(10, 'T');
        value = builder.toString();
    }

    return bigint().serializeNoBoxing(Instant.parse(value).getMillis(), protocolVersion);
}
 
Example #24
Source File: JavaSqlDateCodec.java    From cassandra-jdbc-driver with Apache License 2.0 5 votes vote down vote up
@Override
public Date deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) {
    if (bytes == null || bytes.remaining() == 0)
        return null;
    int unsigned = cint().deserializeNoBoxing(bytes, protocolVersion);
    int signed = fromUnsignedToSignedInt(unsigned);
    return new Date(EPOCH.plusDays(signed).toDate().getTime());
}
 
Example #25
Source File: JavaSqlDateCodec.java    From cassandra-jdbc-driver with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuffer serialize(Date value, ProtocolVersion protocolVersion) {
    if (value == null)
        return null;
    Days days = daysBetween(EPOCH, LocalDate.fromDateFields(value));
    int unsigned = fromSignedToUnsignedInt(days.getDays());
    return cint().serializeNoBoxing(unsigned, protocolVersion);
}
 
Example #26
Source File: StringTimeCodec.java    From cassandra-jdbc-driver with Apache License 2.0 5 votes vote down vote up
@Override
public String deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException {
    if (bytes == null || bytes.remaining() == 0)
        return null;
    long nanosOfDay = bigint().deserializeNoBoxing(bytes, protocolVersion);
    return LocalTime.fromMillisOfDay(nanosOfDay / 1000000).toString();
}
 
Example #27
Source File: UniqueValueSerializationStrategyV1Impl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
@Override
protected List<Object> deserializePartitionKey(ByteBuffer bb){


    /**
     *  List<Object> keys = new ArrayList<>(8);
        keys.add(0, appUUID);
        keys.add(1, applicationType);
        keys.add(2, appUUID);
        keys.add(3, applicationType);
        keys.add(4, entityType);
        keys.add(5, fieldType);
        keys.add(6, fieldName);
        keys.add(7, fieldValueString);

     */

    int count = 0;
    List<Object> stuff = new ArrayList<>();
    while(bb.hasRemaining()){
        ByteBuffer data = CQLUtils.getWithShortLength(bb);
        if(count == 0 || count == 2){
            stuff.add(DataType.uuid().deserialize(data.slice(), ProtocolVersion.NEWEST_SUPPORTED));
        }else{
            stuff.add(DataType.text().deserialize(data.slice(), ProtocolVersion.NEWEST_SUPPORTED));
        }
        byte equality = bb.get(); // we don't use this but take the equality byte off the buffer
        count++;
    }

    return stuff;

}
 
Example #28
Source File: JavaSqlTimeCodec.java    From cassandra-jdbc-driver with Apache License 2.0 5 votes vote down vote up
@Override
public Time deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException {
    if (bytes == null || bytes.remaining() == 0)
        return null;
    long nanosOfDay = bigint().deserializeNoBoxing(bytes, protocolVersion);
    return new Time(LocalTime.fromMillisOfDay(nanosOfDay / 1000000L).toDateTimeToday().getMillis());
}
 
Example #29
Source File: JavaSqlTimestampCodec.java    From cassandra-jdbc-driver with Apache License 2.0 5 votes vote down vote up
@Override
public Timestamp deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) {
    if (bytes == null || bytes.remaining() == 0)
        return null;
    long millis = bigint().deserializeNoBoxing(bytes, protocolVersion);
    return new Timestamp(millis);
}
 
Example #30
Source File: StringTimestampCodec.java    From cassandra-jdbc-driver with Apache License 2.0 5 votes vote down vote up
@Override
public String deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) {
    if (bytes == null || bytes.remaining() == 0)
        return null;
    long millis = bigint().deserializeNoBoxing(bytes, protocolVersion);
    return new Instant(millis).toString();
}