org.apache.cassandra.db.marshal.DoubleType Java Examples

The following examples show how to use org.apache.cassandra.db.marshal.DoubleType. 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: CassandraTypeConverterTest.java    From debezium-incubator with Apache License 2.0 6 votes vote down vote up
@Test
public void testMap() {
    // map from ASCII to Double
    // test non-frozen
    DataType mapType = DataType.map(DataType.ascii(), DataType.cdouble());
    AbstractType<?> convertedType = CassandraTypeConverter.convert(mapType);

    MapType<?, ?> expectedType = MapType.getInstance(AsciiType.instance, DoubleType.instance, true);
    Assert.assertEquals(expectedType, convertedType);

    // test frozen
    mapType = DataType.map(DataType.ascii(), DataType.cdouble(), true);
    convertedType = CassandraTypeConverter.convert(mapType);
    expectedType = MapType.getInstance(AsciiType.instance, DoubleType.instance, false);
    Assert.assertEquals(expectedType, convertedType);
    Assert.assertTrue("Expected convertType to be frozen", convertedType.isFrozenCollection());
}
 
Example #2
Source File: CassandraTypeDeserializerTest.java    From debezium-incubator with Apache License 2.0 6 votes vote down vote up
@Test
public void testMapType() {
    Map<String, Double> expectedMap = new HashMap<>();
    expectedMap.put("foo", 1D);
    expectedMap.put("bar", 50D);

    // non-frozen
    MapType<String, Double> nonFrozenMapType = MapType.getInstance(AsciiType.instance, DoubleType.instance, true);
    ByteBuffer serializedMap = nonFrozenMapType.decompose(expectedMap);
    Object deserializedMap = CassandraTypeDeserializer.deserialize(nonFrozenMapType, serializedMap);
    Assert.assertEquals(expectedMap, deserializedMap);

    // frozen
    MapType<String, Double> frozenMapType = MapType.getInstance(AsciiType.instance, DoubleType.instance, false);
    serializedMap = frozenMapType.decompose(expectedMap);
    deserializedMap = CassandraTypeDeserializer.deserialize(frozenMapType, serializedMap);
    Assert.assertEquals(expectedMap, deserializedMap);
}
 
