use of com.datastax.oss.dsbulk.codecs.api.ConversionContext in project dsbulk by datastax.
the class TemporalConvertingCodecsProvider method maybeProvide.
@NonNull
@Override
public Optional<ConvertingCodec<?, ?>> maybeProvide(@NonNull DataType cqlType, @NonNull GenericType<?> externalJavaType, @NonNull ConvertingCodecFactory codecFactory, boolean rootCodec) {
ConversionContext context = codecFactory.getContext();
if (externalJavaType.isSubtypeOf(TEMPORAL_GENERIC_TYPE) && isTemporal(cqlType)) {
@SuppressWarnings("unchecked") Class<Temporal> fromTemporalType = (Class<Temporal>) externalJavaType.getRawType();
if (cqlType == DataTypes.DATE) {
return Optional.of(new TemporalToTemporalCodec<>(fromTemporalType, TypeCodecs.DATE, context.getAttribute(TIME_ZONE), context.getAttribute(EPOCH)));
}
if (cqlType == DataTypes.TIME) {
return Optional.of(new TemporalToTemporalCodec<>(fromTemporalType, TypeCodecs.TIME, context.getAttribute(TIME_ZONE), context.getAttribute(EPOCH)));
}
if (cqlType == DataTypes.TIMESTAMP) {
return Optional.of(new TemporalToTemporalCodec<>(fromTemporalType, TypeCodecs.TIMESTAMP, context.getAttribute(TIME_ZONE), context.getAttribute(EPOCH)));
}
}
if (externalJavaType.isSubtypeOf(TEMPORAL_GENERIC_TYPE) && isUUID(cqlType)) {
TypeCodec<UUID> uuidCodec = codecFactory.getCodecRegistry().codecFor(cqlType);
@SuppressWarnings({ "unchecked", "rawtypes" }) TemporalToTemporalCodec<TemporalAccessor, Instant> instantCodec = (TemporalToTemporalCodec) codecFactory.createConvertingCodec(DataTypes.TIMESTAMP, externalJavaType, false);
return Optional.of(new TemporalToUUIDCodec<>(uuidCodec, instantCodec, context.getAttribute(TIME_UUID_GENERATOR)));
}
if (externalJavaType.isSubtypeOf(DATE_GENERIC_TYPE) && isTemporal(cqlType)) {
if (cqlType == DataTypes.DATE) {
return Optional.of(new DateToTemporalCodec<>(Date.class, TypeCodecs.DATE, context.getAttribute(TIME_ZONE)));
}
if (cqlType == DataTypes.TIME) {
return Optional.of(new DateToTemporalCodec<>(Date.class, TypeCodecs.TIME, context.getAttribute(TIME_ZONE)));
}
if (cqlType == DataTypes.TIMESTAMP) {
return Optional.of(new DateToTemporalCodec<>(Date.class, TypeCodecs.TIMESTAMP, context.getAttribute(TIME_ZONE)));
}
}
if (externalJavaType.isSubtypeOf(DATE_GENERIC_TYPE) && isUUID(cqlType)) {
TypeCodec<UUID> uuidCodec = codecFactory.getCodecRegistry().codecFor(cqlType);
@SuppressWarnings({ "unchecked", "rawtypes" }) DateToTemporalCodec<Date, Instant> instantCodec = (DateToTemporalCodec) codecFactory.createConvertingCodec(DataTypes.TIMESTAMP, externalJavaType, false);
return Optional.of(new DateToUUIDCodec<>(uuidCodec, instantCodec, context.getAttribute(TIME_UUID_GENERATOR)));
}
return Optional.empty();
}
use of com.datastax.oss.dsbulk.codecs.api.ConversionContext in project dsbulk by datastax.
the class JsonNodeConvertingCodecProvider method createJsonNodeConvertingCodec.
@Nullable
private ConvertingCodec<JsonNode, ?> createJsonNodeConvertingCodec(@NonNull DataType cqlType, @NonNull ConvertingCodecFactory codecFactory, boolean rootCodec) {
CodecRegistry codecRegistry = codecFactory.getCodecRegistry();
ConversionContext context = codecFactory.getContext();
// Don't apply null strings for non-root codecs
List<String> nullStrings = rootCodec ? context.getAttribute(NULL_STRINGS) : ImmutableList.of();
int cqlTypeCode = cqlType.getProtocolCode();
switch(cqlTypeCode) {
case ASCII:
case VARCHAR:
TypeCodec<String> typeCodec = codecRegistry.codecFor(cqlType);
return new JsonNodeToStringCodec(typeCodec, context.getAttribute(OBJECT_MAPPER), nullStrings);
case BOOLEAN:
return new JsonNodeToBooleanCodec(context.getAttribute(BOOLEAN_INPUT_WORDS), nullStrings);
case TINYINT:
return new JsonNodeToByteCodec(context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
case SMALLINT:
return new JsonNodeToShortCodec(context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
case INT:
return new JsonNodeToIntegerCodec(context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
case BIGINT:
return new JsonNodeToLongCodec(TypeCodecs.BIGINT, context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
case COUNTER:
return new JsonNodeToLongCodec(TypeCodecs.COUNTER, context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
case FLOAT:
return new JsonNodeToFloatCodec(context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
case DOUBLE:
return new JsonNodeToDoubleCodec(context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
case VARINT:
return new JsonNodeToBigIntegerCodec(context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
case DECIMAL:
return new JsonNodeToBigDecimalCodec(context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
case DATE:
return new JsonNodeToLocalDateCodec(context.getAttribute(LOCAL_DATE_FORMAT), context.getAttribute(TIME_ZONE), nullStrings);
case TIME:
return new JsonNodeToLocalTimeCodec(context.getAttribute(LOCAL_TIME_FORMAT), context.getAttribute(TIME_ZONE), nullStrings);
case TIMESTAMP:
return new JsonNodeToInstantCodec(context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(EPOCH), nullStrings);
case INET:
return new JsonNodeToInetAddressCodec(nullStrings);
case UUID:
{
ConvertingCodec<String, Instant> instantCodec = codecFactory.createConvertingCodec(DataTypes.TIMESTAMP, GenericType.STRING, false);
return new JsonNodeToUUIDCodec(TypeCodecs.UUID, instantCodec, context.getAttribute(TIME_UUID_GENERATOR), nullStrings);
}
case TIMEUUID:
{
ConvertingCodec<String, Instant> instantCodec = codecFactory.createConvertingCodec(DataTypes.TIMESTAMP, GenericType.STRING, false);
return new JsonNodeToUUIDCodec(TypeCodecs.TIMEUUID, instantCodec, context.getAttribute(TIME_UUID_GENERATOR), nullStrings);
}
case BLOB:
return new JsonNodeToBlobCodec(context.getAttribute(BINARY_FORMAT), nullStrings);
case DURATION:
return new JsonNodeToDurationCodec(nullStrings);
case LIST:
{
DataType elementType = ((ListType) cqlType).getElementType();
TypeCodec<List<Object>> collectionCodec = codecRegistry.codecFor(cqlType);
@SuppressWarnings("unchecked") ConvertingCodec<JsonNode, Object> eltCodec = (ConvertingCodec<JsonNode, Object>) createJsonNodeConvertingCodec(elementType, codecFactory, false);
return new JsonNodeToListCodec<>(collectionCodec, eltCodec, context.getAttribute(OBJECT_MAPPER), nullStrings);
}
case SET:
{
DataType elementType = ((SetType) cqlType).getElementType();
TypeCodec<Set<Object>> collectionCodec = codecRegistry.codecFor(cqlType);
@SuppressWarnings("unchecked") ConvertingCodec<JsonNode, Object> eltCodec = (ConvertingCodec<JsonNode, Object>) createJsonNodeConvertingCodec(elementType, codecFactory, false);
return new JsonNodeToSetCodec<>(collectionCodec, eltCodec, context.getAttribute(OBJECT_MAPPER), nullStrings);
}
case MAP:
{
DataType keyType = ((MapType) cqlType).getKeyType();
DataType valueType = ((MapType) cqlType).getValueType();
TypeCodec<Map<Object, Object>> mapCodec = codecRegistry.codecFor(cqlType);
ConvertingCodec<String, Object> keyCodec = codecFactory.createConvertingCodec(keyType, GenericType.STRING, false);
ConvertingCodec<JsonNode, Object> valueCodec = codecFactory.createConvertingCodec(valueType, JSON_NODE_TYPE, false);
return new JsonNodeToMapCodec<>(mapCodec, keyCodec, valueCodec, context.getAttribute(OBJECT_MAPPER), nullStrings);
}
case TUPLE:
{
TypeCodec<TupleValue> tupleCodec = codecRegistry.codecFor(cqlType);
ImmutableList.Builder<ConvertingCodec<JsonNode, Object>> eltCodecs = new ImmutableList.Builder<>();
for (DataType eltType : ((TupleType) cqlType).getComponentTypes()) {
ConvertingCodec<JsonNode, Object> eltCodec = codecFactory.createConvertingCodec(eltType, JSON_NODE_TYPE, false);
eltCodecs.add(Objects.requireNonNull(eltCodec));
}
return new JsonNodeToTupleCodec(tupleCodec, eltCodecs.build(), context.getAttribute(OBJECT_MAPPER), nullStrings, context.getAttribute(ALLOW_EXTRA_FIELDS), context.getAttribute(ALLOW_MISSING_FIELDS));
}
case UDT:
{
TypeCodec<UdtValue> udtCodec = codecRegistry.codecFor(cqlType);
ImmutableMap.Builder<CqlIdentifier, ConvertingCodec<JsonNode, Object>> fieldCodecs = new ImmutableMap.Builder<>();
List<CqlIdentifier> fieldNames = ((UserDefinedType) cqlType).getFieldNames();
List<DataType> fieldTypes = ((UserDefinedType) cqlType).getFieldTypes();
assert (fieldNames.size() == fieldTypes.size());
for (int idx = 0; idx < fieldNames.size(); idx++) {
CqlIdentifier fieldName = fieldNames.get(idx);
DataType fieldType = fieldTypes.get(idx);
ConvertingCodec<JsonNode, Object> fieldCodec = codecFactory.createConvertingCodec(fieldType, JSON_NODE_TYPE, false);
fieldCodecs.put(fieldName, Objects.requireNonNull(fieldCodec));
}
return new JsonNodeToUDTCodec(udtCodec, fieldCodecs.build(), context.getAttribute(OBJECT_MAPPER), nullStrings, context.getAttribute(ALLOW_EXTRA_FIELDS), context.getAttribute(ALLOW_MISSING_FIELDS));
}
case CUSTOM:
{
CustomType customType = (CustomType) cqlType;
switch(customType.getClassName()) {
case POINT_CLASS_NAME:
return new JsonNodeToPointCodec(context.getAttribute(OBJECT_MAPPER), context.getAttribute(GEO_FORMAT), nullStrings);
case LINE_STRING_CLASS_NAME:
return new JsonNodeToLineStringCodec(context.getAttribute(OBJECT_MAPPER), context.getAttribute(GEO_FORMAT), nullStrings);
case POLYGON_CLASS_NAME:
return new JsonNodeToPolygonCodec(context.getAttribute(OBJECT_MAPPER), context.getAttribute(GEO_FORMAT), nullStrings);
case DATE_RANGE_CLASS_NAME:
return new JsonNodeToDateRangeCodec(nullStrings);
}
// fall through
}
default:
try {
TypeCodec<?> innerCodec = codecRegistry.codecFor(cqlType);
LOGGER.warn(String.format("CQL type %s is not officially supported by this version of DSBulk; " + "JSON literals will be parsed and formatted using registered codec %s", cqlType, innerCodec.getClass().getSimpleName()));
return new JsonNodeToUnknownTypeCodec<>(innerCodec, nullStrings);
} catch (CodecNotFoundException ignored) {
}
return null;
}
}
use of com.datastax.oss.dsbulk.codecs.api.ConversionContext in project dsbulk by datastax.
the class JsonNodeToMapCodecTest method setUp.
@BeforeEach
void setUp() {
ConversionContext context = new TextConversionContext().setNullStrings("NULL").setFormatNumbers(true).setRoundingMode(HALF_EVEN);
ConvertingCodecFactory codecFactory = new ConvertingCodecFactory(context);
codec = (JsonNodeToMapCodec<Double, List<String>>) codecFactory.<JsonNode, Map<Double, List<String>>>createConvertingCodec(DataTypes.mapOf(DataTypes.DOUBLE, DataTypes.listOf(DataTypes.TEXT)), JSON_NODE_TYPE, true);
}
use of com.datastax.oss.dsbulk.codecs.api.ConversionContext in project dsbulk by datastax.
the class JsonNodeToSetCodecTest method setUp.
@BeforeEach
void setUp() {
ConversionContext context = new TextConversionContext().setNullStrings("NULL");
ConvertingCodecFactory codecFactory = new ConvertingCodecFactory(context);
codec1 = (JsonNodeToSetCodec<Double>) codecFactory.<JsonNode, Set<Double>>createConvertingCodec(DataTypes.setOf(DataTypes.DOUBLE), JSON_NODE_TYPE, true);
codec2 = (JsonNodeToSetCodec<String>) codecFactory.<JsonNode, Set<String>>createConvertingCodec(DataTypes.setOf(DataTypes.TEXT), JSON_NODE_TYPE, true);
}
use of com.datastax.oss.dsbulk.codecs.api.ConversionContext in project dsbulk by datastax.
the class JsonNodeToShortCodecTest method setUp.
@BeforeEach
void setUp() {
ConversionContext context = new TextConversionContext().setNullStrings("NULL").setFormatNumbers(true);
ConvertingCodecFactory codecFactory = new ConvertingCodecFactory(context);
codec = (JsonNodeToShortCodec) codecFactory.<JsonNode, Short>createConvertingCodec(DataTypes.SMALLINT, JSON_NODE_TYPE, true);
}
Aggregations