Example usage for org.apache.hadoop.fs Path toString

List of usage examples for org.apache.hadoop.fs Path toString

Introduction

In this page you can find the example usage for org.apache.hadoop.fs Path toString.

Prototype

@Override
    public String toString() 

Source Link

Usage

From source file:com.ibm.stocator.fs.swift.SwiftAPIClient.java

License:Open Source License

/**
 * Merge between two paths//  w w  w  . j a va 2  s  . c o  m
 *
 * @param hostName
 * @param p path
 * @param objectName
 * @return merged path
 */
private String getMergedPath(String hostName, Path p, String objectName) {
    if ((p.getParent() != null) && (p.getName() != null) && (p.getParent().toString().equals(hostName))) {
        if (objectName.equals(p.getName())) {
            return p.toString();
        }
        if (objectName.startsWith(p.getName())) {
            return p.getParent() + objectName;
        }
        return p.toString();
    }
    return hostName + objectName;
}

From source file:com.ibm.stocator.fs.swift.SwiftAPIClient.java

License:Open Source License

@Override
public boolean delete(String hostName, Path path, boolean recursive) throws IOException {
    String obj = path.toString();
    if (path.toString().startsWith(hostName)) {
        obj = path.toString().substring(hostName.length());
    }//from www.  j  a v  a  2  s.c  o  m
    LOG.debug("Object name to delete {}. Path {}", obj, path.toString());
    StoredObject so = mAccount.getContainer(container).getObject(obj);
    if (so.exists()) {
        so.delete();
    }
    return true;
}

From source file:com.ibm.stocator.fs.swift.SwiftAPIDirect.java

License:Open Source License

/**
 * GET object/*from  w ww  . ja v  a 2 s.  c  o  m*/
 *
 * @param path path to the object
 * @param account Joss Account wrapper object
 * @param bytesFrom from from
 * @param bytesTo bytes to
 * @param scm Swift Connection manager
 * @return SwiftInputStreamWrapper that includes input stream and length
 * @throws IOException if network errors
 */
public static SwiftInputStreamWrapper getObject(Path path, JossAccount account, long bytesFrom, long bytesTo,
        SwiftConnectionManager scm) throws IOException {
    Tuple<Integer, Tuple<HttpRequestBase, HttpResponse>> resp = httpGET(path.toString(), bytesFrom, bytesTo,
            account, scm);
    if (resp.x.intValue() >= 400) {
        LOG.warn("Re-authentication attempt for GET {}", path.toString());
        account.authenticate();
        resp = httpGET(path.toString(), bytesFrom, bytesTo, account, scm);
    }

    SwiftInputStreamWrapper httpStream = new SwiftInputStreamWrapper(resp.y.y.getEntity(), resp.y.x);
    return httpStream;
}

From source file:com.ibm.stocator.fs.swift.SwiftAPIDirect.java

License:Open Source License

public static long getTempUrlObjectLength(Path path, SwiftConnectionManager scm) throws IOException {

    HttpHead head = new HttpHead(path.toString().replace("swift2d", "https"));
    CloseableHttpResponse response = scm.createHttpConnection().execute(head);
    return Long.parseLong(response.getFirstHeader("Content-Length").getValue());
}

From source file:com.ibm.stocator.fs.swift.SwiftInputStream.java

License:Open Source License

public SwiftInputStream(SwiftAPIClient storeNative, String hostName, Path path) throws IOException {
    this(storeNative);
    LOG.debug("init: {}", path.toString());
    String objectName = path.toString().substring(hostName.length());
    storedObject = nativeStore.getAccount().getContainer(nativeStore.getDataRoot()).getObject(objectName);
    if (!storedObject.exists()) {
        throw new FileNotFoundException(objectName + " is not exists");
    }/*  ww  w.  j  a  v  a2 s .  c om*/
}

From source file:com.ikanow.aleph2.aleph2_rest_utils.DataStoreCrudService.java

License:Apache License

@Override
public CompletableFuture<Boolean> deleteObjectById(Object id) {
    try {//from  w ww .ja v  a  2  s .  com
        final Path path = new Path(output_directory + id.toString());
        _logger.debug("Trying to delete: " + path.toString());
        final boolean delete_success = fileContext.delete(path, false); //this is always returning false
        _logger.debug("success deleteing: " + delete_success);
        return CompletableFuture.completedFuture(!doesPathExist(path, fileContext)); //if file does not exist, delete was a success
    } catch (IllegalArgumentException | IOException e) {
        final CompletableFuture<Boolean> fut = new CompletableFuture<Boolean>();
        fut.completeExceptionally(e);
        return fut;
    }
}

From source file:com.ikanow.aleph2.analytics.hadoop.assets.BeFileInputReader.java

License:Open Source License

/** For input files (pure enrichment, not when used for analytics), deletes or archives the files following completion
 *///  ww  w  .j  av  a2s. c o  m
private void archiveOrDeleteFile() {
    try {
        final Path currentPath = _fileSplit.getPath(_currFile);
        // First check - if only want to do anything if this is an internal job:
        if (!currentPath.toString().contains(IStorageService.TO_IMPORT_DATA_SUFFIX)) {
            return; // (not your file to modify....)
        }

        final boolean storage_enabled = Optional.ofNullable(_dataBucket.data_schema())
                .map(ds -> ds.storage_schema()).map(ss -> Optional.ofNullable(ss.enabled()).orElse(true))
                .orElse(false);

        final boolean archive_enabled = storage_enabled
                && Optionals.of(() -> _dataBucket.data_schema().storage_schema().raw())
                        .map(raw -> Optional.ofNullable(raw.enabled()).orElse(true)).orElse(false);

        if (archive_enabled) {
            Path newPath = createArchivePath(currentPath);
            _fs.mkdirs(newPath);

            @SuppressWarnings("unused")
            final boolean success = _fs.rename(currentPath,
                    Path.mergePaths(newPath, new Path("/" + currentPath.getName())));
        } else {
            _fs.delete(currentPath, false);
        }
    } catch (Exception e) {
        logger.error(ErrorUtils.getLongForm(HadoopErrorUtils.EXCEPTION_CAUGHT, e));
        // We're just going to move on if we can't delete the file, it's
        // probably a permissions error
    }
}