Example #3
Source File: CellValidatorTest.java    From deep-spark with Apache License 2.0 6 votes vote down vote up
public void testValidatorClassToKind() {
    assertEquals(Kind.validatorClassToKind(null), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(TimeUUIDType.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(UTF8Type.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(Int32Type.class), Kind.NOT_A_COLLECTION);

    assertEquals(Kind.validatorClassToKind(BooleanType.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(TimestampType.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(DecimalType.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(LongType.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(DoubleType.class), Kind.NOT_A_COLLECTION);

    assertEquals(Kind.validatorClassToKind(FloatType.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(InetAddressType.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(IntegerType.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(UUIDType.class), Kind.NOT_A_COLLECTION);

    assertEquals(Kind.validatorClassToKind(SetType.class), Kind.SET);
    assertEquals(Kind.validatorClassToKind(ListType.class), Kind.LIST);
    assertEquals(Kind.validatorClassToKind(MapType.class), Kind.MAP);
}
 
Example #4
Source File: LazyCassandraUtils.java    From Hive-Cassandra with Apache License 2.0 6 votes vote down vote up
public static AbstractType getCassandraType(PrimitiveObjectInspector oi) {
  switch (oi.getPrimitiveCategory()) {
  case BOOLEAN:
    return BooleanType.instance;
  case INT:
    return Int32Type.instance;
  case LONG:
    return LongType.instance;
  case FLOAT:
    return FloatType.instance;
  case DOUBLE:
    return DoubleType.instance;
  case STRING:
    return UTF8Type.instance;
  case BYTE:
  case SHORT:
  case BINARY:
    return BytesType.instance;
  case TIMESTAMP:
    return DateType.instance;
  default:
    throw new RuntimeException("Hive internal error.");

  }
}
 
Example #5
Source File: CassandraTypeConverterTest.java    From debezium-incubator with Apache License 2.0 5 votes vote down vote up
@Test
public void testDouble() {
    DataType doubleType = DataType.cdouble();
    AbstractType<?> convertedType = CassandraTypeConverter.convert(doubleType);

    DoubleType expectedType = DoubleType.instance;

    Assert.assertEquals(expectedType, convertedType);
}
 
Example #6
Source File: CassandraTypeConverterTest.java    From debezium-incubator with Apache License 2.0 4 votes vote down vote up
@Test
public void testUdt() {
    // we can't create a UserType directly, and there isn't really a good way to make one some other way, so...
    // mock it!
    UserType userType = Mockito.mock(UserType.class);
    Mockito.when(userType.getName()).thenReturn(DataType.Name.UDT);
    Mockito.when(userType.getTypeName()).thenReturn("FooType");
    Mockito.when(userType.getKeyspace()).thenReturn("barspace");
    List<String> fieldNames = new ArrayList<>();
    fieldNames.add("asciiField");
    fieldNames.add("doubleField");
    Mockito.when(userType.getFieldNames()).thenReturn(fieldNames);
    Mockito.when(userType.getFieldType("asciiField")).thenReturn(DataType.ascii());
    Mockito.when(userType.getFieldType("doubleField")).thenReturn(DataType.cdouble());
    Mockito.when(userType.isFrozen()).thenReturn(false, true); // cheaty way to test non-frozen and then frozen path.

    ByteBuffer expectedTypeName = ByteBuffer.wrap("FooType".getBytes(Charset.defaultCharset()));
    List<FieldIdentifier> expectedFieldIdentifiers = new ArrayList<>();
    expectedFieldIdentifiers.add(new FieldIdentifier(ByteBuffer.wrap("asciiField".getBytes(Charset.defaultCharset()))));
    expectedFieldIdentifiers.add(new FieldIdentifier(ByteBuffer.wrap("doubleField".getBytes(Charset.defaultCharset()))));
    List<AbstractType<?>> expectedFieldTypes = new ArrayList<>();
    expectedFieldTypes.add(AsciiType.instance);
    expectedFieldTypes.add(DoubleType.instance);

    // non-frozen
    org.apache.cassandra.db.marshal.UserType expectedAbstractType = new org.apache.cassandra.db.marshal.UserType("barspace",
            expectedTypeName,
            expectedFieldIdentifiers,
            expectedFieldTypes,
            true);
    AbstractType<?> convertedType = CassandraTypeConverter.convert(userType);
    Assert.assertEquals(expectedAbstractType, convertedType);

    // frozen
    expectedAbstractType = new org.apache.cassandra.db.marshal.UserType("barspace",
            expectedTypeName,
            expectedFieldIdentifiers,
            expectedFieldTypes,
            false);
    convertedType = CassandraTypeConverter.convert(userType);
    Assert.assertEquals(expectedAbstractType, convertedType);
}
 
Example #7
Source File: CassandraTypeDeserializerTest.java    From debezium-incubator with Apache License 2.0 4 votes vote down vote up
@Test
public void testUserType() {
    // this is slightly complicated, so we're testing in two parts:
    // first, explicitly test for schema correctness
    ByteBuffer expectedTypeName = ByteBuffer.wrap("FooType".getBytes(Charset.defaultCharset()));
    List<FieldIdentifier> expectedFieldIdentifiers = new ArrayList<>();
    expectedFieldIdentifiers.add(new FieldIdentifier(ByteBuffer.wrap("asciiField".getBytes(Charset.defaultCharset()))));
    expectedFieldIdentifiers.add(new FieldIdentifier(ByteBuffer.wrap("doubleField".getBytes(Charset.defaultCharset()))));
    expectedFieldIdentifiers.add(new FieldIdentifier(ByteBuffer.wrap("durationField".getBytes(Charset.defaultCharset()))));
    // testing duration to make sure that recursive deserialization works correctly
    List<AbstractType<?>> expectedFieldTypes = new ArrayList<>();
    expectedFieldTypes.add(AsciiType.instance);
    expectedFieldTypes.add(DoubleType.instance);
    expectedFieldTypes.add(DurationType.instance);
    UserType userType = new UserType("barspace",
            expectedTypeName,
            expectedFieldIdentifiers,
            expectedFieldTypes,
            true);

    Schema userSchema = CassandraTypeDeserializer.getSchemaBuilder(userType).build();

    long expectedNanoDuration = (30 + 2) * ChronoUnit.DAYS.getDuration().toNanos() + 3;

    Struct expectedUserTypeData = new Struct(userSchema)
            .put("asciiField", "foobar")
            .put("doubleField", 1.5d)
            .put("durationField", expectedNanoDuration);

    Map<String, Object> jsonObject = new HashMap<>(3);
    jsonObject.put("\"asciiField\"", "foobar");
    jsonObject.put("\"doubleField\"", 1.5d);
    jsonObject.put("\"durationField\"", DurationType.instance.getSerializer().toString(Duration.newInstance(1, 2, 3)));
    Term userTypeObject = userType.fromJSONObject(jsonObject);

    ByteBuffer buffer = userTypeObject.bindAndGet(QueryOptions.DEFAULT);

    ByteBuffer serializedUserTypeObject = userType.decompose(buffer);

    Object deserializedUserTypeObject = CassandraTypeDeserializer.deserialize(userType, serializedUserTypeObject);

    Assert.assertEquals(expectedUserTypeData, deserializedUserTypeObject);
}
 
Example #8
Source File: Doubles.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public Doubles(String name, GeneratorConfig config)
{
    super(DoubleType.instance, config, name, Double.class);
}
 
Example #9
Source File: CassandraTypeDeserializerTest.java    From debezium-incubator with Apache License 2.0 3 votes vote down vote up
@Test
public void testDoubleType() {
    Double expectedDouble = 100.5;

    ByteBuffer serializedDouble = DoubleType.instance.decompose(expectedDouble);

    Object deserializedDouble = CassandraTypeDeserializer.deserialize(DoubleType.instance, serializedDouble);

    Assert.assertEquals(expectedDouble, deserializedDouble);
}