Search in sources :

Example 36 with Record

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

the class LogManagerTest method should_stop_when_max_result_mapping_errors_reached.

@Test
void should_stop_when_max_result_mapping_errors_reached() throws Exception {
    Path outputDir = Files.createTempDirectory("test");
    LogManager logManager = new LogManager(session, outputDir, ErrorThreshold.forAbsoluteValue(2), ErrorThreshold.forAbsoluteValue(0), true, statementFormatter, EXTENDED, rowFormatter);
    logManager.init();
    Flux<Record> stmts = Flux.just(rowRecord1, rowRecord2, rowRecord3);
    try {
        stmts.transform(logManager.newUnmappableRecordsHandler()).blockLast();
        fail("Expecting TooManyErrorsException to be thrown");
    } catch (TooManyErrorsException e) {
        assertThat(e).hasMessage("Too many errors, the maximum allowed is 2.");
        assertThat(((AbsoluteErrorThreshold) e.getThreshold()).getMaxErrors()).isEqualTo(2);
    }
    logManager.close();
    Path errors = logManager.getOperationDirectory().resolve("mapping-errors.log");
    assertThat(errors.toFile()).exists();
    assertThat(FileUtils.listAllFilesInDirectory(logManager.getOperationDirectory())).containsOnly(errors);
    List<String> lines = Files.readAllLines(errors, UTF_8);
    String content = String.join("\n", lines);
    assertThat(content).doesNotContain("Source: ").doesNotContain("Resource: ").doesNotContain("Position: ").contains("SELECT 1").containsOnlyOnce("c1: 1").containsOnlyOnce("java.lang.RuntimeException: error 1").contains("SELECT 2").containsOnlyOnce("c1: 2").containsOnlyOnce("java.lang.RuntimeException: error 2").doesNotContain("c3: 3");
}
Also used : Path(java.nio.file.Path) TooManyErrorsException(com.datastax.oss.dsbulk.workflow.api.error.TooManyErrorsException) DefaultErrorRecord(com.datastax.oss.dsbulk.connectors.api.DefaultErrorRecord) DefaultRecord(com.datastax.oss.dsbulk.connectors.api.DefaultRecord) Record(com.datastax.oss.dsbulk.connectors.api.Record) Test(org.junit.jupiter.api.Test)

Example 37 with Record

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

the class LogManagerTest method should_handle_unmappable_statements_without_source.

@Test
void should_handle_unmappable_statements_without_source() throws Exception {
    Path outputDir = Files.createTempDirectory("test");
    LogManager logManager = new LogManager(session, outputDir, ErrorThreshold.forAbsoluteValue(1), ErrorThreshold.forAbsoluteValue(0), true, statementFormatter, EXTENDED, rowFormatter);
    logManager.init();
    Record record = DefaultRecord.indexed(null, resource1, 1, "foo", " bar");
    UnmappableStatement stmt = new UnmappableStatement(record, new RuntimeException("error 1"));
    Flux<BatchableStatement<?>> stmts = Flux.just(stmt);
    stmts.transform(logManager.newUnmappableStatementsHandler()).blockLast();
    logManager.close();
    Path errors = logManager.getOperationDirectory().resolve("mapping-errors.log");
    Path positions = logManager.getOperationDirectory().resolve("positions.txt");
    assertThat(errors.toFile()).exists();
    assertThat(positions.toFile()).exists();
    assertThat(FileUtils.listAllFilesInDirectory(logManager.getOperationDirectory())).containsOnly(errors, positions);
    List<String> lines = Files.readAllLines(errors, UTF_8);
    String content = String.join("\n", lines);
    assertThat(content).doesNotContain("Source: ").contains("Resource: " + resource1).contains("Position: 1").contains("java.lang.RuntimeException: error 1");
}
Also used : Path(java.nio.file.Path) BatchableStatement(com.datastax.oss.driver.api.core.cql.BatchableStatement) DefaultErrorRecord(com.datastax.oss.dsbulk.connectors.api.DefaultErrorRecord) DefaultRecord(com.datastax.oss.dsbulk.connectors.api.DefaultRecord) Record(com.datastax.oss.dsbulk.connectors.api.Record) UnmappableStatement(com.datastax.oss.dsbulk.workflow.commons.statement.UnmappableStatement) Test(org.junit.jupiter.api.Test)

Example 38 with Record

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

the class DefaultReadResultMapperTest method should_map_result_to_mapped_record_when_mapping_succeeds.

@ParameterizedTest
@ValueSource(booleans = { true, false })
void should_map_result_to_mapped_record_when_mapping_succeeds(boolean retainRecordSources) {
    DefaultReadResultMapper mapper = new DefaultReadResultMapper(mapping, recordMetadata, RESOURCE, retainRecordSources);
    Record record = mapper.map(result);
    Assertions.assertThat(record.fields()).containsOnly(F0, F1, F2);
    assertThat(record.getFieldValue(F0)).isEqualTo(42);
    assertThat(record.getFieldValue(F1)).isEqualTo("foo");
    assertThat(record.getFieldValue(F2)).isEqualTo("bar");
    if (retainRecordSources) {
        assertThat(record.getSource()).isSameAs(result);
    } else {
        assertThat(record.getSource()).isNull();
    }
    assertThat(record.getResource()).isEqualTo(URI.create("cql://ks1/table1"));
}
Also used : ErrorRecord(com.datastax.oss.dsbulk.connectors.api.ErrorRecord) Record(com.datastax.oss.dsbulk.connectors.api.Record) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 39 with Record

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

