Search in sources :

Example 31 with ComponentException

use of org.talend.components.api.exception.ComponentException in project components by Talend.

the class AzureStoragePutRuntime method upload.

private void upload(RuntimeContainer runtimeContainer) {
    AzureStorageUtils utils = new AzureStorageUtils();
    List<Map<String, String>> list = new ArrayList<>();
    // process files list
    if (useFileList && files != null && files.size() > 0) {
        for (int idx = 0; idx < files.fileMask.getValue().size(); idx++) {
            String fileMask = files.fileMask.getValue().get(idx);
            String newName = files.newName.getValue().get(idx);
            Map<String, String> map = new HashMap<>();
            map.put(fileMask, newName);
            list.add(map);
        }
    }
    Map<String, String> fileMap;
    if (useFileList) {
        fileMap = utils.genFileFilterList(list, localFolder, remoteFolder);
    } else {
        fileMap = utils.genAzureObjectList(new File(localFolder), remoteFolder);
    }
    for (Map.Entry<String, String> entry : fileMap.entrySet()) {
        File source = new File(entry.getKey());
        try (FileInputStream stream = new FileInputStream(source)) {
            // see try-with-resources concept
            // TODO Any Action ??? if remoteFolder doesn't exist it will fail...
            azureStorageBlobService.upload(containerName, entry.getValue(), stream, source.length());
        } catch (StorageException | URISyntaxException | IOException | InvalidKeyException e) {
            LOGGER.error(e.getLocalizedMessage());
            if (dieOnError) {
                throw new ComponentException(e);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) FileInputStream(java.io.FileInputStream) ComponentException(org.talend.components.api.exception.ComponentException) AzureStorageUtils(org.talend.components.azurestorage.utils.AzureStorageUtils) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) StorageException(com.microsoft.azure.storage.StorageException)

Example 32 with ComponentException

use of org.talend.components.api.exception.ComponentException in project components by Talend.

the class AzureStorageSourceOrSink method getSchemaNames.

@Override
public List<NamedThing> getSchemaNames(RuntimeContainer container) throws IOException {
    List<NamedThing> result = new ArrayList<>();
    try {
        CloudStorageAccount storageAccount = getAzureConnection(container).getCloudStorageAccount();
        CloudBlobClient client = storageAccount.createCloudBlobClient();
        for (CloudBlobContainer c : client.listContainers()) {
            result.add(new SimpleNamedThing(c.getName(), c.getName()));
        }
    } catch (InvalidKeyException | URISyntaxException e) {
        throw new ComponentException(e);
    }
    return result;
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) SimpleNamedThing(org.talend.daikon.SimpleNamedThing) ArrayList(java.util.ArrayList) CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) ComponentException(org.talend.components.api.exception.ComponentException) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) URISyntaxException(java.net.URISyntaxException) NamedThing(org.talend.daikon.NamedThing) SimpleNamedThing(org.talend.daikon.SimpleNamedThing) InvalidKeyException(java.security.InvalidKeyException)

Example 33 with ComponentException

use of org.talend.components.api.exception.ComponentException in project components by Talend.

the class AzureStorageListReader method start.

@Override
public boolean start() throws IOException {
    String mycontainer = properties.container.getValue();
    List<CloudBlob> blobs = new ArrayList<>();
    // build a list with remote blobs to fetch
    List<RemoteBlob> remoteBlobs = ((AzureStorageSource) getCurrentSource()).getRemoteBlobs();
    try {
        for (RemoteBlob rmtb : remoteBlobs) {
            for (ListBlobItem blob : azureStorageBlobService.listBlobs(mycontainer, rmtb.prefix, rmtb.include)) {
                if (blob instanceof CloudBlob) {
                    blobs.add((CloudBlob) blob);
                }
            }
        }
        startable = !blobs.isEmpty();
        blobsIterator = blobs.iterator();
    } catch (StorageException | URISyntaxException | InvalidKeyException e) {
        LOGGER.error(e.getLocalizedMessage());
        if (properties.dieOnError.getValue()) {
            throw new ComponentException(e);
        }
    }
    if (startable) {
        dataCount++;
        currentBlob = blobsIterator.next();
        IndexedRecord dataRecord = new GenericData.Record(properties.schema.schema.getValue());
        dataRecord.put(0, currentBlob.getName());
        Schema rootSchema = RootSchemaUtils.createRootSchema(properties.schema.schema.getValue(), properties.outOfBandSchema);
        currentRecord = new GenericData.Record(rootSchema);
        currentRecord.put(0, dataRecord);
        currentRecord.put(1, dataRecord);
    }
    return startable;
}
Also used : ListBlobItem(com.microsoft.azure.storage.blob.ListBlobItem) IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) InvalidKeyException(java.security.InvalidKeyException) GenericData(org.apache.avro.generic.GenericData) CloudBlob(com.microsoft.azure.storage.blob.CloudBlob) ComponentException(org.talend.components.api.exception.ComponentException) RemoteBlob(org.talend.components.azurestorage.blob.helpers.RemoteBlob) IndexedRecord(org.apache.avro.generic.IndexedRecord) StorageException(com.microsoft.azure.storage.StorageException)

