Java Code Examples for com.datastax.driver.core.ProtocolVersion#V4

The following examples show how to use com.datastax.driver.core.ProtocolVersion#V4 . 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: 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 2
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 3
Source File: TestCassandraTarget.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Test
public void testWriteSingleRecord() 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]", "dt"),
      new CassandraFieldMappingConfig("[6]", "ts"),
      new CassandraFieldMappingConfig("[7]", "time_id"),
      new CassandraFieldMappingConfig("[8]", "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();

  long now = System.currentTimeMillis();
  LocalDate dt = LocalDate.fromMillisSinceEpoch(now);
  Date ts = new Date();

  Record record = RecordCreator.create();
  List<Field> fields = new ArrayList<>();
  fields.add(Field.create(1));
  fields.add(Field.create(2));
  fields.add(Field.create(3));
  fields.add(Field.create(4.0));
  fields.add(Field.create(5.0));
  fields.add(Field.create(Field.Type.DATE, new Date(dt.getMillisSinceEpoch())));
  fields.add(Field.create(Field.Type.DATETIME, ts));
  fields.add(Field.create(SAMPLE_TIMEUUID));
  fields.add(Field.create(SAMPLE_UUID));
  record.set(Field.create(fields));

  List<Record> singleRecord = ImmutableList.of(record);
  targetRunner.runInit();
  targetRunner.runWrite(singleRecord);

  // Should not be any error records.
  Assert.assertTrue(targetRunner.getErrorRecords().isEmpty());
  Assert.assertTrue(targetRunner.getErrors().isEmpty());

  targetRunner.runDestroy();

  ResultSet resultSet = session.execute("SELECT * FROM test.trips");
  List<Row> allRows = resultSet.all();
  Assert.assertEquals(1, allRows.size());

  Row row = allRows.get(0);
  Assert.assertEquals(1, row.getInt("driver_id"));
  Assert.assertEquals(2, row.getInt("trip_id"));
  Assert.assertEquals(3, row.getInt("time"));
  Assert.assertEquals(4.0, row.getDouble("x"), EPSILON);
  Assert.assertEquals(5.0, row.getDouble("y"), EPSILON);
  Assert.assertEquals(dt, row.getDate("dt"));
  Assert.assertEquals(ts, row.getTimestamp("ts"));
  Assert.assertEquals(SAMPLE_TIMEUUID, row.getUUID("time_id").toString());
  Assert.assertEquals(SAMPLE_UUID, row.getUUID("unique_id").toString());
}
 
Example 4
Source File: TestCassandraTarget.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Test
public void testCollectionTypes() throws InterruptedException, StageException {
  final String tableName = "test.collections";
  List<CassandraFieldMappingConfig> fieldMappings = ImmutableList.of(
      new CassandraFieldMappingConfig("[0]", "id"),
      new CassandraFieldMappingConfig("[1]", "a_list"),
      new CassandraFieldMappingConfig("[2]", "a_map")
  );

  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();

  Record record = RecordCreator.create();
  List<Field> fields = new ArrayList<>();
  fields.add(Field.create(1));
  fields.add(Field.create(ImmutableList.of(Field.create(2))));
  fields.add(Field.create(ImmutableMap.of("3", Field.create(4))));
  record.set(Field.create(fields));

  List<Record> singleRecord = ImmutableList.of(record);
  targetRunner.runInit();
  targetRunner.runWrite(singleRecord);

  // Should not be any error records.
  Assert.assertTrue(targetRunner.getErrorRecords().isEmpty());
  Assert.assertTrue(targetRunner.getErrors().isEmpty());

  targetRunner.runDestroy();

  ResultSet resultSet = session.execute("SELECT * FROM test.collections");
  List<Row> allRows = resultSet.all();
  assert (1 == allRows.size());

  Row row = allRows.get(0);
  assert (row.getInt("id") == 1);
  assert (row.getList("a_list", Integer.class).contains(2));
  assert (row.getMap("a_map", String.class, Integer.class).containsKey("3"));
}
 
