Search in sources :

Example 1 with DefaultRecord

use of com.datastax.oss.dsbulk.connectors.api.DefaultRecord in project dsbulk by datastax.

the class RecordUtils method mappedJson.

public static Record mappedJson(String... tokens) {
    int counter = COUNTER.incrementAndGet();
    DefaultRecord record = DefaultRecord.indexed("source" + counter, URI.create("file://file" + counter + ".json"), counter - 1);
    for (int i = 0; i < tokens.length; i += 2) {
        record.put(new DefaultMappedField(tokens[i]), JsonNodeFactory.instance.textNode(tokens[i + 1]));
    }
    return record;
}
Also used : DefaultMappedField(com.datastax.oss.dsbulk.connectors.api.DefaultMappedField) DefaultRecord(com.datastax.oss.dsbulk.connectors.api.DefaultRecord)

Example 2 with DefaultRecord

use of com.datastax.oss.dsbulk.connectors.api.DefaultRecord in project dsbulk by datastax.

the class DefaultReadResultMapper method map.

@NonNull
@Override
public Record map(@NonNull ReadResult result) {
    Object source = retainRecordSources ? result : null;
    try {
        Row row = result.getRow().orElseThrow(IllegalStateException::new);
        ColumnDefinitions columnDefinitions = row.getColumnDefinitions();
        DefaultRecord record = new DefaultRecord(source, resource, -1);
        for (ColumnDefinition def : columnDefinitions) {
            CQLWord variable = CQLWord.fromInternal(def.getName().asInternal());
            CqlIdentifier name = variable.asIdentifier();
            DataType cqlType = def.getType();
            Set<Field> fields = mapping.variableToFields(variable);
            for (Field field : fields) {
                GenericType<?> fieldType = null;
                try {
                    fieldType = recordMetadata.getFieldType(field, cqlType);
                    TypeCodec<?> codec = mapping.codec(variable, cqlType, fieldType);
                    Object value = row.get(name, codec);
                    record.setFieldValue(field, value);
                } catch (Exception e) {
                    String msg = String.format("Could not deserialize column %s of type %s as %s", name.asCql(true), cqlType, fieldType);
                    throw new IllegalArgumentException(msg, e);
                }
            }
        }
        return record;
    } catch (Exception e) {
        return new DefaultErrorRecord(source, resource, -1, e);
    }
}
Also used : ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) DefaultRecord(com.datastax.oss.dsbulk.connectors.api.DefaultRecord) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) ColumnDefinition(com.datastax.oss.driver.api.core.cql.ColumnDefinition) Field(com.datastax.oss.dsbulk.connectors.api.Field) DefaultErrorRecord(com.datastax.oss.dsbulk.connectors.api.DefaultErrorRecord) DataType(com.datastax.oss.driver.api.core.type.DataType) CQLWord(com.datastax.oss.dsbulk.mapping.CQLWord) Row(com.datastax.oss.driver.api.core.cql.Row) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 3 with DefaultRecord

use of com.datastax.oss.dsbulk.connectors.api.DefaultRecord in project dsbulk by datastax.

the class RecordUtils method indexedCSV.

public static Record indexedCSV(String... values) {
    int counter = COUNTER.incrementAndGet();
    DefaultRecord record = DefaultRecord.indexed("source" + counter, URI.create("file://file" + counter + ".csv"), counter - 1);
    for (int i = 0; i < values.length; i++) {
        record.put(new DefaultIndexedField(i), values[i]);
    }
    return record;
}
Also used : DefaultIndexedField(com.datastax.oss.dsbulk.connectors.api.DefaultIndexedField) DefaultRecord(com.datastax.oss.dsbulk.connectors.api.DefaultRecord)

Example 4 with DefaultRecord

use of com.datastax.oss.dsbulk.connectors.api.DefaultRecord in project dsbulk by datastax.

the class RecordUtils method mappedCSV.

public static Record mappedCSV(String... tokens) {
    int counter = COUNTER.incrementAndGet();
    DefaultRecord record = DefaultRecord.indexed("source" + counter, URI.create("file://file" + counter + ".csv"), counter - 1);
    for (int i = 0; i < tokens.length; i += 2) {
        record.put(new DefaultMappedField(tokens[i]), tokens[i + 1]);
        record.put(new DefaultIndexedField(i % 2), tokens[i + 1]);
    }
    return record;
}
Also used : DefaultIndexedField(com.datastax.oss.dsbulk.connectors.api.DefaultIndexedField) DefaultMappedField(com.datastax.oss.dsbulk.connectors.api.DefaultMappedField) DefaultRecord(com.datastax.oss.dsbulk.connectors.api.DefaultRecord)

Aggregations

DefaultRecord (com.datastax.oss.dsbulk.connectors.api.DefaultRecord)4 DefaultIndexedField (com.datastax.oss.dsbulk.connectors.api.DefaultIndexedField)2 DefaultMappedField (com.datastax.oss.dsbulk.connectors.api.DefaultMappedField)2 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)1 ColumnDefinition (com.datastax.oss.driver.api.core.cql.ColumnDefinition)1 ColumnDefinitions (com.datastax.oss.driver.api.core.cql.ColumnDefinitions)1 Row (com.datastax.oss.driver.api.core.cql.Row)1 DataType (com.datastax.oss.driver.api.core.type.DataType)1 DefaultErrorRecord (com.datastax.oss.dsbulk.connectors.api.DefaultErrorRecord)1 Field (com.datastax.oss.dsbulk.connectors.api.Field)1 CQLWord (com.datastax.oss.dsbulk.mapping.CQLWord)1 NonNull (edu.umd.cs.findbugs.annotations.NonNull)1