Search in sources :

Example 6 with ConnectorContext

use of io.cdap.cdap.etl.api.connector.ConnectorContext in project cdap by caskdata.

the class ConnectionHandler method specGenerationLocally.

private void specGenerationLocally(String namespace, SpecGenerationRequest specRequest, Connection conn, HttpServiceResponder responder) throws IOException {
    ServicePluginConfigurer pluginConfigurer = getContext().createServicePluginConfigurer(namespace);
    ConnectorConfigurer connectorConfigurer = new DefaultConnectorConfigurer(pluginConfigurer);
    ConnectorContext connectorContext = new DefaultConnectorContext(new SimpleFailureCollector(), pluginConfigurer);
    // 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(conn.getPlugin().getArtifact()));
    try (Connector connector = getConnector(pluginConfigurer, conn.getPlugin(), namespace, pluginSelector)) {
        connector.configure(connectorConfigurer);
        ConnectorSpecRequest connectorSpecRequest = ConnectorSpecRequest.builder().setPath(specRequest.getPath()).setConnection(conn.getName()).setProperties(specRequest.getProperties()).build();
        ConnectorSpec spec = connector.generateSpec(connectorContext, connectorSpecRequest);
        ConnectorSpec newSpec = ConnectionUtils.filterSpecWithPluginNameAndType(spec, specRequest.getPluginName(), specRequest.getPluginType());
        responder.sendString(GSON.toJson(ConnectionUtils.getConnectorDetail(pluginSelector.getSelectedArtifact(), newSpec)));
    }
}
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) 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) ConnectorSpecRequest(io.cdap.cdap.etl.api.connector.ConnectorSpecRequest) ConnectorSpec(io.cdap.cdap.etl.api.connector.ConnectorSpec) DefaultConnectorConfigurer(io.cdap.cdap.datapipeline.connection.DefaultConnectorConfigurer) DefaultConnectorConfigurer(io.cdap.cdap.datapipeline.connection.DefaultConnectorConfigurer) ConnectorConfigurer(io.cdap.cdap.etl.api.connector.ConnectorConfigurer) ServicePluginConfigurer(io.cdap.cdap.api.service.http.ServicePluginConfigurer)

Example 7 with ConnectorContext

use of io.cdap.cdap.etl.api.connector.ConnectorContext in project hydrator-plugins by cdapio.

the class DBConnectorTest method testTest.

private void testTest(DBConnector connector) {
    ConnectorContext context = new MockConnectorContext(new MockConnectorConfigurer());
    connector.test(context);
    ValidationException validationException = context.getFailureCollector().getOrThrowException();
    Assert.assertTrue(validationException.getFailures().isEmpty());
}
Also used : ValidationException(io.cdap.cdap.etl.api.validation.ValidationException) MockConnectorConfigurer(io.cdap.cdap.etl.mock.common.MockConnectorConfigurer) ConnectorContext(io.cdap.cdap.etl.api.connector.ConnectorContext) MockConnectorContext(io.cdap.cdap.etl.mock.common.MockConnectorContext) MockConnectorContext(io.cdap.cdap.etl.mock.common.MockConnectorContext)

Example 8 with ConnectorContext

use of io.cdap.cdap.etl.api.connector.ConnectorContext in project hydrator-plugins by cdapio.

the class AbstractFileConnector method getInputFormatProvider.