Example 5
Source File: TestCassandraTarget.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Test
public void testWriteRecordsOnErrorDiscard() throws Exception {
  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)
      .setOnRecordError(OnRecordError.DISCARD)
      .build();

  Record record = RecordCreator.create();
  List<Field> fields = new ArrayList<>();
  fields.add(Field.create(1.3));
  fields.add(Field.create(2));
  fields.add(Field.create(3));
  fields.add(Field.create(4.0));
  fields.add(Field.create(5.0));
  fields.add(Field.create(SAMPLE_TIMEUUID));
  fields.add(Field.create(SAMPLE_UUID));
  record.set(Field.create(fields));

  List<Record> singleRecord = ImmutableList.of(record);
  targetRunner.runInit();
  targetRunner.runWrite(singleRecord);

  // Should not be any error records if we are discarding.
  Assert.assertTrue(targetRunner.getErrorRecords().isEmpty());
  Assert.assertTrue(targetRunner.getErrors().isEmpty());

  targetRunner.runDestroy();

  ResultSet resultSet = session.execute("SELECT * FROM test.trips");
  List<Row> allRows = resultSet.all();
  Assert.assertEquals(0, allRows.size());
}
 
Example 6
Source File: TestCassandraTarget.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Test
public void testWriteRecordsOnErrorToError() throws Exception {
  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)
      .setOnRecordError(OnRecordError.TO_ERROR)
      .build();

  Record record = RecordCreator.create();
  List<Field> fields = new ArrayList<>();
  fields.add(Field.create(1.3));
  fields.add(Field.create(2));
  fields.add(Field.create(3));
  // intentionally passing doubles where these values are expected to be ints triggers errors!
  fields.add(Field.create(4.0));
  fields.add(Field.create(5.0));
  fields.add(Field.create(SAMPLE_TIMEUUID));
  fields.add(Field.create(SAMPLE_UUID));
  record.set(Field.create(fields));

  List<Record> singleRecord = ImmutableList.of(record);
  targetRunner.runInit();
  targetRunner.runWrite(singleRecord);

  // Should have gone to error pipeline
  Assert.assertEquals(1, targetRunner.getErrorRecords().size());
  Assert.assertTrue(targetRunner.getErrors().isEmpty());

  targetRunner.runDestroy();

  ResultSet resultSet = session.execute("SELECT * FROM test.trips");
  List<Row> allRows = resultSet.all();
  Assert.assertEquals(0, allRows.size());
}
 
Example 7
Source File: TestCassandraTarget.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Test(expected = StageException.class)
public void testWriteRecordsOnErrorStopPipeline() throws Exception {
  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)
      .setOnRecordError(OnRecordError.STOP_PIPELINE)
      .build();

  Record record = RecordCreator.create();
  List<Field> fields = new ArrayList<>();
  fields.add(Field.create(1.3));
  fields.add(Field.create(2));
  fields.add(Field.create(3));
  fields.add(Field.create(4.0));
  fields.add(Field.create(5.0));
  fields.add(Field.create(SAMPLE_TIMEUUID));
  fields.add(Field.create(SAMPLE_UUID));
  record.set(Field.create(fields));

  List<Record> singleRecord = ImmutableList.of(record);
  targetRunner.runInit();
  targetRunner.runWrite(singleRecord);

  // Should have gone to error pipeline
  Assert.assertEquals(1, targetRunner.getErrorRecords().size());
  Assert.assertTrue(targetRunner.getErrors().isEmpty());

  targetRunner.runDestroy();

  ResultSet resultSet = session.execute("SELECT * FROM test.trips");
  List<Row> allRows = resultSet.all();
  Assert.assertEquals(0, allRows.size());
}
 
