Search in sources :

Example 11 with SnowflakeConnectionProperties

use of org.talend.components.snowflake.SnowflakeConnectionProperties in project components by Talend.

the class SnowflakeSourceOrSink method validateConnectionProperties.

protected static ValidationResultMutable validateConnectionProperties(SnowflakeProvideConnectionProperties properties) {
    ValidationResultMutable vr = new ValidationResultMutable();
    vr.setStatus(Result.OK);
    SnowflakeConnectionProperties connectionProperties = properties.getConnectionProperties();
    StringBuilder missingProperties = new StringBuilder();
    if (StringUtils.isEmpty(connectionProperties.account.getValue())) {
        missingProperties.append("'Account', ");
    }
    if (StringUtils.isEmpty(connectionProperties.userPassword.password.getValue())) {
        missingProperties.append("'Password', ");
    }
    if (StringUtils.isEmpty(connectionProperties.userPassword.userId.getValue())) {
        missingProperties.append("'UserID', ");
    }
    if (StringUtils.isEmpty(connectionProperties.schemaName.getValue())) {
        missingProperties.append("'Schema', ");
    }
    if (StringUtils.isEmpty(connectionProperties.db.getValue())) {
        missingProperties.append("'Database', ");
    }
    if (!missingProperties.toString().isEmpty()) {
        vr.setStatus(Result.ERROR);
        vr.setMessage(i18nMessages.getMessage("error.requiredPropertyIsEmpty", missingProperties.toString().substring(0, missingProperties.length() - 2)));
    }
    return vr;
}
Also used : ValidationResultMutable(org.talend.daikon.properties.ValidationResultMutable) SnowflakeConnectionProperties(org.talend.components.snowflake.SnowflakeConnectionProperties)

Example 12 with SnowflakeConnectionProperties

use of org.talend.components.snowflake.SnowflakeConnectionProperties in project components by Talend.

the class SnowflakeSourceOrSink method getSchemaNames.

protected List<NamedThing> getSchemaNames(RuntimeContainer container, Connection connection) throws IOException {
    // Returns the list with a table names (for the wh, db and schema)
    List<NamedThing> returnList = new ArrayList<>();
    SnowflakeConnectionProperties connProps = getEffectiveConnectionProperties(container);
    try {
        DatabaseMetaData metaData = connection.getMetaData();
        // Fetch all tables in the db and schema provided
        String[] types = { "TABLE" };
        ResultSet resultIter = metaData.getTables(getCatalog(connProps), getDbSchema(connProps), null, types);
        String tableName = null;
        while (resultIter.next()) {
            tableName = resultIter.getString("TABLE_NAME");
            returnList.add(new SimpleNamedThing(tableName, tableName));
        }
    } catch (SQLException se) {
        throw new IOException(i18nMessages.getMessage("error.searchingTable", getCatalog(connProps), getDbSchema(connProps), se.getMessage()), se);
    }
    return returnList;
}
Also used : SQLException(java.sql.SQLException) SimpleNamedThing(org.talend.daikon.SimpleNamedThing) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) SnowflakeConnectionProperties(org.talend.components.snowflake.SnowflakeConnectionProperties) IOException(java.io.IOException) NamedThing(org.talend.daikon.NamedThing) SimpleNamedThing(org.talend.daikon.SimpleNamedThing) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 13 with SnowflakeConnectionProperties

use of org.talend.components.snowflake.SnowflakeConnectionProperties in project components by Talend.

the class SnowflakeWriter method open.

@Override
public void open(String uId) throws IOException {
    this.uId = uId;
    processingConnection = sink.createConnection(container);
    uploadConnection = sink.createConnection(container);
    if (null == mainSchema) {
        mainSchema = sink.getRuntimeSchema(container);
    }
    SnowflakeConnectionProperties connectionProperties = sprops.getConnectionProperties();
    Map<LoaderProperty, Object> prop = new HashMap<>();
    boolean isUpperCase = sprops.convertColumnsAndTableToUppercase.getValue();
    String tableName = isUpperCase ? sprops.getTableName().toUpperCase() : sprops.getTableName();
    prop.put(LoaderProperty.tableName, tableName);
    prop.put(LoaderProperty.schemaName, connectionProperties.schemaName.getStringValue());
    prop.put(LoaderProperty.databaseName, connectionProperties.db.getStringValue());
    switch(sprops.outputAction.getValue()) {
        case INSERT:
            prop.put(LoaderProperty.operation, Operation.INSERT);
            break;
        case UPDATE:
            prop.put(LoaderProperty.operation, Operation.MODIFY);
            break;
        case UPSERT:
            prop.put(LoaderProperty.operation, Operation.UPSERT);
            break;
        case DELETE:
            prop.put(LoaderProperty.operation, Operation.DELETE);
            break;
    }
    List<Field> columns = mainSchema.getFields();
    List<String> keyStr = new ArrayList<>();
    List<String> columnsStr = new ArrayList<>();
    for (Field f : columns) {
        String dbColumnName = f.getProp(SchemaConstants.TALEND_COLUMN_DB_COLUMN_NAME);
        String fName = isUpperCase ? dbColumnName.toUpperCase() : dbColumnName;
        columnsStr.add(fName);
        if (null != f.getProp(SchemaConstants.TALEND_COLUMN_IS_KEY)) {
            keyStr.add(fName);
        }
    }
    row = new Object[columnsStr.size()];
    prop.put(LoaderProperty.columns, columnsStr);
    if (sprops.outputAction.getValue() == UPSERT) {
        keyStr.clear();
        keyStr.add(sprops.upsertKeyColumn.getValue());
    }
    if (keyStr.size() > 0) {
        prop.put(LoaderProperty.keys, keyStr);
    }
    prop.put(LoaderProperty.remoteStage, "~");
    loader = (StreamLoader) LoaderFactory.createLoader(prop, uploadConnection, processingConnection);
    loader.setListener(listener);
    loader.start();
}
Also used : LoaderProperty(net.snowflake.client.loader.LoaderProperty) Field(org.apache.avro.Schema.Field) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SnowflakeConnectionProperties(org.talend.components.snowflake.SnowflakeConnectionProperties)

