protected static String getPrimitiveAvroTypeFromCassandraType(DataType dataType) { // Map types from Cassandra to Avro where possible if (dataType.equals(DataType.ascii()) || dataType.equals(DataType.text()) || dataType.equals(DataType.varchar()) // Nonstandard types represented by this processor as a string || dataType.equals(DataType.timestamp()) || dataType.equals(DataType.timeuuid()) || dataType.equals(DataType.uuid()) || dataType.equals(DataType.inet()) || dataType.equals(DataType.varint())) { return "string"; } else if (dataType.equals(DataType.cboolean())) { return "boolean"; } else if (dataType.equals(DataType.cint())) { return "int"; } else if (dataType.equals(DataType.bigint()) || dataType.equals(DataType.counter())) { return "long"; } else if (dataType.equals(DataType.cfloat())) { return "float"; } else if (dataType.equals(DataType.cdouble())) { return "double"; } else if (dataType.equals(DataType.blob())) { return "bytes"; } else { throw new IllegalArgumentException("createSchema: Unknown Cassandra data type " + dataType.getName() + " cannot be converted to Avro type"); } }
@Override public void putLong( final MapScope scope, final String key, final Long value ) { Preconditions.checkNotNull( scope, "mapscope is required" ); Preconditions.checkNotNull( key, "key is required" ); Preconditions.checkNotNull( value, "value is required" ); Statement mapEntry = QueryBuilder.insertInto(MAP_ENTRIES_TABLE) .value("key", getMapEntryPartitionKey(scope, key)) .value("column1", DataType.cboolean().serialize(true, ProtocolVersion.NEWEST_SUPPORTED)) .value("value", DataType.bigint().serialize(value, ProtocolVersion.NEWEST_SUPPORTED)); session.execute(mapEntry); final int bucket = BUCKET_LOCATOR.getCurrentBucket( scope.getName() ); Statement mapKey; mapKey = QueryBuilder.insertInto(MAP_KEYS_TABLE) .value("key", getMapKeyPartitionKey(scope, bucket)) .value("column1", DataType.text().serialize(key, ProtocolVersion.NEWEST_SUPPORTED)) .value("value", DataType.cboolean().serialize(true, ProtocolVersion.NEWEST_SUPPORTED)); session.execute(mapKey); }
} else if (dataType.equals(DataType.cboolean())) { return row.getBool(i);
} else if (mainType.equals(DataType.cboolean())) { statement.setBool(paramIndex, (boolean) typeCodec.parse(paramValue));
@Override public void putUuid( final MapScope scope, final String key, final UUID putUuid ) { Preconditions.checkNotNull( scope, "mapscope is required" ); Preconditions.checkNotNull( key, "key is required" ); Preconditions.checkNotNull( putUuid, "value is required" ); final BatchStatement batchStatement = new BatchStatement(); batchStatement.add(QueryBuilder.insertInto(MAP_ENTRIES_TABLE) .value("key", getMapEntryPartitionKey(scope, key)) .value("column1", DataType.cboolean().serialize(true, ProtocolVersion.NEWEST_SUPPORTED)) .value("value", DataType.uuid().serialize(putUuid, ProtocolVersion.NEWEST_SUPPORTED))); final int bucket = BUCKET_LOCATOR.getCurrentBucket( scope.getName() ); batchStatement.add(QueryBuilder.insertInto(MAP_KEYS_TABLE) .value("key", getMapKeyPartitionKey(scope, bucket)) .value("column1", DataType.text().serialize(key, ProtocolVersion.NEWEST_SUPPORTED)) .value("value", DataType.serializeValue(null, ProtocolVersion.NEWEST_SUPPORTED))); session.execute(batchStatement); }
private BooleanCodec() { super(DataType.cboolean()); }
.using(timeToLive) .value("key", getMapEntryPartitionKey(scope, key)) .value("column1", DataType.cboolean().serialize(true, ProtocolVersion.NEWEST_SUPPORTED)) .value("value", DataType.text().serialize(value, ProtocolVersion.NEWEST_SUPPORTED))); .value("key", getMapKeyPartitionKey(scope, bucket)) .value("column1", DataType.text().serialize(key, ProtocolVersion.NEWEST_SUPPORTED)) .value("value", DataType.cboolean().serialize(true, ProtocolVersion.NEWEST_SUPPORTED))); }else{ .value("column1", DataType.cboolean().serialize(true, ProtocolVersion.NEWEST_SUPPORTED)) .value("value", DataType.text().serialize(value, ProtocolVersion.NEWEST_SUPPORTED))); .value("key", getMapKeyPartitionKey(scope, bucket)) .value("column1", DataType.text().serialize(key, ProtocolVersion.NEWEST_SUPPORTED)) .value("value", DataType.cboolean().serialize(true, ProtocolVersion.NEWEST_SUPPORTED)));
private static boolean checkWasApplied( List<ByteBuffer> firstRow, ColumnDefinitions metadata, ProtocolVersion protocolVersion) { // If the column is not present or not a boolean, we assume the query // was not a conditional statement, and therefore return true. if (firstRow == null) return true; int[] is = metadata.findAllIdx("[applied]"); if (is == null) return true; int i = is[0]; if (!DataType.cboolean().equals(metadata.getType(i))) return true; // Otherwise return the value of the column ByteBuffer value = firstRow.get(i); if (value == null || value.remaining() == 0) return false; return TypeCodec.cboolean().deserializeNoBoxing(value, protocolVersion); } }
@Override public void init(CassandraSessionPool.Session session) { ImmutableMap<HugeKeys, DataType> pkeys = ImmutableMap.of( HugeKeys.ID, DATATYPE_SL ); ImmutableMap<HugeKeys, DataType> ckeys = ImmutableMap.of(); ImmutableMap<HugeKeys, DataType> columns = ImmutableMap .<HugeKeys, DataType>builder() .put(HugeKeys.NAME, DataType.text()) .put(HugeKeys.ID_STRATEGY, DataType.tinyint()) .put(HugeKeys.PRIMARY_KEYS, DataType.list(DATATYPE_PK)) .put(HugeKeys.NULLABLE_KEYS, DataType.set(DATATYPE_PK)) .put(HugeKeys.INDEX_LABELS, DataType.set(DATATYPE_IL)) .put(HugeKeys.PROPERTIES, DataType.set(DATATYPE_PK)) .put(HugeKeys.ENABLE_LABEL_INDEX, DataType.cboolean()) .put(HugeKeys.USER_DATA, DATATYPE_UD) .put(HugeKeys.STATUS, DataType.tinyint()) .build(); this.createTable(session, pkeys, ckeys, columns); this.createIndex(session, NAME_INDEX, HugeKeys.NAME); } }
@Override public void init(CassandraSessionPool.Session session) { ImmutableMap<HugeKeys, DataType> pkeys = ImmutableMap.of( HugeKeys.ID, DATATYPE_SL ); ImmutableMap<HugeKeys, DataType> ckeys = ImmutableMap.of(); ImmutableMap<HugeKeys, DataType> columns = ImmutableMap .<HugeKeys, DataType>builder() .put(HugeKeys.NAME, DataType.text()) .put(HugeKeys.FREQUENCY, DataType.tinyint()) .put(HugeKeys.SOURCE_LABEL, DATATYPE_SL) .put(HugeKeys.TARGET_LABEL, DATATYPE_SL) .put(HugeKeys.SORT_KEYS, DataType.list(DATATYPE_PK)) .put(HugeKeys.NULLABLE_KEYS, DataType.set(DATATYPE_PK)) .put(HugeKeys.INDEX_LABELS, DataType.set(DATATYPE_IL)) .put(HugeKeys.PROPERTIES, DataType.set(DATATYPE_PK)) .put(HugeKeys.ENABLE_LABEL_INDEX, DataType.cboolean()) .put(HugeKeys.USER_DATA, DATATYPE_UD) .put(HugeKeys.STATUS, DataType.tinyint()) .build(); this.createTable(session, pkeys, ckeys, columns); this.createIndex(session, NAME_INDEX, HugeKeys.NAME); } }
private BooleanCodec() { super(DataType.cboolean()); }
.put(DataType.bigint(), Long.MAX_VALUE) .put(DataType.blob(), Bytes.fromHexString("0xCAFE")) .put(DataType.cboolean(), Boolean.TRUE) .put(DataType.decimal(), new BigDecimal("12.3E+7")) .put(DataType.cdouble(), Double.MAX_VALUE)
private BooleanCodec() { super(DataType.cboolean()); }
cluster() .getMetadata() .newTupleType(DataType.ascii(), DataType.cint(), DataType.cboolean());
assertThat(rs.getColumnDefinitions()) .hasSize(1) .containsVariable("[applied]", DataType.cboolean());
@DataProvider public static Object[][] cqlAndValue() { return new Object[][] { {DataType.blob(), ByteBuffer.allocate(0), TypeCodec.blob()}, {DataType.cboolean(), true, TypeCodec.cboolean()}, {DataType.smallint(), (short) 42, TypeCodec.smallInt()}, {DataType.tinyint(), (byte) 42, TypeCodec.tinyInt()}, {DataType.cint(), 42, TypeCodec.cint()}, {DataType.bigint(), 42L, TypeCodec.bigint()}, {DataType.counter(), 42L, TypeCodec.counter()}, {DataType.cdouble(), 42D, TypeCodec.cdouble()}, {DataType.cfloat(), 42F, TypeCodec.cfloat()}, {DataType.varint(), new BigInteger("1234"), TypeCodec.varint()}, {DataType.decimal(), new BigDecimal("123.45"), TypeCodec.decimal()}, {DataType.varchar(), "foo", TypeCodec.varchar()}, {DataType.ascii(), "foo", TypeCodec.ascii()}, {DataType.timestamp(), new Date(42), TypeCodec.timestamp()}, {DataType.date(), LocalDate.fromDaysSinceEpoch(42), TypeCodec.date()}, {DataType.time(), 42L, TypeCodec.time()}, {DataType.uuid(), UUID.randomUUID(), TypeCodec.uuid()}, {DataType.timeuuid(), UUID.randomUUID(), TypeCodec.timeUUID()}, {DataType.inet(), mock(InetAddress.class), TypeCodec.inet()}, {DataType.duration(), Duration.from("1mo2d3h"), TypeCodec.duration()} }; }
@DataProvider public static Object[][] cql() { return new Object[][] { {DataType.blob(), TypeCodec.blob()}, {DataType.cboolean(), TypeCodec.cboolean()}, {DataType.smallint(), TypeCodec.smallInt()}, {DataType.tinyint(), TypeCodec.tinyInt()}, {DataType.cint(), TypeCodec.cint()}, {DataType.bigint(), TypeCodec.bigint()}, {DataType.counter(), TypeCodec.counter()}, {DataType.cdouble(), TypeCodec.cdouble()}, {DataType.cfloat(), TypeCodec.cfloat()}, {DataType.varint(), TypeCodec.varint()}, {DataType.decimal(), TypeCodec.decimal()}, {DataType.varchar(), TypeCodec.varchar()}, {DataType.ascii(), TypeCodec.ascii()}, {DataType.timestamp(), TypeCodec.timestamp()}, {DataType.date(), TypeCodec.date()}, {DataType.time(), TypeCodec.time()}, {DataType.uuid(), TypeCodec.uuid()}, {DataType.timeuuid(), TypeCodec.timeUUID()}, {DataType.inet(), TypeCodec.inet()}, {DataType.duration(), TypeCodec.duration()} }; }
@DataProvider public static Object[][] cqlAndJava() { return new Object[][] { {DataType.blob(), ByteBuffer.class, TypeCodec.blob()}, {DataType.cboolean(), Boolean.class, TypeCodec.cboolean()}, {DataType.smallint(), Short.class, TypeCodec.smallInt()}, {DataType.tinyint(), Byte.class, TypeCodec.tinyInt()}, {DataType.cint(), Integer.class, TypeCodec.cint()}, {DataType.bigint(), Long.class, TypeCodec.bigint()}, {DataType.counter(), Long.class, TypeCodec.counter()}, {DataType.cdouble(), Double.class, TypeCodec.cdouble()}, {DataType.cfloat(), Float.class, TypeCodec.cfloat()}, {DataType.varint(), BigInteger.class, TypeCodec.varint()}, {DataType.decimal(), BigDecimal.class, TypeCodec.decimal()}, {DataType.varchar(), String.class, TypeCodec.varchar()}, {DataType.ascii(), String.class, TypeCodec.ascii()}, {DataType.timestamp(), Date.class, TypeCodec.timestamp()}, {DataType.date(), LocalDate.class, TypeCodec.date()}, {DataType.time(), Long.class, TypeCodec.time()}, {DataType.uuid(), UUID.class, TypeCodec.uuid()}, {DataType.timeuuid(), UUID.class, TypeCodec.timeUUID()}, {DataType.inet(), InetAddress.class, TypeCodec.inet()}, {DataType.duration(), Duration.class, TypeCodec.duration()} }; }
@Test(groups = "short") public void should_parse_native_types() { assertThat(parse("ascii", cluster(), null, null, null, false, false)).isEqualTo(ascii()); assertThat(parse("bigint", cluster(), null, null, null, false, false)).isEqualTo(bigint()); assertThat(parse("blob", cluster(), null, null, null, false, false)).isEqualTo(blob()); assertThat(parse("boolean", cluster(), null, null, null, false, false)).isEqualTo(cboolean()); assertThat(parse("counter", cluster(), null, null, null, false, false)).isEqualTo(counter()); assertThat(parse("decimal", cluster(), null, null, null, false, false)).isEqualTo(decimal()); assertThat(parse("double", cluster(), null, null, null, false, false)).isEqualTo(cdouble()); assertThat(parse("float", cluster(), null, null, null, false, false)).isEqualTo(cfloat()); assertThat(parse("inet", cluster(), null, null, null, false, false)).isEqualTo(inet()); assertThat(parse("int", cluster(), null, null, null, false, false)).isEqualTo(cint()); assertThat(parse("text", cluster(), null, null, null, false, false)).isEqualTo(text()); assertThat(parse("varchar", cluster(), null, null, null, false, false)).isEqualTo(varchar()); assertThat(parse("timestamp", cluster(), null, null, null, false, false)) .isEqualTo(timestamp()); assertThat(parse("date", cluster(), null, null, null, false, false)).isEqualTo(date()); assertThat(parse("time", cluster(), null, null, null, false, false)).isEqualTo(time()); assertThat(parse("uuid", cluster(), null, null, null, false, false)).isEqualTo(uuid()); assertThat(parse("varint", cluster(), null, null, null, false, false)).isEqualTo(varint()); assertThat(parse("timeuuid", cluster(), null, null, null, false, false)).isEqualTo(timeuuid()); assertThat(parse("tinyint", cluster(), null, null, null, false, false)).isEqualTo(tinyint()); assertThat(parse("smallint", cluster(), null, null, null, false, false)).isEqualTo(smallint()); }