the class JsonConnectorTest method should_read_from_stdin_with_special_encoding.

@Test
void should_read_from_stdin_with_special_encoding() throws Exception {
    InputStream stdin = System.in;
    try {
        String line = "{ \"fóô\" : \"bàr\", \"qïx\" : null }\n";
        InputStream is = new ByteArrayInputStream(line.getBytes(ISO_8859_1));
        System.setIn(is);
        JsonConnector connector = new JsonConnector();
        Config settings = TestConfigUtils.createTestConfig("dsbulk.connector.json", "encoding", "ISO-8859-1");
        connector.configure(settings, true, true);
        connector.init();
        assertThat(connector.readConcurrency()).isOne();
        assertThat(ReflectionUtils.invokeMethod("isDataSizeSamplingAvailable", connector, Boolean.TYPE)).isFalse();
        List<Record> actual = Flux.merge(connector.read()).collectList().block();
        assertThat(actual).hasSize(1);
        assertThat(actual.get(0).getSource()).isEqualTo(objectMapper.readTree(line));
        assertThat(actual.get(0).getResource()).isEqualTo(URI.create("std:/"));
        assertThat(actual.get(0).getPosition()).isEqualTo(1L);
        assertThat(actual.get(0).getFieldValue(new DefaultMappedField("fóô"))).isEqualTo(factory.textNode("bàr"));
        assertThat(actual.get(0).getFieldValue(new DefaultMappedField("qïx"))).isEqualTo(factory.nullNode());
        connector.close();
    } finally {
        System.setIn(stdin);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Config(com.typesafe.config.Config) DefaultMappedField(com.datastax.oss.dsbulk.connectors.api.DefaultMappedField) DefaultRecord(com.datastax.oss.dsbulk.connectors.api.DefaultRecord) Record(com.datastax.oss.dsbulk.connectors.api.Record) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 40 with Record

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

the class JsonConnectorTest method should_write_multiple_files.

@Test
void should_write_multiple_files() throws Exception {
    JsonConnector connector = new JsonConnector();
    Path out = Files.createTempDirectory("test");
    try {
        int maxConcurrentFiles = 4;
        Config settings = TestConfigUtils.createTestConfig("dsbulk.connector.json", "url", quoteJson(out), "escape", "\"\\\"\"", "maxConcurrentFiles", maxConcurrentFiles);
        connector.configure(settings, false, true);
        connector.init();
        assertThat(connector.writeConcurrency()).isEqualTo(maxConcurrentFiles);
        // repeat the records 1000 times to fully exercise multiple file writing
        Scheduler scheduler = Schedulers.newParallel("workflow");
        Function<Publisher<Record>, Publisher<Record>> write = connector.write();
        Flux.range(0, 1000).flatMap(i -> Flux.fromIterable(createRecords(false, resource)).transform(write).subscribeOn(scheduler), maxConcurrentFiles).blockLast();
        connector.close();
        List<String> actual = FileUtils.readAllLinesInDirectoryAsStream(out).sorted().distinct().collect(Collectors.toList());
        assertThat(actual).containsOnly("{\"Year\":1997,\"Make\":\"Ford\",\"Model\":\"E350\",\"Description\":\"ac, abs, moon\",\"Price\":3000.0}", "{\"Year\":1999,\"Make\":\"Chevy\",\"Model\":\"Venture \\\"Extended Edition\\\"\",\"Description\":null,\"Price\":4900.0}", "{\"Year\":1996,\"Make\":\"Jeep\",\"Model\":\"Grand Cherokee\",\"Description\":\"MUST SELL!\\nair, moon roof, loaded\",\"Price\":4799.0}", "{\"Year\":1999,\"Make\":\"Chevy\",\"Model\":\"Venture \\\"Extended Edition, Very Large\\\"\",\"Description\":null,\"Price\":5000.0}", "{\"Year\":null,\"Make\":null,\"Model\":\"Venture \\\"Extended Edition\\\"\",\"Description\":null,\"Price\":4900.0}");
    } finally {
        deleteDirectory(out);
    }
}
Also used : Path(java.nio.file.Path) Arrays(java.util.Arrays) GZIPInputStream(java.util.zip.GZIPInputStream) URL(java.net.URL) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) URISyntaxException(java.net.URISyntaxException) ConfigUtils(com.datastax.oss.dsbulk.config.ConfigUtils) Throwables(org.assertj.core.util.Throwables) WireMockServer(com.github.tomakehurst.wiremock.WireMockServer) AfterAll(org.junit.jupiter.api.AfterAll) FileUtils.readFile(com.datastax.oss.dsbulk.tests.utils.FileUtils.readFile) ISO_8859_1(java.nio.charset.StandardCharsets.ISO_8859_1) ByteArrayInputStream(java.io.ByteArrayInputStream) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeforeAll(org.junit.jupiter.api.BeforeAll) Level(org.slf4j.event.Level) JsonNode(com.fasterxml.jackson.databind.JsonNode) URI(java.net.URI) DefaultRecord(com.datastax.oss.dsbulk.connectors.api.DefaultRecord) JsonParseException(com.fasterxml.jackson.core.JsonParseException) Method(java.lang.reflect.Method) Path(java.nio.file.Path) Record(com.datastax.oss.dsbulk.connectors.api.Record) LogInterceptor(com.datastax.oss.dsbulk.tests.logging.LogInterceptor) MethodSource(org.junit.jupiter.params.provider.MethodSource) WiremockResolver(ru.lanwen.wiremock.ext.WiremockResolver) CommonConnectorFeature(com.datastax.oss.dsbulk.connectors.api.CommonConnectorFeature) WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) BulkLoaderURLStreamHandlerFactory(com.datastax.oss.dsbulk.url.BulkLoaderURLStreamHandlerFactory) Arguments(org.junit.jupiter.params.provider.Arguments) Collectors(java.util.stream.Collectors) UncheckedIOException(java.io.UncheckedIOException) Test(org.junit.jupiter.api.Test) Field(com.datastax.oss.dsbulk.connectors.api.Field) List(java.util.List) Stream(java.util.stream.Stream) FileUtils.deleteDirectory(com.datastax.oss.dsbulk.tests.utils.FileUtils.deleteDirectory) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) TestConfigUtils(com.datastax.oss.dsbulk.tests.utils.TestConfigUtils) Assertions.assertDoesNotThrow(org.junit.jupiter.api.Assertions.assertDoesNotThrow) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WireMock.any(com.github.tomakehurst.wiremock.client.WireMock.any) Scheduler(reactor.core.scheduler.Scheduler) Function(java.util.function.Function) Wiremock(ru.lanwen.wiremock.ext.WiremockResolver.Wiremock) ArrayList(java.util.ArrayList) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Schedulers(reactor.core.scheduler.Schedulers) CompressedIOUtils(com.datastax.oss.dsbulk.io.CompressedIOUtils) Arguments.arguments(org.junit.jupiter.params.provider.Arguments.arguments) FileUtils(com.datastax.oss.dsbulk.tests.utils.FileUtils) WireMock.urlPathEqualTo(com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo) PrintStream(java.io.PrintStream) ValueSource(org.junit.jupiter.params.provider.ValueSource) Config(com.typesafe.config.Config) Files(java.nio.file.Files) StringUtils.quoteJson(com.datastax.oss.dsbulk.tests.utils.StringUtils.quoteJson) Publisher(org.reactivestreams.Publisher) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DefaultMappedField(com.datastax.oss.dsbulk.connectors.api.DefaultMappedField) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) InputStreamReader(java.io.InputStreamReader) LogCapture(com.datastax.oss.dsbulk.tests.logging.LogCapture) DisplayName(org.junit.jupiter.api.DisplayName) Flux(reactor.core.publisher.Flux) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Paths(java.nio.file.Paths) ReflectionUtils(com.datastax.oss.dsbulk.tests.utils.ReflectionUtils) BufferedReader(java.io.BufferedReader) LogInterceptingExtension(com.datastax.oss.dsbulk.tests.logging.LogInterceptingExtension) Throwables.getRootCause(org.assertj.core.util.Throwables.getRootCause) Charsets(com.datastax.oss.driver.shaded.guava.common.base.Charsets) InputStream(java.io.InputStream) Config(com.typesafe.config.Config) Scheduler(reactor.core.scheduler.Scheduler) Publisher(org.reactivestreams.Publisher) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Record (com.datastax.oss.dsbulk.connectors.api.Record)54 DefaultRecord (com.datastax.oss.dsbulk.connectors.api.DefaultRecord)40 Config (com.typesafe.config.Config)39 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)37 Test (org.junit.jupiter.api.Test)35 ErrorRecord (com.datastax.oss.dsbulk.connectors.api.ErrorRecord)24 Path (java.nio.file.Path)24 DefaultIndexedField (com.datastax.oss.dsbulk.connectors.api.DefaultIndexedField)10 Function (java.util.function.Function)9 MethodSource (org.junit.jupiter.params.provider.MethodSource)9 DefaultMappedField (com.datastax.oss.dsbulk.connectors.api.DefaultMappedField)8 ArrayList (java.util.ArrayList)8 List (java.util.List)8 Publisher (org.reactivestreams.Publisher)8 DefaultErrorRecord (com.datastax.oss.dsbulk.connectors.api.DefaultErrorRecord)7 IOException (java.io.IOException)7 ValueSource (org.junit.jupiter.params.provider.ValueSource)7 Flux (reactor.core.publisher.Flux)7 DataStaxBulkLoader (com.datastax.oss.dsbulk.runner.DataStaxBulkLoader)6 ExitStatus (com.datastax.oss.dsbulk.runner.ExitStatus)6