From source file:com.ikanow.aleph2.core.shared.utils.DirUtils.java

License:Apache License

/** This method returns the path to the first subdirectory matching the subDirectoryName parameter or null if not found.
* @param fileContext/* w w  w.  j av a 2  s. co m*/
* @param start
* @param subDirectoryName
* @return
*/
public static Path findOneSubdirectory(FileContext fileContext, Path start, String subDirectoryName) {
    Path p = null;
    try {
        logger.debug("findOneSubdirectory :" + start.toString());
        FileStatus[] statuss = fileContext.util().listStatus(start);
        for (int i = 0; i < statuss.length; i++) {
            FileStatus dir = statuss[i];
            logger.debug("FileStatus:" + statuss[i].getPath().toString());
            if (dir.isDirectory()) {
                if (dir.getPath().getName().contains(subDirectoryName)) {
                    logger.debug("findOneSubdirectory match:" + dir.getPath().getName());
                    return dir.getPath();
                } else {
                    p = findOneSubdirectory(fileContext, dir.getPath(), subDirectoryName);
                    if (p != null) {
                        return p;
                    }
                }
            }
        }

    } catch (Exception e) {
        logger.error("findOneSubdirectory Caught Exception", e);
    }

    return p;
}

From source file:com.ikanow.aleph2.core.shared.utils.DirUtils.java

License:Apache License

/**
 * @param allPaths//from w ww . j  av  a2  s . c o m
 * @param fileContext
 * @param start
 * @param subDirectoryName
 * @param includeMatched
 */
public static void findAllSubdirectories(List<Path> allPaths, FileContext fileContext, Path start,
        String subDirectoryName, boolean includeMatched) {
    try {
        logger.debug("findAllSubdirectories :" + start.toString());
        FileStatus[] statuss = fileContext.util().listStatus(start);
        for (int i = 0; i < statuss.length; i++) {
            FileStatus dir = statuss[i];
            logger.debug("FileStatus:" + statuss[i].getPath().toString());
            if (dir.isDirectory()) {
                if (dir.getPath().getName().contains(subDirectoryName)) {
                    logger.debug("findOneSubdirectory match:" + dir.getPath().getName());
                    if (includeMatched) {
                        allPaths.add(dir.getPath());
                    } else {
                        allPaths.add(dir.getPath().getParent());
                    }
                } else {
                    findAllSubdirectories(allPaths, fileContext, dir.getPath(), subDirectoryName,
                            includeMatched);
                }
            }
        }

    } catch (Exception e) {
        logger.error("findAllSubdirectories Caught Exception", e);
    }
}

From source file:com.ikanow.aleph2.core.shared.utils.JarCacheUtils.java

License:Apache License

/** Moves a shared JAR into a local spot (if required)
 * @param library_bean// w w w.  j a v a 2s.  c  o  m
 * @param fs
 * @return either a basic message bean containing an error, or the fully qualified path of the cached JAR
 */
public static <M> CompletableFuture<Validation<BasicMessageBean, String>> getCachedJar(
        final String local_cached_jar_dir, final SharedLibraryBean library_bean, final IStorageService fs,
        final String handler_for_errors, final M msg_for_errors) {
    try {
        final FileContext dfs = fs.getUnderlyingPlatformDriver(FileContext.class, Optional.empty()).get();
        final FileContext lfs = fs.getUnderlyingPlatformDriver(FileContext.class, IStorageService.LOCAL_FS)
                .get();

        final Path cached_jar_file = lfs
                .makeQualified(new Path(local_cached_jar_dir + "/" + buildCachedJarName(library_bean)));
        final Path original_jar_file = dfs.makeQualified(new Path(library_bean.path_name()));

        final FileStatus file_status = dfs.getFileStatus(original_jar_file); // (this will exception out if it doesn't exist, as it should)

        try {
            final FileStatus local_file_status = lfs.getFileStatus(cached_jar_file); // (this will exception in to case 2 if it doesn't exist)

            // if the local version exists then overwrite it

            if (file_status.getModificationTime() > local_file_status.getModificationTime()) {
                // (it gets kinda complicated here so just invalidate the entire classloader cache..)
                // TODO (ALEPH-12): add a coverage test for this
                ClassloaderUtils.clearCache();

                lfs.util().copy(original_jar_file, cached_jar_file, false, true);
            }
        } catch (FileNotFoundException f) {

            // 2) if the local version doesn't exist then just copy the distributed file across
            // (note: don't need to do anything with the classloader cache here since the file doesn't exist so can't have a cache key)

            lfs.util().copy(original_jar_file, cached_jar_file);
        }
        return CompletableFuture.completedFuture(Validation.success(cached_jar_file.toString()));

    } catch (Throwable e) {
        return CompletableFuture.completedFuture(
                Validation.fail(SharedErrorUtils.buildErrorMessage(handler_for_errors, msg_for_errors,
                        SharedErrorUtils.getLongForm(SharedErrorUtils.SHARED_LIBRARY_NAME_NOT_FOUND, e,
                                library_bean.path_name()))));
    }
}