Search in sources :

Example 1 with SampleResponse

use of io.cdap.cdap.etl.proto.connection.SampleResponse in project cdap by caskdata.

the class DataPipelineConnectionTest method testBrowseSample.

@Test
public void testBrowseSample() throws Exception {
    File directory = TEMP_FOLDER.newFolder();
    List<BrowseEntity> entities = addFilesInDirectory(directory);
    String conn = "BrowseSample";
    addConnection(conn, new ConnectionCreationRequest("", new PluginInfo(FileConnector.NAME, Connector.PLUGIN_TYPE, null, Collections.emptyMap(), // in set up we add "-mocks" as the suffix for the artifact id
    new ArtifactSelectorConfig("system", APP_ARTIFACT_ID.getArtifact() + "-mocks", APP_ARTIFACT_ID.getVersion()))));
    // get all 10 results back
    BrowseDetail browseDetail = browseConnection(conn, directory.getCanonicalPath(), 10);
    BrowseDetail expected = BrowseDetail.builder().setTotalCount(10).setEntities(entities).build();
    Assert.assertEquals(expected, browseDetail);
    // only retrieve 5 back, count should still be 10
    browseDetail = browseConnection(conn, directory.getCanonicalPath(), 5);
    expected = BrowseDetail.builder().setTotalCount(10).setEntities(entities.subList(0, 5)).build();
    Assert.assertEquals(expected, browseDetail);
    // browse the created directory, should give empty result
    browseDetail = browseConnection(conn, entities.get(0).getPath(), 10);
    expected = BrowseDetail.builder().setTotalCount(0).build();
    Assert.assertEquals(expected, browseDetail);
    // browse the file, since it is not browsable, it should return itself
    browseDetail = browseConnection(conn, entities.get(1).getPath(), 10);
    expected = BrowseDetail.builder().setTotalCount(1).addEntity(entities.get(1)).build();
    Assert.assertEquals(expected, browseDetail);
    List<StructuredRecord> records = new ArrayList<>();
    Schema schema = Schema.recordOf("schema", Schema.Field.of("offset", Schema.of(Schema.Type.LONG)), Schema.Field.of("body", Schema.of(Schema.Type.STRING)));
    for (int i = 0; i < 100; i++) {
        records.add(StructuredRecord.builder(schema).set("offset", i * 2L).set("body", "1").build());
    }
    ArtifactSelectorConfig artifact = new ArtifactSelectorConfig("SYSTEM", APP_ARTIFACT_ID.getArtifact() + "-mocks", APP_ARTIFACT_ID.getVersion());
    Map<String, String> properties = ImmutableMap.of("path", entities.get(1).getPath(), "useConnection", "true", "connection", String.format("${conn(%s)}", conn));
    ConnectorDetail detail = new ConnectorDetail(ImmutableSet.of(new PluginDetail("file", "batchsource", properties, artifact, schema), new PluginDetail("file", "streamingsource", properties, artifact, schema)));
    SampleResponse expectedSample = new SampleResponse(detail, schema, records);
    // sample the file, the file has 100 lines, so 200 should retrieve all lines
    SampleResponse sampleResponse = sampleConnection(conn, entities.get(1).getPath(), 200);
    Assert.assertEquals(expectedSample, sampleResponse);
    // sample 100, should get all
    sampleResponse = sampleConnection(conn, entities.get(1).getPath(), 100);
    Assert.assertEquals(expectedSample, sampleResponse);
    // sample 50, should only get 50
    sampleResponse = sampleConnection(conn, entities.get(1).getPath(), 50);
    expectedSample = new SampleResponse(detail, schema, records.subList(0, 50));
    Assert.assertEquals(expectedSample, sampleResponse);
    deleteConnection(conn);
}
Also used : ArtifactSelectorConfig(io.cdap.cdap.etl.proto.ArtifactSelectorConfig) BrowseEntity(io.cdap.cdap.etl.api.connector.BrowseEntity) Schema(io.cdap.cdap.api.data.schema.Schema) SampleResponse(io.cdap.cdap.etl.proto.connection.SampleResponse) ArrayList(java.util.ArrayList) ConnectorDetail(io.cdap.cdap.etl.proto.connection.ConnectorDetail) BrowseDetail(io.cdap.cdap.etl.api.connector.BrowseDetail) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) PluginDetail(io.cdap.cdap.etl.proto.connection.PluginDetail) ConnectionCreationRequest(io.cdap.cdap.etl.proto.connection.ConnectionCreationRequest) PluginInfo(io.cdap.cdap.etl.proto.connection.PluginInfo) File(java.io.File) Test(org.junit.Test)

