Search in sources :

Example 1 with KnoxSession

use of org.apache.knox.gateway.shell.KnoxSession in project knox by apache.

the class WebHDFSCommand method remove.

private String remove(Map<String, String> mounts, String path) {
    String mountPoint = determineMountPoint(path);
    KnoxSession session = getSessionForMountPoint(mounts, mountPoint);
    if (session != null) {
        String targetPath = determineTargetPath(path, mountPoint);
        try {
            Hdfs.rm(session).file(targetPath).now().getString();
        } catch (KnoxShellException | IOException e) {
            e.printStackTrace();
        }
    } else {
        return "No session established for mountPoint: " + mountPoint + " Use :fs mount {topology-url} {mountpoint-name}";
    }
    return "Successfully removed: " + path;
}
Also used : KnoxSession(org.apache.knox.gateway.shell.KnoxSession) IOException(java.io.IOException) KnoxShellException(org.apache.knox.gateway.shell.KnoxShellException)

Example 2 with KnoxSession

use of org.apache.knox.gateway.shell.KnoxSession in project knox by apache.

the class WebHDFSCommand method mount.

private String mount(Map<String, String> mounts, String url, String mountPoint) {
    KnoxSession session = establishSession(mountPoint, url);
    if (session != null) {
        mounts.put(mountPoint, url);
        KnoxSession.persistMountPoints(mounts);
        return url + " mounted as " + mountPoint;
    }
    return "Failed to mount " + url + " as " + mountPoint;
}
Also used : KnoxSession(org.apache.knox.gateway.shell.KnoxSession)

Example 3 with KnoxSession

use of org.apache.knox.gateway.shell.KnoxSession in project knox by apache.

the class WebHDFSCommand method establishSession.

private KnoxSession establishSession(String mountPoint, String url) {
    CredentialCollector dlg;
    try {
        dlg = login();
    } catch (CredentialCollectionException e) {
        e.printStackTrace();
        return null;
    }
    String username = dlg.name();
    String password = new String(dlg.chars());
    KnoxSession session = null;
    try {
        session = KnoxSession.login(url, username, password);
        sessions.put(mountPoint, session);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    return session;
}
Also used : CredentialCollector(org.apache.knox.gateway.shell.CredentialCollector) CredentialCollectionException(org.apache.knox.gateway.shell.CredentialCollectionException) KnoxSession(org.apache.knox.gateway.shell.KnoxSession) URISyntaxException(java.net.URISyntaxException)

Example 4 with KnoxSession

use of org.apache.knox.gateway.shell.KnoxSession in project knox by apache.

the class WebHDFSCommand method cat.

private String cat(Map<String, String> mounts, String path) {
    String response = null;
    String mountPoint = determineMountPoint(path);
    KnoxSession session = getSessionForMountPoint(mounts, mountPoint);
    if (session != null) {
        String targetPath = determineTargetPath(path, mountPoint);
        try {
            String contents = Hdfs.get(session).from(targetPath).now().getString();
            response = contents;
        } catch (KnoxShellException | IOException e) {
            e.printStackTrace();
            response = "Exception ocurred: " + e.getMessage();
        }
    } else {
        response = "No session established for mountPoint: " + mountPoint + " Use :fs mount {topology-url} {mountpoint-name}";
    }
    return response;
}
Also used : KnoxSession(org.apache.knox.gateway.shell.KnoxSession) IOException(java.io.IOException) KnoxShellException(org.apache.knox.gateway.shell.KnoxShellException)

Example 5 with KnoxSession

use of org.apache.knox.gateway.shell.KnoxSession in project knox by apache.

the class WebHDFSCommand method put.

private String put(Map<String, String> mounts, String localFile, String path, int permission) {
    String mountPoint = determineMountPoint(path);
    KnoxSession session = getSessionForMountPoint(mounts, mountPoint);
    if (session != null) {
        String targetPath = determineTargetPath(path, mountPoint);
        try {
            boolean overwrite = false;
            if (exists(session, targetPath)) {
                if (collectClearInput(targetPath + " already exists would you like to overwrite (Y/n)").equalsIgnoreCase("y")) {
                    overwrite = true;
                }
            }
            Hdfs.put(session).file(localFile).to(targetPath).overwrite(overwrite).permission(permission).now().getString();
        } catch (IOException e) {
            e.printStackTrace();
            return "Exception ocurred: " + e.getMessage();
        }
    } else {
        return "No session established for mountPoint: " + mountPoint + " Use :fs mount {topology-url} {mountpoint-name}";
    }
    return "Successfully put: " + localFile + " to: " + path;
}
Also used : KnoxSession(org.apache.knox.gateway.shell.KnoxSession) IOException(java.io.IOException)

Aggregations

KnoxSession (org.apache.knox.gateway.shell.KnoxSession)22 IOException (java.io.IOException)12 Callable (java.util.concurrent.Callable)6 Test (org.junit.Test)6 KnoxShellException (org.apache.knox.gateway.shell.KnoxShellException)5 URISyntaxException (java.net.URISyntaxException)2 HttpEntity (org.apache.http.HttpEntity)2 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)2 HttpPost (org.apache.http.client.methods.HttpPost)2 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)2 CredentialCollectionException (org.apache.knox.gateway.shell.CredentialCollectionException)2 HashMap (java.util.HashMap)1 CredentialCollector (org.apache.knox.gateway.shell.CredentialCollector)1 KnoxShellTable (org.apache.knox.gateway.shell.table.KnoxShellTable)1