Java Code Examples for com.datastax.driver.core.LocalDate#fromMillisSinceEpoch()

The following examples show how to use com.datastax.driver.core.LocalDate#fromMillisSinceEpoch() . 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: LocalDateAsDateCodec.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Override
protected LocalDate serialize(Date d) { return LocalDate.fromMillisSinceEpoch(d.getTime()); }
 
Example 2
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());
}