Example 2 with SampleResponse

use of io.cdap.cdap.etl.proto.connection.SampleResponse in project cdap by caskdata.

the class ConnectionUtils method getSampleResponse.

/**
 * Return {@link SampleResponse} for the connector
 *
 * @throws IOException
 */
public static SampleResponse getSampleResponse(Connector connector, ConnectorContext connectorContext, SampleRequest sampleRequest, ConnectorDetail detail, ServicePluginConfigurer pluginConfigurer) throws IOException {
    if (connector instanceof DirectConnector) {
        DirectConnector directConnector = (DirectConnector) connector;
        List<StructuredRecord> sample = directConnector.sample(connectorContext, sampleRequest);
        return new SampleResponse(detail, sample.isEmpty() ? null : sample.get(0).getSchema(), sample);
    }
    if (connector instanceof BatchConnector) {
        LimitingConnector limitingConnector = new LimitingConnector((BatchConnector) connector, pluginConfigurer);
        List<StructuredRecord> sample = limitingConnector.sample(connectorContext, sampleRequest);
        return new SampleResponse(detail, sample.isEmpty() ? null : sample.get(0).getSchema(), sample);
    }
    throw new ConnectionBadRequestException("Connector is not supported. " + "The supported connector should be DirectConnector or BatchConnector.");
}
Also used : ConnectionBadRequestException(io.cdap.cdap.etl.proto.connection.ConnectionBadRequestException) BatchConnector(io.cdap.cdap.etl.api.batch.BatchConnector) DirectConnector(io.cdap.cdap.etl.api.connector.DirectConnector) SampleResponse(io.cdap.cdap.etl.proto.connection.SampleResponse) LimitingConnector(io.cdap.cdap.datapipeline.connection.LimitingConnector) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord)

Example 3 with SampleResponse

use of io.cdap.cdap.etl.proto.connection.SampleResponse in project cdap by caskdata.

the class ConnectionHandler method sampleLocally.