Example 14 with SnowflakeConnectionProperties

use of org.talend.components.snowflake.SnowflakeConnectionProperties in project components by Talend.

the class SnowflakeRuntime method createConnection.

/**
 * Creates or gets connection.
 * If component use existing connection then get it by reference component id from container(and check if it's not closed),
 * else creates new connection using {@link DriverManager} and saves it in container.
 *
 * @param container - runtime container
 * @param connProps - properties, than contain all required settings for establishing new connection or get it by stored
 * reference id.
 * @return active connection.
 * @throws IOException may be thrown if referenced connection is closed or if failed to create connection using
 * {@link DriverManager}
 */
protected Connection createConnection(RuntimeContainer container) throws IOException {
    Connection conn = null;
    SnowflakeConnectionProperties connectionProperties = getConnectionProperties();
    String refComponentId = connectionProperties.getReferencedComponentId();
    // Using another component's connection
    if (refComponentId != null) {
        // In a runtime container
        if (container != null) {
            conn = (Connection) container.getComponentData(refComponentId, KEY_CONNECTION);
            try {
                if (conn != null && !conn.isClosed()) {
                    return conn;
                }
            } catch (SQLException ex) {
                throw new IOException(ex);
            }
            throw new IOException(I18N_MESSAGES.getMessage("error.refComponentNotConnected", refComponentId));
        }
        // Design time
        connectionProperties = connectionProperties.getReferencedConnectionProperties();
        // FIXME This should not happen - but does as of now
        if (connectionProperties == null) {
            throw new IOException(I18N_MESSAGES.getMessage("error.refComponentWithoutProperties", refComponentId));
        }
    }
    conn = DriverManagerUtils.getConnection(connectionProperties);
    if (container != null) {
        container.setComponentData(container.getCurrentComponentId(), KEY_CONNECTION, conn);
        container.setComponentData(container.getCurrentComponentId(), KEY_CONNECTION_PROPERTIES, connectionProperties);
    }
    return conn;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) SnowflakeConnectionProperties(org.talend.components.snowflake.SnowflakeConnectionProperties) IOException(java.io.IOException)

Example 15 with SnowflakeConnectionProperties

use of org.talend.components.snowflake.SnowflakeConnectionProperties in project components by Talend.

the class SnowflakeSourceOrSink method getSchema.

protected Schema getSchema(RuntimeContainer container, Connection connection, String tableName) throws IOException {
    Schema tableSchema = null;
    SnowflakeConnectionProperties connProps = getEffectiveConnectionProperties(container);
    try {
        JDBCTableMetadata tableMetadata = new JDBCTableMetadata();
        tableMetadata.setDatabaseMetaData(connection.getMetaData()).setCatalog(getCatalog(connProps)).setDbSchema(getDbSchema(connProps)).setTablename(tableName);
        tableSchema = getSnowflakeAvroRegistry().inferSchema(tableMetadata);
        if (tableSchema == null)
            throw new IOException(i18nMessages.getMessage("error.tableNotFound", tableName));
    } catch (SQLException se) {
        throw new IOException(se);
    }
    return tableSchema;
}
Also used : JDBCTableMetadata(org.talend.components.common.avro.JDBCTableMetadata) SQLException(java.sql.SQLException) Schema(org.apache.avro.Schema) SnowflakeConnectionProperties(org.talend.components.snowflake.SnowflakeConnectionProperties) IOException(java.io.IOException)

Aggregations

SnowflakeConnectionProperties (org.talend.components.snowflake.SnowflakeConnectionProperties)24 Test (org.junit.Test)16 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 Connection (java.sql.Connection)6 DatabaseMetaData (java.sql.DatabaseMetaData)5 ResultSet (java.sql.ResultSet)5 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 SQLException (java.sql.SQLException)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 SnowflakeSourceOrSink (org.talend.components.snowflake.runtime.SnowflakeSourceOrSink)4 Form (org.talend.daikon.properties.presentation.Form)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Schema (org.apache.avro.Schema)3 DefaultComponentRuntimeContainerImpl (org.talend.components.api.container.DefaultComponentRuntimeContainerImpl)3 RuntimeContainer (org.talend.components.api.container.RuntimeContainer)3 ComponentWizard (org.talend.components.api.wizard.ComponentWizard)2 SnowflakeCloseSourceOrSink (org.talend.components.snowflake.runtime.SnowflakeCloseSourceOrSink)2 TSnowflakeCloseProperties (org.talend.components.snowflake.tsnowflakeclose.TSnowflakeCloseProperties)2 TSnowflakeInputProperties (org.talend.components.snowflake.tsnowflakeinput.TSnowflakeInputProperties)2