Example 34 with ComponentException

use of org.talend.components.api.exception.ComponentException in project components by Talend.

the class AzureStorageBlobService method createContainerIfNotExist.

/**
 * This method create an azure container if it doesn't exist and set it access policy
 *
 * @param containerName : the name of the container to be created
 * @return true if the container was created, false otherwise
 */
public boolean createContainerIfNotExist(final String containerName) throws StorageException, URISyntaxException, InvalidKeyException {
    CloudBlobClient cloudBlobClient = connection.getCloudStorageAccount().createCloudBlobClient();
    CloudBlobContainer cloudBlobContainer = cloudBlobClient.getContainerReference(containerName);
    boolean containerCreated;
    try {
        containerCreated = cloudBlobContainer.createIfNotExists();
    } catch (StorageException e) {
        if (!e.getErrorCode().equals(StorageErrorCodeStrings.CONTAINER_BEING_DELETED)) {
            throw e;
        }
        LOGGER.warn(messages.getMessage("error.CONTAINER_BEING_DELETED", containerName));
        // See https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/delete-container
        try {
            Thread.sleep(40000);
        } catch (InterruptedException eint) {
            LOGGER.error(messages.getMessage("error.InterruptedException"));
            throw new ComponentException(eint);
        }
        containerCreated = cloudBlobContainer.createIfNotExists();
        LOGGER.debug(messages.getMessage("debug.ContainerCreated", containerName));
    }
    return containerCreated;
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) ComponentException(org.talend.components.api.exception.ComponentException) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) StorageException(com.microsoft.azure.storage.StorageException)

Example 35 with ComponentException

use of org.talend.components.api.exception.ComponentException in project components by Talend.

the class HadoopCMClusterService method getConfFileContent.

@Override
public String getConfFileContent(String confFileName) {
    Configuration conf = confs.get(confFileName);
    if (conf == null) {
        return null;
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        conf.writeXml(baos);
    } catch (IOException e) {
        throw new ComponentException(e);
    }
    try {
        return baos.toString("UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new ComponentException(e);
    }
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) ComponentException(org.talend.components.api.exception.ComponentException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

ComponentException (org.talend.components.api.exception.ComponentException)101 URL (java.net.URL)32 MalformedURLException (java.net.MalformedURLException)30 JarRuntimeInfo (org.talend.components.api.component.runtime.JarRuntimeInfo)27 IOException (java.io.IOException)18 ArrayList (java.util.ArrayList)17 InvalidKeyException (java.security.InvalidKeyException)14 Schema (org.apache.avro.Schema)14 URISyntaxException (java.net.URISyntaxException)12 StorageException (com.microsoft.azure.storage.StorageException)11 NamedThing (org.talend.daikon.NamedThing)11 ValidationResult (org.talend.daikon.properties.ValidationResult)10 SandboxedInstance (org.talend.daikon.sandbox.SandboxedInstance)10 IndexedRecord (org.apache.avro.generic.IndexedRecord)8 Test (org.junit.Test)8 SimpleNamedThing (org.talend.daikon.SimpleNamedThing)8 NetSuiteException (org.talend.components.netsuite.client.NetSuiteException)6 GeneralSecurityException (java.security.GeneralSecurityException)5 JDBCSource (org.talend.components.jdbc.runtime.JDBCSource)5 TJDBCInputDefinition (org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition)5