private void sampleLocally(String namespace, String sampleRequestString, Connection conn, HttpServiceResponder responder) throws IOException {
    SampleRequest sampleRequest = GSON.fromJson(sampleRequestString, SampleRequest.class);
    ServicePluginConfigurer pluginConfigurer = getContext().createServicePluginConfigurer(namespace);
    ConnectorConfigurer connectorConfigurer = new DefaultConnectorConfigurer(pluginConfigurer);
    ConnectorContext connectorContext = new DefaultConnectorContext(new SimpleFailureCollector(), pluginConfigurer);
    PluginInfo plugin = conn.getPlugin();
    // use tracked selector to get exact plugin version that gets selected since the passed version can be null
    TrackedPluginSelector pluginSelector = new TrackedPluginSelector(new ArtifactSelectorProvider().getPluginSelector(plugin.getArtifact()));
    try (Connector connector = getConnector(pluginConfigurer, plugin, namespace, pluginSelector)) {
        connector.configure(connectorConfigurer);
        ConnectorSpecRequest specRequest = ConnectorSpecRequest.builder().setPath(sampleRequest.getPath()).setConnection(conn.getName()).setProperties(sampleRequest.getProperties()).build();
        ConnectorSpec spec = connector.generateSpec(connectorContext, specRequest);
        ConnectorDetail detail = ConnectionUtils.getConnectorDetail(pluginSelector.getSelectedArtifact(), spec);
        try {
            SampleResponse sampleResponse = ConnectionUtils.getSampleResponse(connector, connectorContext, sampleRequest, detail, pluginConfigurer);
            responder.sendString(GSON.toJson(sampleResponse));
        } catch (ConnectionBadRequestException e) {
            // should not happen
            responder.sendError(HttpURLConnection.HTTP_BAD_REQUEST, e.getMessage());
        }
    }
}
Also used : Connector(io.cdap.cdap.etl.api.connector.Connector) SimpleFailureCollector(io.cdap.cdap.etl.proto.validation.SimpleFailureCollector) ArtifactSelectorProvider(io.cdap.cdap.etl.common.ArtifactSelectorProvider) SampleResponse(io.cdap.cdap.etl.proto.connection.SampleResponse) ConnectorDetail(io.cdap.cdap.etl.proto.connection.ConnectorDetail) ConnectorSpec(io.cdap.cdap.etl.api.connector.ConnectorSpec) SampleRequest(io.cdap.cdap.etl.api.connector.SampleRequest) DefaultConnectorConfigurer(io.cdap.cdap.datapipeline.connection.DefaultConnectorConfigurer) ConnectionBadRequestException(io.cdap.cdap.etl.proto.connection.ConnectionBadRequestException) DefaultConnectorContext(io.cdap.cdap.datapipeline.connection.DefaultConnectorContext) TrackedPluginSelector(io.cdap.cdap.etl.spec.TrackedPluginSelector) ConnectorContext(io.cdap.cdap.etl.api.connector.ConnectorContext) DefaultConnectorContext(io.cdap.cdap.datapipeline.connection.DefaultConnectorContext) PluginInfo(io.cdap.cdap.etl.proto.connection.PluginInfo) ConnectorSpecRequest(io.cdap.cdap.etl.api.connector.ConnectorSpecRequest) DefaultConnectorConfigurer(io.cdap.cdap.datapipeline.connection.DefaultConnectorConfigurer) ConnectorConfigurer(io.cdap.cdap.etl.api.connector.ConnectorConfigurer) ServicePluginConfigurer(io.cdap.cdap.api.service.http.ServicePluginConfigurer)

Example 4 with SampleResponse

use of io.cdap.cdap.etl.proto.connection.SampleResponse in project cdap by caskdata.

the class RemoteConnectionSampleTask method execute.

@Override
public String execute(SystemAppTaskContext systemAppContext, RemoteConnectionRequest request) throws Exception {
    String namespace = request.getNamespace();
    Connection connection = request.getConnection();
    // Plugin selector and configurer
    TrackedPluginSelector pluginSelector = new TrackedPluginSelector(new ArtifactSelectorProvider().getPluginSelector(connection.getPlugin().getArtifact()));
    ServicePluginConfigurer pluginConfigurer = systemAppContext.createServicePluginConfigurer(namespace);
    ConnectorContext connectorContext = ConnectionUtils.getConnectorContext(pluginConfigurer);
    SampleRequest sampleRequest = GSON.fromJson(request.getRequest(), SampleRequest.class);
    try (Connector connector = getConnector(systemAppContext, pluginConfigurer, connection.getPlugin(), namespace, pluginSelector)) {
        connector.configure(new DefaultConnectorConfigurer(pluginConfigurer));
        ConnectorSpecRequest specRequest = ConnectorSpecRequest.builder().setPath(sampleRequest.getPath()).setConnection(connection.getName()).setProperties(sampleRequest.getProperties()).build();
        ConnectorSpec spec = connector.generateSpec(connectorContext, specRequest);
        ConnectorDetail detail = ConnectionUtils.getConnectorDetail(pluginSelector.getSelectedArtifact(), spec);
        SampleResponse sampleResponse = ConnectionUtils.getSampleResponse(connector, connectorContext, sampleRequest, detail, pluginConfigurer);
        return GSON.toJson(sampleResponse);
    }
}
Also used : Connector(io.cdap.cdap.etl.api.connector.Connector) ArtifactSelectorProvider(io.cdap.cdap.etl.common.ArtifactSelectorProvider) SampleResponse(io.cdap.cdap.etl.proto.connection.SampleResponse) Connection(io.cdap.cdap.etl.proto.connection.Connection) ConnectorDetail(io.cdap.cdap.etl.proto.connection.ConnectorDetail) ConnectorSpec(io.cdap.cdap.etl.api.connector.ConnectorSpec) SampleRequest(io.cdap.cdap.etl.api.connector.SampleRequest) DefaultConnectorConfigurer(io.cdap.cdap.datapipeline.connection.DefaultConnectorConfigurer) TrackedPluginSelector(io.cdap.cdap.etl.spec.TrackedPluginSelector) ConnectorContext(io.cdap.cdap.etl.api.connector.ConnectorContext) ConnectorSpecRequest(io.cdap.cdap.etl.api.connector.ConnectorSpecRequest) ServicePluginConfigurer(io.cdap.cdap.api.service.http.ServicePluginConfigurer)

