Search in sources :

Example 21 with ConversionContext

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

the class JsonNodeToInstantCodecTest method setUpCodec1.

@BeforeEach
void setUpCodec1() {
    ConversionContext context1 = new TextConversionContext().setNullStrings("NULL");
    ConversionContext context2 = new TextConversionContext().setNullStrings("NULL").setTimestampFormat("yyyyMMddHHmmss");
    ConversionContext context3 = new TextConversionContext().setNullStrings("NULL").setTimeUnit(MINUTES).setEpoch(ZonedDateTime.parse("2000-01-01T00:00:00Z"));
    ConversionContext context4 = new TextConversionContext().setNullStrings("NULL").setTimeUnit(MINUTES).setEpoch(ZonedDateTime.parse("2000-01-01T00:00:00Z")).setTimestampFormat("UNITS_SINCE_EPOCH");
    codec1 = (JsonNodeToInstantCodec) new ConvertingCodecFactory(context1).<JsonNode, Instant>createConvertingCodec(DataTypes.TIMESTAMP, JSON_NODE_TYPE, true);
    codec2 = (JsonNodeToInstantCodec) new ConvertingCodecFactory(context2).<JsonNode, Instant>createConvertingCodec(DataTypes.TIMESTAMP, JSON_NODE_TYPE, true);
    codec3 = (JsonNodeToInstantCodec) new ConvertingCodecFactory(context3).<JsonNode, Instant>createConvertingCodec(DataTypes.TIMESTAMP, JSON_NODE_TYPE, true);
    codec4 = (JsonNodeToInstantCodec) new ConvertingCodecFactory(context4).<JsonNode, Instant>createConvertingCodec(DataTypes.TIMESTAMP, JSON_NODE_TYPE, true);
}
Also used : ConvertingCodecFactory(com.datastax.oss.dsbulk.codecs.api.ConvertingCodecFactory) TextConversionContext(com.datastax.oss.dsbulk.codecs.text.TextConversionContext) TextConversionContext(com.datastax.oss.dsbulk.codecs.text.TextConversionContext) ConversionContext(com.datastax.oss.dsbulk.codecs.api.ConversionContext) Instant(java.time.Instant) JsonNode(com.fasterxml.jackson.databind.JsonNode) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 22 with ConversionContext

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

the class JsonNodeToIntegerCodecTest method setUp.

@BeforeEach
void setUp() {
    ConversionContext context = new TextConversionContext().setNullStrings("NULL").setFormatNumbers(true);
    ConvertingCodecFactory codecFactory = new ConvertingCodecFactory(context);
    codec = (JsonNodeToIntegerCodec) codecFactory.<JsonNode, Integer>createConvertingCodec(DataTypes.INT, JSON_NODE_TYPE, true);
}
Also used : ConvertingCodecFactory(com.datastax.oss.dsbulk.codecs.api.ConvertingCodecFactory) TextConversionContext(com.datastax.oss.dsbulk.codecs.text.TextConversionContext) TextConversionContext(com.datastax.oss.dsbulk.codecs.text.TextConversionContext) ConversionContext(com.datastax.oss.dsbulk.codecs.api.ConversionContext) JsonNode(com.fasterxml.jackson.databind.JsonNode) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 23 with ConversionContext

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

the class JsonNodeToListCodecTest method setUp.

@BeforeEach
void setUp() {
    ConversionContext context = new TextConversionContext().setNullStrings("NULL");
    ConvertingCodecFactory codecFactory = new ConvertingCodecFactory(context);
    codec1 = (JsonNodeToListCodec<Double>) codecFactory.<JsonNode, List<Double>>createConvertingCodec(DataTypes.listOf(DataTypes.DOUBLE), JSON_NODE_TYPE, true);
    codec2 = (JsonNodeToListCodec<String>) codecFactory.<JsonNode, List<String>>createConvertingCodec(DataTypes.listOf(DataTypes.TEXT), JSON_NODE_TYPE, true);
}
Also used : ConvertingCodecFactory(com.datastax.oss.dsbulk.codecs.api.ConvertingCodecFactory) TextConversionContext(com.datastax.oss.dsbulk.codecs.text.TextConversionContext) TextConversionContext(com.datastax.oss.dsbulk.codecs.text.TextConversionContext) ConversionContext(com.datastax.oss.dsbulk.codecs.api.ConversionContext) JsonNode(com.fasterxml.jackson.databind.JsonNode) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) List(java.util.List) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 24 with ConversionContext

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

