Search in sources :

Example 1 with ConvertingCodec

use of com.datastax.oss.dsbulk.codecs.api.ConvertingCodec in project dsbulk by datastax.

the class MapConvertingCodecsProvider method maybeProvide.

@NonNull
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public Optional<ConvertingCodec<?, ?>> maybeProvide(@NonNull DataType cqlType, @NonNull GenericType<?> externalJavaType, @NonNull ConvertingCodecFactory codecFactory, boolean rootCodec) {
    CodecRegistry codecRegistry = codecFactory.getCodecRegistry();
    if (externalJavaType.isSubtypeOf(GenericType.of(Map.class)) && externalJavaType.getType() instanceof ParameterizedType && ((ParameterizedType) externalJavaType.getType()).getActualTypeArguments().length == 2) {
        GenericType<?> keyType = GenericType.of(((ParameterizedType) externalJavaType.getType()).getActualTypeArguments()[0]);
        GenericType<?> valueType = GenericType.of(((ParameterizedType) externalJavaType.getType()).getActualTypeArguments()[1]);
        if (cqlType instanceof MapType) {
            TypeCodec<?> typeCodec = codecRegistry.codecFor(cqlType);
            ConvertingCodec<?, ?> keyConvertingCodec = codecFactory.createConvertingCodec(((MapType) cqlType).getKeyType(), keyType, false);
            ConvertingCodec<?, ?> valueConvertingCodec = codecFactory.createConvertingCodec(((MapType) cqlType).getValueType(), valueType, false);
            MapToMapCodec codec = new MapToMapCodec(externalJavaType.getRawType(), typeCodec, keyConvertingCodec, valueConvertingCodec);
            return Optional.of(codec);
        } else if (cqlType instanceof UserDefinedType) {
            TypeCodec<UdtValue> udtCodec = codecRegistry.codecFor(cqlType);
            ImmutableMap.Builder<CqlIdentifier, ConvertingCodec<?, Object>> fieldCodecs = new ImmutableMap.Builder<>();
            List<CqlIdentifier> fieldNames = ((UserDefinedType) cqlType).getFieldNames();
            List<DataType> fieldTypes = ((UserDefinedType) cqlType).getFieldTypes();
            ConvertingCodec<?, String> keyCodec = codecFactory.createConvertingCodec(DataTypes.TEXT, keyType, false);
            assert (fieldNames.size() == fieldTypes.size());
            for (int idx = 0; idx < fieldNames.size(); idx++) {
                CqlIdentifier fieldName = fieldNames.get(idx);
                DataType fieldType = fieldTypes.get(idx);
                ConvertingCodec<?, Object> fieldCodec = codecFactory.createConvertingCodec(fieldType, valueType, false);
                fieldCodecs.put(fieldName, fieldCodec);
            }
            MapToUDTCodec codec = new MapToUDTCodec(externalJavaType.getRawType(), udtCodec, keyCodec, fieldCodecs.build());
            return Optional.of(codec);
        }
    }
    return Optional.empty();
}
Also used : ConvertingCodec(com.datastax.oss.dsbulk.codecs.api.ConvertingCodec) TypeCodec(com.datastax.oss.driver.api.core.type.codec.TypeCodec) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) MapType(com.datastax.oss.driver.api.core.type.MapType) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) ParameterizedType(java.lang.reflect.ParameterizedType) DataType(com.datastax.oss.driver.api.core.type.DataType) List(java.util.List) CodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) Map(java.util.Map) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 2 with ConvertingCodec