Example 5 with SampleResponse

use of io.cdap.cdap.etl.proto.connection.SampleResponse in project cdap by caskdata.

the class DataPipelineConnectionTest method sampleConnection.

private SampleResponse sampleConnection(String connection, String path, int limit) throws IOException {
    String url = URLEncoder.encode(String.format("v1/contexts/%s/connections/%s/sample", NamespaceId.DEFAULT.getNamespace(), connection), StandardCharsets.UTF_8.name());
    URL validatePipelineURL = serviceURI.resolve(String.format("%s?path=%s&limit=%s", url, path, limit)).toURL();
    HttpRequest.Builder request = HttpRequest.builder(HttpMethod.POST, validatePipelineURL).withBody(GSON.toJson(SampleRequest.builder(limit).setPath(path).build()));
    HttpResponse response = executeRequest(request);
    Assert.assertEquals("Wrong answer: " + response.getResponseBodyAsString(), expectedCode, response.getResponseCode());
    return expectedCode != HttpURLConnection.HTTP_OK ? null : GSON.fromJson(response.getResponseBodyAsString(), SampleResponse.class);
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) SampleResponse(io.cdap.cdap.etl.proto.connection.SampleResponse) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL)

Aggregations

SampleResponse (io.cdap.cdap.etl.proto.connection.SampleResponse)5 ConnectorDetail (io.cdap.cdap.etl.proto.connection.ConnectorDetail)3 StructuredRecord (io.cdap.cdap.api.data.format.StructuredRecord)2 ServicePluginConfigurer (io.cdap.cdap.api.service.http.ServicePluginConfigurer)2 DefaultConnectorConfigurer (io.cdap.cdap.datapipeline.connection.DefaultConnectorConfigurer)2 Connector (io.cdap.cdap.etl.api.connector.Connector)2 ConnectorContext (io.cdap.cdap.etl.api.connector.ConnectorContext)2 ConnectorSpec (io.cdap.cdap.etl.api.connector.ConnectorSpec)2 ConnectorSpecRequest (io.cdap.cdap.etl.api.connector.ConnectorSpecRequest)2 SampleRequest (io.cdap.cdap.etl.api.connector.SampleRequest)2 ArtifactSelectorProvider (io.cdap.cdap.etl.common.ArtifactSelectorProvider)2 ConnectionBadRequestException (io.cdap.cdap.etl.proto.connection.ConnectionBadRequestException)2 PluginInfo (io.cdap.cdap.etl.proto.connection.PluginInfo)2 TrackedPluginSelector (io.cdap.cdap.etl.spec.TrackedPluginSelector)2 Schema (io.cdap.cdap.api.data.schema.Schema)1 DefaultConnectorContext (io.cdap.cdap.datapipeline.connection.DefaultConnectorContext)1 LimitingConnector (io.cdap.cdap.datapipeline.connection.LimitingConnector)1 BatchConnector (io.cdap.cdap.etl.api.batch.BatchConnector)1 BrowseDetail (io.cdap.cdap.etl.api.connector.BrowseDetail)1 BrowseEntity (io.cdap.cdap.etl.api.connector.BrowseEntity)1