the class NumericConvertingCodecsProvider method maybeProvide.

@NonNull
@Override
public Optional<ConvertingCodec<?, ?>> maybeProvide(@NonNull DataType cqlType, @NonNull GenericType<?> externalJavaType, @NonNull ConvertingCodecFactory codecFactory, boolean rootCodec) {
    CodecRegistry codecRegistry = codecFactory.getCodecRegistry();
    ConversionContext context = codecFactory.getContext();
    if (externalJavaType.isSubtypeOf(GenericType.of(Number.class))) {
        @SuppressWarnings("unchecked") Class<Number> numberType = (Class<Number>) externalJavaType.getRawType();
        if (isNumeric(cqlType)) {
            TypeCodec<Number> typeCodec = codecRegistry.codecFor(cqlType);
            return Optional.of(new NumberToNumberCodec<>(numberType, typeCodec));
        }
        if (cqlType == DataTypes.TIMESTAMP) {
            return Optional.of(new NumberToInstantCodec<>(numberType, context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH)));
        }
        if (isUUID(cqlType)) {
            TypeCodec<UUID> uuidCodec = codecRegistry.codecFor(cqlType);
            NumberToInstantCodec<Number> instantCodec = new NumberToInstantCodec<>(numberType, context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH));
            return Optional.of(new NumberToUUIDCodec<>(uuidCodec, instantCodec, context.getAttribute(TIME_UUID_GENERATOR)));
        }
        if (cqlType == DataTypes.BOOLEAN) {
            return Optional.of(new NumberToBooleanCodec<>(numberType, context.getAttribute(BOOLEAN_NUMBERS)));
        }
        if (cqlType == DataTypes.TEXT || cqlType == DataTypes.ASCII) {
            // number types to String, but rather any java type that has a corresponding cql type.
            return Optional.of(new NumberToStringCodec<>(numberType, context.getAttribute(NUMBER_FORMAT)));
        }
    }
    return Optional.empty();
}
Also used : ConversionContext(com.datastax.oss.dsbulk.codecs.api.ConversionContext) UUID(java.util.UUID) JdkCodecUtils.isUUID(com.datastax.oss.dsbulk.codecs.jdk.JdkCodecUtils.isUUID) CodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 25 with ConversionContext

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

the class JsonNodeToBigDecimalCodecTest method setUp.

@BeforeEach
void setUp() {
    ConversionContext context = new TextConversionContext().setNullStrings("NULL").setFormatNumbers(true);
    ConvertingCodecFactory codecFactory = new ConvertingCodecFactory(context);
    codec = (JsonNodeToBigDecimalCodec) codecFactory.<JsonNode, BigDecimal>createConvertingCodec(DataTypes.DECIMAL, JSON_NODE_TYPE, true);
}
Also used : ConvertingCodecFactory(com.datastax.oss.dsbulk.codecs.api.ConvertingCodecFactory) TextConversionContext(com.datastax.oss.dsbulk.codecs.text.TextConversionContext) TextConversionContext(com.datastax.oss.dsbulk.codecs.text.TextConversionContext) ConversionContext(com.datastax.oss.dsbulk.codecs.api.ConversionContext) JsonNode(com.fasterxml.jackson.databind.JsonNode) BigDecimal(java.math.BigDecimal) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

ConversionContext (com.datastax.oss.dsbulk.codecs.api.ConversionContext)32 ConvertingCodecFactory (com.datastax.oss.dsbulk.codecs.api.ConvertingCodecFactory)28 TextConversionContext (com.datastax.oss.dsbulk.codecs.text.TextConversionContext)28 BeforeEach (org.junit.jupiter.api.BeforeEach)28 JsonNode (com.fasterxml.jackson.databind.JsonNode)15 List (java.util.List)5 Instant (java.time.Instant)4 ImmutableMap (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap)3 TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)2 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)2 CustomType (com.datastax.oss.driver.api.core.type.CustomType)2 CodecNotFoundException (com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException)2 CodecRegistry (com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry)2 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)2 ConvertingCodec (com.datastax.oss.dsbulk.codecs.api.ConvertingCodec)2 JdkCodecUtils.isUUID (com.datastax.oss.dsbulk.codecs.jdk.JdkCodecUtils.isUUID)2 NonNull (edu.umd.cs.findbugs.annotations.NonNull)2 Nullable (edu.umd.cs.findbugs.annotations.Nullable)2 BigDecimal (java.math.BigDecimal)2 BigInteger (java.math.BigInteger)2