use of com.datastax.oss.dsbulk.codecs.api.ConvertingCodec 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;
    }
}
Also used : ConvertingCodec(com.datastax.oss.dsbulk.codecs.api.ConvertingCodec) TypeCodec(com.datastax.oss.driver.api.core.type.codec.TypeCodec) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) JsonNode(com.fasterxml.jackson.databind.JsonNode) DataType(com.datastax.oss.driver.api.core.type.DataType) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) List(java.util.List) DefaultCodecRegistry(com.datastax.oss.driver.internal.core.type.codec.registry.DefaultCodecRegistry) CodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry) CustomType(com.datastax.oss.driver.api.core.type.CustomType) JsonNodeToPointCodec(com.datastax.oss.dsbulk.codecs.text.json.dse.JsonNodeToPointCodec) JsonNodeToPolygonCodec(com.datastax.oss.dsbulk.codecs.text.json.dse.JsonNodeToPolygonCodec) ConversionContext(com.datastax.oss.dsbulk.codecs.api.ConversionContext) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) JsonNodeToDateRangeCodec(com.datastax.oss.dsbulk.codecs.text.json.dse.JsonNodeToDateRangeCodec) JsonNodeToLineStringCodec(com.datastax.oss.dsbulk.codecs.text.json.dse.JsonNodeToLineStringCodec) CodecNotFoundException(com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException) Nullable(edu.umd.cs.findbugs.annotations.Nullable)

Example 3 with ConvertingCodec

use of com.datastax.oss.dsbulk.codecs.api.ConvertingCodec in project dsbulk by datastax.

the class CollectionConvertingCodecsProvider method maybeProvide.

@NonNull
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public Optional<ConvertingCodec<?, ?>> maybeProvide(@NonNull DataType cqlType, @NonNull GenericType<?> externalJavaType, @NonNull ConvertingCodecFactory codecFactory, boolean rootCodec) {
    CodecRegistry codecRegistry = codecFactory.getCodecRegistry();
    if (externalJavaType.isSubtypeOf(GenericType.of(List.class)) && externalJavaType.getType() instanceof ParameterizedType && ((ParameterizedType) externalJavaType.getType()).getActualTypeArguments().length == 1) {
        GenericType<?> componentType = GenericType.of(((ParameterizedType) externalJavaType.getType()).getActualTypeArguments()[0]);
        if (cqlType instanceof UserDefinedType) {
            TypeCodec<UdtValue> udtCodec = codecRegistry.codecFor(cqlType);
            ImmutableList.Builder<ConvertingCodec> eltCodecs = new ImmutableList.Builder<>();
            for (DataType eltType : ((UserDefinedType) cqlType).getFieldTypes()) {
                ConvertingCodec eltCodec = codecFactory.createConvertingCodec(eltType, componentType, false);
                eltCodecs.add(eltCodec);
            }
            ListToUDTCodec codec = new ListToUDTCodec(externalJavaType.getRawType(), udtCodec, eltCodecs.build());
            return Optional.of(codec);
        } else if (cqlType instanceof TupleType) {
            TypeCodec<TupleValue> tupleCodec = codecRegistry.codecFor(cqlType);
            ImmutableList.Builder<ConvertingCodec> eltCodecs = new ImmutableList.Builder<>();
            for (DataType eltType : ((TupleType) cqlType).getComponentTypes()) {
                ConvertingCodec eltCodec = codecFactory.createConvertingCodec(eltType, componentType, false);
                eltCodecs.add(eltCodec);
            }
            ListToTupleCodec codec = new ListToTupleCodec(externalJavaType.getRawType(), tupleCodec, eltCodecs.build());
            return Optional.of(codec);
        }
    }
    if (externalJavaType.isSubtypeOf(GenericType.of(Collection.class)) && externalJavaType.getType() instanceof ParameterizedType && ((ParameterizedType) externalJavaType.getType()).getActualTypeArguments().length == 1) {
        GenericType<?> componentType = GenericType.of(((ParameterizedType) externalJavaType.getType()).getActualTypeArguments()[0]);
        if (isCollection(cqlType)) {
            TypeCodec<?> typeCodec = codecRegistry.codecFor(cqlType);
            ConvertingCodec elementCodec = null;
            Supplier<? extends Collection<?>> collectionCreator;
            if (cqlType instanceof SetType) {
                elementCodec = codecFactory.createConvertingCodec(((SetType) cqlType).getElementType(), componentType, false);
            } else if (cqlType instanceof ListType) {
                elementCodec = codecFactory.createConvertingCodec(((ListType) cqlType).getElementType(), componentType, false);
            }
            if (cqlType instanceof SetType) {
                collectionCreator = HashSet::new;
            } else {
                collectionCreator = ArrayList::new;
            }
            Class<? extends Collection<?>> collType = (Class<? extends Collection<?>>) externalJavaType.getRawType();
            CollectionToCollectionCodec codec = new CollectionToCollectionCodec(collType, typeCodec, elementCodec, collectionCreator);
            return Optional.of(codec);
        }
    }
    return Optional.empty();
}
Also used : ConvertingCodec(com.datastax.oss.dsbulk.codecs.api.ConvertingCodec) UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) TypeCodec(com.datastax.oss.driver.api.core.type.codec.TypeCodec) ArrayList(java.util.ArrayList) ParameterizedType(java.lang.reflect.ParameterizedType) SetType(com.datastax.oss.driver.api.core.type.SetType) ListType(com.datastax.oss.driver.api.core.type.ListType) TupleType(com.datastax.oss.driver.api.core.type.TupleType) DataType(com.datastax.oss.driver.api.core.type.DataType) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) ArrayList(java.util.ArrayList) List(java.util.List) CodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry) HashSet(java.util.HashSet) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) Collection(java.util.Collection) JdkCodecUtils.isCollection(com.datastax.oss.dsbulk.codecs.jdk.JdkCodecUtils.isCollection) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 4 with ConvertingCodec