@Override
public InputFormatProvider getInputFormatProvider(ConnectorContext context, SampleRequest sampleRequest) throws IOException {
    String fullPath = getFullPath(sampleRequest.getPath());
    Map<String, String> sampleRequestProperties = sampleRequest.getProperties();
    ValidatingInputFormat inputFormat = getValidatingInputFormat(context, fullPath, sampleRequestProperties);
    Job job = JobUtils.createInstance();
    Configuration conf = job.getConfiguration();
    // set entries here, before FileSystem is used
    for (Map.Entry<String, String> entry : getFileSystemProperties(fullPath).entrySet()) {
        conf.set(entry.getKey(), entry.getValue());
    }
    Path path = new Path(fullPath);
    // need this to load the extra class loader to avoid ClassNotFoundException for the file system
    FileSystem fs = JobUtils.applyWithExtraClassLoader(job, getClass().getClassLoader(), f -> FileSystem.get(path.toUri(), conf));
    FileStatus[] fileStatus = fs.globStatus(path);
    if (fileStatus == null) {
        throw new IOException(String.format("Input path %s does not exist", path));
    }
    // add input path also needs to create the file system so need to wrap it
    JobUtils.applyWithExtraClassLoader(job, getClass().getClassLoader(), (ThrowableFunction<JobContext, Void, IOException>) t -> {
        FileInputFormat.addInputPath(job, path);
        return null;
    });
    String inputFormatClassName = inputFormat.getInputFormatClassName();
    Configuration hConf = job.getConfiguration();
    Map<String, String> inputFormatConfiguration = inputFormat.getInputFormatConfiguration();
    for (Map.Entry<String, String> propertyEntry : inputFormatConfiguration.entrySet()) {
        hConf.set(propertyEntry.getKey(), propertyEntry.getValue());
    }
    // set entries here again, in case anything set by PathTrackingInputFormat should be overridden
    for (Map.Entry<String, String> entry : getFileSystemProperties(fullPath).entrySet()) {
        conf.set(entry.getKey(), entry.getValue());
    }
    return new SourceInputFormatProvider(inputFormatClassName, conf);
}
Also used : Path(org.apache.hadoop.fs.Path) FileFormat(io.cdap.plugin.format.FileFormat) Arrays(java.util.Arrays) NullWritable(org.apache.hadoop.io.NullWritable) ConnectorSpec(io.cdap.cdap.etl.api.connector.ConnectorSpec) SourceInputFormatProvider(io.cdap.plugin.common.SourceInputFormatProvider) FileSystem(org.apache.hadoop.fs.FileSystem) Description(io.cdap.cdap.api.annotation.Description) HashMap(java.util.HashMap) FileStatus(org.apache.hadoop.fs.FileStatus) ConnectorSpecRequest(io.cdap.cdap.etl.api.connector.ConnectorSpecRequest) SamplePropertyField(io.cdap.cdap.etl.api.connector.SamplePropertyField) JobUtils(io.cdap.plugin.common.batch.JobUtils) ArrayList(java.util.ArrayList) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) PluginConfig(io.cdap.cdap.api.plugin.PluginConfig) ThrowableFunction(io.cdap.plugin.common.batch.ThrowableFunction) FileSourceProperties(io.cdap.plugin.format.plugin.FileSourceProperties) HashSet(java.util.HashSet) Gson(com.google.gson.Gson) InputFormatProvider(io.cdap.cdap.api.data.batch.InputFormatProvider) BrowseEntity(io.cdap.cdap.etl.api.connector.BrowseEntity) ConnectorContext(io.cdap.cdap.etl.api.connector.ConnectorContext) Configuration(org.apache.hadoop.conf.Configuration) Map(java.util.Map) Path(org.apache.hadoop.fs.Path) FileInputFormat(org.apache.hadoop.mapreduce.lib.input.FileInputFormat) FormatContext(io.cdap.cdap.etl.api.validation.FormatContext) ValidatingInputFormat(io.cdap.cdap.etl.api.validation.ValidatingInputFormat) ValidationException(io.cdap.cdap.etl.api.validation.ValidationException) BrowseEntityPropertyValue(io.cdap.cdap.etl.api.connector.BrowseEntityPropertyValue) Set(java.util.Set) BrowseEntityTypeInfo(io.cdap.cdap.etl.api.connector.BrowseEntityTypeInfo) IOException(java.io.IOException) PluginProperties(io.cdap.cdap.api.plugin.PluginProperties) UUID(java.util.UUID) Field(java.lang.reflect.Field) Schema(io.cdap.cdap.api.data.schema.Schema) List(java.util.List) SampleRequest(io.cdap.cdap.etl.api.connector.SampleRequest) Job(org.apache.hadoop.mapreduce.Job) JobContext(org.apache.hadoop.mapreduce.JobContext) BatchConnector(io.cdap.cdap.etl.api.batch.BatchConnector) Collections(java.util.Collections) ValidatingInputFormat(io.cdap.cdap.etl.api.validation.ValidatingInputFormat) FileStatus(org.apache.hadoop.fs.FileStatus) Configuration(org.apache.hadoop.conf.Configuration) IOException(java.io.IOException) FileSystem(org.apache.hadoop.fs.FileSystem) SourceInputFormatProvider(io.cdap.plugin.common.SourceInputFormatProvider) JobContext(org.apache.hadoop.mapreduce.JobContext) Job(org.apache.hadoop.mapreduce.Job) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with ConnectorContext

use of io.cdap.cdap.etl.api.connector.ConnectorContext in project cdap by cdapio.

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 10 with ConnectorContext

use of io.cdap.cdap.etl.api.connector.ConnectorContext in project cdap by cdapio.

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)

Aggregations

ConnectorContext (io.cdap.cdap.etl.api.connector.ConnectorContext)18 ServicePluginConfigurer (io.cdap.cdap.api.service.http.ServicePluginConfigurer)16 DefaultConnectorConfigurer (io.cdap.cdap.datapipeline.connection.DefaultConnectorConfigurer)16 Connector (io.cdap.cdap.etl.api.connector.Connector)16 ArtifactSelectorProvider (io.cdap.cdap.etl.common.ArtifactSelectorProvider)16 TrackedPluginSelector (io.cdap.cdap.etl.spec.TrackedPluginSelector)16 DefaultConnectorContext (io.cdap.cdap.datapipeline.connection.DefaultConnectorContext)12 ConnectorConfigurer (io.cdap.cdap.etl.api.connector.ConnectorConfigurer)12 SimpleFailureCollector (io.cdap.cdap.etl.proto.validation.SimpleFailureCollector)12 ConnectorSpec (io.cdap.cdap.etl.api.connector.ConnectorSpec)9 ConnectorSpecRequest (io.cdap.cdap.etl.api.connector.ConnectorSpecRequest)9 ValidationException (io.cdap.cdap.etl.api.validation.ValidationException)6 Connection (io.cdap.cdap.etl.proto.connection.Connection)6 SampleRequest (io.cdap.cdap.etl.api.connector.SampleRequest)5 ConnectorDetail (io.cdap.cdap.etl.proto.connection.ConnectorDetail)4 SampleResponse (io.cdap.cdap.etl.proto.connection.SampleResponse)4 BrowseDetail (io.cdap.cdap.etl.api.connector.BrowseDetail)2 BrowseRequest (io.cdap.cdap.etl.api.connector.BrowseRequest)2 ConnectionBadRequestException (io.cdap.cdap.etl.proto.connection.ConnectionBadRequestException)2 ConnectionCreationRequest (io.cdap.cdap.etl.proto.connection.ConnectionCreationRequest)2