Search in sources :

Example 6 with HadoopServerException

use of org.talend.designer.hdfsbrowse.exceptions.HadoopServerException in project tbd-studio-se by Talend.

the class HadoopServerUtil method upload.

/**
 * DOC ycbai Comment method "upload".
 *
 * Upload a local file to the distributed file system
 *
 * @param localFile the file which you want to upload from local
 * @param dfsPath the path on file system which you want to upload.
 * @param fileSystem
 * @param classLoader
 * @param monitor
 * @throws OozieJobDeployException
 */
public static void upload(File localFile, String dfsPath, Object fileSystem, ClassLoader classLoader, IProgressMonitor monitor) throws HadoopServerException {
    if (classLoader == null) {
        classLoader = HadoopServerUtil.class.getClassLoader();
    }
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    monitor.setTaskName("Upload file " + dfsPath);
    BufferedInputStream istream = null;
    DataOutputStream ostream = null;
    Object destPath = null;
    try {
        // $NON-NLS-1$
        destPath = ReflectionUtils.newInstance("org.apache.hadoop.fs.Path", classLoader, new Object[] { dfsPath });
        if (localFile.isDirectory()) {
            // $NON-NLS-1$
            ReflectionUtils.invokeMethod(fileSystem, "mkdirs", new Object[] { destPath });
            monitor.worked(1);
            for (File child : localFile.listFiles()) {
                if (monitor.isCanceled()) {
                    return;
                }
                // $NON-NLS-1$
                upload(child, dfsPath + "/" + child.getName(), fileSystem, classLoader, monitor);
            }
        } else if (localFile.isFile()) {
            istream = new BufferedInputStream(new FileInputStream(localFile));
            // $NON-NLS-1$
            ostream = (DataOutputStream) ReflectionUtils.invokeMethod(fileSystem, "create", new Object[] { destPath });
            int bytes;
            final int taskSize = 1024;
            byte[] buffer = new byte[taskSize];
            while ((bytes = istream.read(buffer)) >= 0) {
                if (monitor.isCanceled()) {
                    return;
                }
                ostream.write(buffer, 0, bytes);
                monitor.worked(1);
            }
        }
    } catch (Exception e) {
        // $NON-NLS-1$
        throw new HadoopServerException(String.format("Unable to upload file %s to %s", localFile, destPath), e);
    } finally {
        try {
            if (istream != null) {
                istream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        // nothing we can do here
        }
        try {
            if (ostream != null) {
                ostream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        // nothing we can do here
        }
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) BufferedInputStream(java.io.BufferedInputStream) DataOutputStream(java.io.DataOutputStream) HadoopServerException(org.talend.designer.hdfsbrowse.exceptions.HadoopServerException) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) HadoopServerException(org.talend.designer.hdfsbrowse.exceptions.HadoopServerException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 7 with HadoopServerException

use of org.talend.designer.hdfsbrowse.exceptions.HadoopServerException in project tbd-studio-se by Talend.

the class HadoopOperationManager method getFileContent.

public InputStream getFileContent(Object fileSystem, Object configuration, ClassLoader classLoader, String filePath) throws HadoopServerException {
    InputStream stream = null;
    if (fileSystem == null) {
        return null;
    }
    ClassLoader oldClassLoaderLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(classLoader);
        Object pathObj = ReflectionUtils.newInstance("org.apache.hadoop.fs.Path", classLoader, new Object[] { filePath });
        Object factory = ReflectionUtils.newInstance("org.apache.hadoop.io.compress.CompressionCodecFactory", classLoader, new Object[] { configuration });
        Object codec = ReflectionUtils.invokeMethod(factory, "getCodec", new Object[] { pathObj });
        if (codec != null) {
            Object originStream = ReflectionUtils.invokeMethod(fileSystem, "open", new Object[] { pathObj });
            stream = (InputStream) ReflectionUtils.invokeMethod(codec, "createInputStream", new Object[] { originStream }, java.io.InputStream.class);
        } else {
            stream = (InputStream) ReflectionUtils.invokeMethod(fileSystem, "open", new Object[] { pathObj });
        }
    } catch (Exception e) {
        throw new HadoopServerException(e);
    } finally {
        Thread.currentThread().setContextClassLoader(oldClassLoaderLoader);
    }
    return stream;
}
Also used : InputStream(java.io.InputStream) HadoopServerException(org.talend.designer.hdfsbrowse.exceptions.HadoopServerException) HadoopServerException(org.talend.designer.hdfsbrowse.exceptions.HadoopServerException)

Example 8 with HadoopServerException

use of org.talend.designer.hdfsbrowse.exceptions.HadoopServerException in project tbd-studio-se by Talend.

the class HadoopOperationManager method getFileSize.

public long getFileSize(Object fileSystem, ClassLoader classLoader, String filePath) throws HadoopServerException {
    long size = 0;
    try {
        Object pathObj = ReflectionUtils.newInstance("org.apache.hadoop.fs.Path", classLoader, new Object[] { filePath });
        Object fileStatus = ReflectionUtils.invokeMethod(fileSystem, "getFileStatus", new Object[] { pathObj });
        size = (Long) ReflectionUtils.invokeMethod(fileStatus, "getLen", new Object[0]);
    } catch (Exception e) {
        throw new HadoopServerException(e);
    }
    return size;
}
Also used : HadoopServerException(org.talend.designer.hdfsbrowse.exceptions.HadoopServerException) HadoopServerException(org.talend.designer.hdfsbrowse.exceptions.HadoopServerException)

Example 9 with HadoopServerException

use of org.talend.designer.hdfsbrowse.exceptions.HadoopServerException in project tbd-studio-se by Talend.

the class HadoopOperationManager method loadHDFSFolderChildren.

public void loadHDFSFolderChildren(HDFSConnectionBean connection, Object fileSystem, ClassLoader classLoader, HDFSPath parent, String path) throws HadoopServerException {
    if (connection == null || fileSystem == null || classLoader == null || parent == null || path == null) {
        return;
    }
    ClassLoader oldClassLoaderLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(classLoader);
        Object pathObj = ReflectionUtils.newInstance("org.apache.hadoop.fs.Path", classLoader, new Object[] { path });
        Object[] statusList = (Object[]) ReflectionUtils.invokeMethod(fileSystem, "listStatus", new Object[] { pathObj });
        if (statusList == null) {
            return;
        }
        for (Object status : statusList) {
            HDFSPath content = null;
            Object statusPath = ReflectionUtils.invokeMethod(status, "getPath", new Object[0]);
            if (statusPath == null) {
                continue;
            }
            String pathName = (String) ReflectionUtils.invokeMethod(statusPath, "getName", new Object[0]);
            if (StringUtils.isBlank(pathName)) {
                continue;
            }
            String absolutePath = ((URI) ReflectionUtils.invokeMethod(statusPath, "toUri", new Object[0])).toString();
            if (StringUtils.isBlank(absolutePath)) {
                continue;
            }
            String relativePath = URI.create(absolutePath).getPath();
            if ((Boolean) ReflectionUtils.invokeMethod(status, "isDir", new Object[0])) {
                content = new HDFSFolder(parent);
            } else {
                content = new HDFSFile(parent);
                content.setTable(createTable(trimFileExtention(pathName)));
            }
            content.setPath(relativePath);
            content.setValue(pathName);
            parent.addChild(content);
        }
    } catch (Exception e) {
        throw new HadoopServerException(e);
    } finally {
        Thread.currentThread().setContextClassLoader(oldClassLoaderLoader);
    }
}
Also used : HDFSFile(org.talend.designer.hdfsbrowse.model.HDFSFile) HDFSPath(org.talend.designer.hdfsbrowse.model.HDFSPath) HDFSFolder(org.talend.designer.hdfsbrowse.model.HDFSFolder) HadoopServerException(org.talend.designer.hdfsbrowse.exceptions.HadoopServerException) URI(java.net.URI) HadoopServerException(org.talend.designer.hdfsbrowse.exceptions.HadoopServerException)

Example 10 with HadoopServerException

use of org.talend.designer.hdfsbrowse.exceptions.HadoopServerException in project tbd-studio-se by Talend.

the class AbstractCheckedServiceProvider method checkService.

@Override
public boolean checkService(final HadoopServiceProperties serviceProperties, final int timeout) throws HadoopServerException {
    boolean checkedOK = true;
    ClassLoader classLoader = getClassLoader(serviceProperties);
    try {
        Thread.currentThread().setContextClassLoader(classLoader);
        ICheckedWorkUnit workUnit = new CheckedWorkUnit() {

            @Override
            protected Object run(ClassLoader cl) throws Exception {
                return check(serviceProperties, cl);
            }
        };
        workUnit.setTimeout(timeout);
        workUnit.setClassLoader(classLoader);
        workUnit.execute();
    } catch (Exception e) {
        checkedOK = false;
        throw new HadoopServerException(e);
    }
    return checkedOK;
}
Also used : DynamicClassLoader(org.talend.core.classloader.DynamicClassLoader) HadoopServerException(org.talend.designer.hdfsbrowse.exceptions.HadoopServerException) MalformedURLException(java.net.MalformedURLException) HadoopServerException(org.talend.designer.hdfsbrowse.exceptions.HadoopServerException)

Aggregations

HadoopServerException (org.talend.designer.hdfsbrowse.exceptions.HadoopServerException)11 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 IOException (java.io.IOException)4 DynamicClassLoader (org.talend.core.classloader.DynamicClassLoader)3 URI (java.net.URI)2 BufferedInputStream (java.io.BufferedInputStream)1 DataOutputStream (java.io.DataOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 Entry (java.util.Map.Entry)1 ExecutorService (java.util.concurrent.ExecutorService)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 IHadoopClusterService (org.talend.core.hadoop.IHadoopClusterService)1 ConnectionStatus (org.talend.core.repository.model.connection.ConnectionStatus)1 ContextType (org.talend.designer.core.model.utils.emf.talendfile.ContextType)1 HadoopServiceProperties (org.talend.designer.hdfsbrowse.hadoop.service.HadoopServiceProperties)1 CheckedKnoxNamenodeProvider (org.talend.designer.hdfsbrowse.hadoop.service.check.provider.CheckedKnoxNamenodeProvider)1 HDFSFile (org.talend.designer.hdfsbrowse.model.HDFSFile)1