Example 8
Source File: TestCassandraTarget.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Test
public void testWriteRecordWithMissingFields() throws InterruptedException, StageException {
  final String tableName = "test.trips";
  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();

  Record record = RecordCreator.create();
  Map<String, Field> fields = new ImmutableMap.Builder<String, Field>()
      .put("driver", Field.create(1))
      .put("trip", Field.create(2))
      .put("time", Field.create(3))
      .put("y", Field.create(5.0))
      .put("time_id", Field.create(SAMPLE_TIMEUUID))
      .put("unique_id", Field.create(SAMPLE_UUID))
      .build();
  record.set(Field.create(fields));

  List<Record> singleRecord = ImmutableList.of(record);
  targetRunner.runInit();
  targetRunner.runWrite(singleRecord);

  // Should not be any error records.
  Assert.assertTrue(targetRunner.getErrorRecords().isEmpty());
  Assert.assertTrue(targetRunner.getErrors().isEmpty());

  targetRunner.runDestroy();

  ResultSet resultSet = session.execute("SELECT * FROM test.trips");
  List<Row> allRows = resultSet.all();
  Assert.assertEquals(1, allRows.size());

  Row row = allRows.get(0);
  Assert.assertEquals(1, row.getInt("driver_id"));
  Assert.assertEquals(2, row.getInt("trip_id"));
  Assert.assertEquals(3, row.getInt("time"));
  Assert.assertEquals(null, row.getBytesUnsafe("x"));
  Assert.assertEquals(5.0, row.getDouble("y"), EPSILON);
  Assert.assertEquals(SAMPLE_TIMEUUID, row.getUUID("time_id").toString());
  Assert.assertEquals(SAMPLE_UUID, row.getUUID("unique_id").toString());
}
 
Example 9
Source File: TestCassandraTarget.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Test
public void testInternalSubBatching() throws Exception {
  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;
  conf.maxBatchSize = 35;

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

  List<Record> records = new ArrayList<>();
  for (int i = 0; i < 1000; i++) {
    Record record = RecordCreator.create();
    List<Field> fields = new ArrayList<>();
    fields.add(Field.create(i));
    fields.add(Field.create(2));
    fields.add(Field.create(3));
    fields.add(Field.create(4.0));
    fields.add(Field.create(5.0));
    fields.add(Field.create(SAMPLE_TIMEUUID));
    fields.add(Field.create(SAMPLE_UUID));
    record.set(Field.create(fields));
    records.add(record);
  }
  targetRunner.runInit();
  targetRunner.runWrite(records);

  // Should not be any error records.
  Assert.assertTrue(targetRunner.getErrorRecords().isEmpty());
  Assert.assertTrue(targetRunner.getErrors().isEmpty());

  targetRunner.runDestroy();

  // simple verification that there are as many records as expected
  ResultSet resultSet = session.execute("SELECT * FROM test.trips");
  List<Row> allRows = resultSet.all();
  Assert.assertEquals(1000, allRows.size());
}
 
Example 10
Source File: TestCassandraTarget.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Test
public void testWriteNullValuedColumns() throws Exception {
  final String tableName = "test.test_null_values";
  List<CassandraFieldMappingConfig> fieldMappings = ImmutableList.of(new CassandraFieldMappingConfig("[0]", "a"),
      new CassandraFieldMappingConfig("[1]", "b")
  );

  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();

  Record record = RecordCreator.create();
  List<Field> fields = new ArrayList<>();
  fields.add(Field.create("abc"));
  fields.add(Field.create(Field.Type.STRING, null));
  record.set(Field.create(fields));

  List<Record> singleRecord = ImmutableList.of(record);
  targetRunner.runInit();
  targetRunner.runWrite(singleRecord);

  // Should not be any error records.
  Assert.assertTrue(targetRunner.getErrorRecords().isEmpty());
  Assert.assertTrue(targetRunner.getErrors().isEmpty());

  targetRunner.runDestroy();

  ResultSet resultSet = session.execute("SELECT * FROM test.test_null_values");
  List<Row> allRows = resultSet.all();
  Assert.assertEquals(1, allRows.size());

  Row row = allRows.get(0);
  Assert.assertEquals("abc", row.getString("a"));
  Assert.assertEquals(null, row.getString("b"));
}