use of com.datastax.oss.dsbulk.codecs.api.ConvertingCodec in project dsbulk by datastax.

the class StringConvertingCodecProvider method createStringConvertingCodec.

@Nullable
private ConvertingCodec<String, ?> createStringConvertingCodec(@NonNull DataType cqlType, @NonNull ConvertingCodecFactory codecFactory, boolean rootCodec) {
    ConversionContext context = codecFactory.getContext();
    // DAT-297: 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 = codecFactory.getCodecRegistry().codecFor(cqlType);
            return new StringToStringCodec(typeCodec, nullStrings);
        case BOOLEAN:
            return new StringToBooleanCodec(context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_OUTPUT_WORDS), nullStrings);
        case TINYINT:
            return new StringToByteCodec(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 StringToShortCodec(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 StringToIntegerCodec(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 StringToLongCodec(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 StringToLongCodec(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 StringToFloatCodec(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 StringToDoubleCodec(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 StringToBigIntegerCodec(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 StringToBigDecimalCodec(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 StringToLocalDateCodec(context.getAttribute(LOCAL_DATE_FORMAT), context.getAttribute(TIME_ZONE), nullStrings);
        case TIME:
            return new StringToLocalTimeCodec(context.getAttribute(LOCAL_TIME_FORMAT), context.getAttribute(TIME_ZONE), nullStrings);
        case TIMESTAMP:
            return new StringToInstantCodec(context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(EPOCH), nullStrings);
        case INET:
            return new StringToInetAddressCodec(nullStrings);
        case UUID:
            {
                ConvertingCodec<String, Instant> instantCodec = codecFactory.createConvertingCodec(DataTypes.TIMESTAMP, GenericType.STRING, false);
                return new StringToUUIDCodec(TypeCodecs.UUID, instantCodec, context.getAttribute(TIME_UUID_GENERATOR), nullStrings);
            }
        case TIMEUUID:
            {
                ConvertingCodec<String, Instant> instantCodec = codecFactory.createConvertingCodec(DataTypes.TIMESTAMP, GenericType.STRING, false);
                return new StringToUUIDCodec(TypeCodecs.TIMEUUID, instantCodec, context.getAttribute(TIME_UUID_GENERATOR), nullStrings);
            }
        case BLOB:
            return new StringToBlobCodec(nullStrings, context.getAttribute(BINARY_FORMAT));
        case DURATION:
            return new StringToDurationCodec(nullStrings);
        case LIST:
            {
                ConvertingCodec<JsonNode, List<Object>> jsonCodec = codecFactory.createConvertingCodec(cqlType, JSON_NODE_TYPE, false);
                return new StringToListCodec<>(jsonCodec, context.getAttribute(OBJECT_MAPPER), nullStrings);
            }
        case SET:
            {
                ConvertingCodec<JsonNode, Set<Object>> jsonCodec = codecFactory.createConvertingCodec(cqlType, JSON_NODE_TYPE, false);
                return new StringToSetCodec<>(jsonCodec, context.getAttribute(OBJECT_MAPPER), nullStrings);
            }
        case MAP:
            {
                ConvertingCodec<JsonNode, Map<Object, Object>> jsonCodec = codecFactory.createConvertingCodec(cqlType, JSON_NODE_TYPE, false);
                return new StringToMapCodec<>(jsonCodec, context.getAttribute(OBJECT_MAPPER), nullStrings);
            }
        case TUPLE:
            {
                ConvertingCodec<JsonNode, TupleValue> jsonCodec = codecFactory.createConvertingCodec(cqlType, JSON_NODE_TYPE, false);
                return new StringToTupleCodec(jsonCodec, context.getAttribute(OBJECT_MAPPER), nullStrings);
            }
        case UDT:
            {
                ConvertingCodec<JsonNode, UdtValue> jsonCodec = codecFactory.createConvertingCodec(cqlType, JSON_NODE_TYPE, false);
                return new StringToUDTCodec(jsonCodec, context.getAttribute(OBJECT_MAPPER), nullStrings);
            }
        case CUSTOM:
            {
                CustomType customType = (CustomType) cqlType;
                switch(customType.getClassName()) {
                    case POINT_CLASS_NAME:
                        return new StringToPointCodec(context.getAttribute(GEO_FORMAT), nullStrings);
                    case LINE_STRING_CLASS_NAME:
                        return new StringToLineStringCodec(context.getAttribute(GEO_FORMAT), nullStrings);
                    case POLYGON_CLASS_NAME:
                        return new StringToPolygonCodec(context.getAttribute(GEO_FORMAT), nullStrings);
                    case DATE_RANGE_CLASS_NAME:
                        return new StringToDateRangeCodec(nullStrings);
                }
            // fall through
            }
        default:
            try {
                TypeCodec<?> innerCodec = codecFactory.getCodecRegistry().codecFor(cqlType);
                LOGGER.warn(String.format("CQL type %s is not officially supported by this version of DSBulk; " + "string literals will be parsed and formatted using registered codec %s", cqlType, innerCodec.getClass().getSimpleName()));
                return new StringToUnknownTypeCodec<>(innerCodec, nullStrings);
            } catch (CodecNotFoundException ignored) {
            }
            return null;
    }
}
Also used : ConvertingCodec(com.datastax.oss.dsbulk.codecs.api.ConvertingCodec) StringToPolygonCodec(com.datastax.oss.dsbulk.codecs.text.string.dse.StringToPolygonCodec) StringToDateRangeCodec(com.datastax.oss.dsbulk.codecs.text.string.dse.StringToDateRangeCodec) CustomType(com.datastax.oss.driver.api.core.type.CustomType) StringToLineStringCodec(com.datastax.oss.dsbulk.codecs.text.string.dse.StringToLineStringCodec) ConversionContext(com.datastax.oss.dsbulk.codecs.api.ConversionContext) StringToPointCodec(com.datastax.oss.dsbulk.codecs.text.string.dse.StringToPointCodec) CodecNotFoundException(com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException) Nullable(edu.umd.cs.findbugs.annotations.Nullable)

Aggregations

ConvertingCodec (com.datastax.oss.dsbulk.codecs.api.ConvertingCodec)4 DataType (com.datastax.oss.driver.api.core.type.DataType)3 TypeCodec (com.datastax.oss.driver.api.core.type.codec.TypeCodec)3 CodecRegistry (com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry)3 List (java.util.List)3 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)2 CustomType (com.datastax.oss.driver.api.core.type.CustomType)2 UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)2 CodecNotFoundException (com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException)2 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)2 ImmutableMap (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap)2 ConversionContext (com.datastax.oss.dsbulk.codecs.api.ConversionContext)2 NonNull (edu.umd.cs.findbugs.annotations.NonNull)2 Nullable (edu.umd.cs.findbugs.annotations.Nullable)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)1 ListType (com.datastax.oss.driver.api.core.type.ListType)1 MapType (com.datastax.oss.driver.api.core.type.MapType)1 SetType (com.datastax.oss.driver.api.core.type.SetType)1 TupleType (com.datastax.oss.driver.api.core.type.TupleType)1