Example usage for org.apache.hadoop.fs FileSystem close

List of usage examples for org.apache.hadoop.fs FileSystem close

Introduction

In this page you can find the example usage for org.apache.hadoop.fs FileSystem close.

Prototype

@Override
public void close() throws IOException 

Source Link

Document

Close this FileSystem instance.

Usage

From source file:tv.icntv.log.crawl.store.HdfsDefaultStore.java

License:Apache License

@Override
public boolean isExist(String name) {
    if (isNull(name)) {
        return false;
    }/*from  w  ww .j a  v  a  2s  .  c om*/
    FileSystem fileSystem = null;
    try {
        fileSystem = FileSystem.get(configuration);
        return fileSystem.exists(new Path(name));
    } catch (IOException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        return false;
    } finally {
        if (null != fileSystem) {
            try {
                fileSystem.close();
            } catch (IOException e) {
                e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }
}

From source file:tv.icntv.log.crawl.store.HdfsDefaultStore.java

License:Apache License

@Override
public void createFile(String name) {
    if (isNull(name)) {
        return;/*from ww w.ja v a 2s  .c  om*/
    }
    FSDataOutputStream out = null;
    FileSystem fileSystem = null;
    try {
        Path path = new Path(name);
        fileSystem = FileSystem.get(configuration);
        if (fileSystem.exists(path)) {
            return;
        }
        out = fileSystem.create(path);
        out.flush();
        return;
    } catch (IOException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        return;
    } finally {

        IOUtils.closeStream(out);
        if (null != fileSystem) {
            try {
                fileSystem.close();
            } catch (IOException e) {
                e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }
}

From source file:tv.icntv.log.crawl.store.HdfsDefaultStore.java

License:Apache License

@Override
public boolean rename(String srcName, String name) {
    FSDataOutputStream out = null;/*from   w  w  w.j  av  a 2  s .  c  o m*/
    FileSystem fileSystem = null;
    try {
        fileSystem = FileSystem.get(configuration);
        if (fileSystem.exists(new Path(srcName))) {
            return fileSystem.rename(new Path(srcName), new Path(name));
        }
        logger.info("try rename ,but name={} not exist,create file{} ", srcName, name);
        out = fileSystem.create(new Path(name));
        out.flush();
        return false;
    } catch (IOException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        logger.error("rename error:", e);
        return false;
    } finally {
        if (null != out) {
            IOUtils.closeStream(out);
        }
        if (null != fileSystem) {
            try {
                fileSystem.close();
            } catch (IOException e) {
                e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }
}

From source file:tv.icntv.log.crawl.store.HdfsDefaultStore.java

License:Apache License

@Override
public void delete(String name) {
    FileSystem fileSystem = null;
    try {/* w ww  .j  av  a  2s .c o  m*/
        fileSystem = FileSystem.get(configuration);
        if (fileSystem.exists(new Path(name))) {
            fileSystem.delete(new Path(name), true);
        }

    } catch (IOException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        logger.error("rename error:", e);

    } finally {
        if (null != fileSystem) {
            try {
                fileSystem.close();
            } catch (IOException e) {
                e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }
}

From source file:tv.icntv.log.crawl.store.HdfsDefaultStore.java

License:Apache License

@Override
public long getSize(String file) {
    FileSystem fileSystem = null;
    if (null == file || file.equals("")) {
        return 0;
    }/*from   w w  w. j a  v a 2s. c o  m*/
    try {
        fileSystem = FileSystem.get(configuration);
        Path path = new Path(file);
        if (fileSystem.exists(path)) {
            FileStatus fileStatus = fileSystem.getFileStatus(new Path(file));
            return fileStatus.getLen();
        }

    } catch (Exception e) {
        e.printStackTrace();
        return 0;
    } finally {
        if (null != fileSystem) {
            try {
                fileSystem.close();
            } catch (IOException e) {
                e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }

    return 0; //To change body of implemented methods use File | Settings | File Templates.
}

From source file:tv.icntv.log.stb.commons.HadoopUtils.java

License:Apache License

public static Path[] createFile(Path from, Path to, PathFilter filter, String fromSuffix, String toSuffix,
        String parsed) throws IOException {
    FileSystem fileSystem = null;
    try {/*from w w  w .  j  av  a2  s.c om*/
        fileSystem = FileSystem.get(configuration);
        Path[] paths = FileUtil.stat2Paths(fileSystem.listStatus(from, filter));
        List<Path> inputs = Lists.newArrayList();
        for (Path path : paths) {
            //file name
            String name = path.getName().replace(fromSuffix, "");

            if (isExist(new Path(to, name.concat(parsed)))) {
                continue;
            }

            if (createFile(new Path(to, name.concat(toSuffix)))) {
                inputs.add(new Path(from, name));
            }
            ;
        }
        return inputs.toArray(new Path[inputs.size()]);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } finally {
        if (null != fileSystem) {
            fileSystem.close();
        }
    }
}

From source file:tv.icntv.log.stb.commons.HadoopUtils.java

License:Apache License

public static boolean createFile(Path path) {
    FSDataOutputStream out = null;/*from   w w w.j  av a  2  s. c  o  m*/
    FileSystem fileSystem = null;
    try {

        fileSystem = FileSystem.get(configuration);
        if (fileSystem.exists(path)) {
            logger.info("file {} existed", path.toString());
            return false;
        }
        out = fileSystem.create(path);
        out.flush();
        return true;
    } catch (IOException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        logger.error("create File error!");
        return false;
    } finally {

        IOUtils.closeStream(out);
        if (null != fileSystem) {
            try {
                fileSystem.close();
            } catch (IOException e) {
                e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }
}

From source file:tv.icntv.log.stb.commons.HadoopUtils.java

License:Apache License

/**
 * mv/*  w  w  w . java 2  s . c o  m*/
 * @param from
 * @param to
 * @throws IOException
 */
public static void rename(Path from, Path to) throws IOException {
    FileSystem fileSystem = null;
    try {
        fileSystem = FileSystem.get(configuration);
        fileSystem.rename(from, to);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (null != fileSystem) {
            fileSystem.close();
        }
    }
}

From source file:tv.icntv.log.stb.commons.HadoopUtils.java

License:Apache License

public static boolean isLzo(Path path) throws IOException {
    FileSystem fileSystem = null;
    try {//from  www  . jav a  2s.com
        fileSystem = FileSystem.get(configuration);
        Path[] paths = FileUtil.stat2Paths(fileSystem.listStatus(path));
        for (Path p : paths) {
            if (!p.getName().contains(".lzo")) {
                return false;
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (null != fileSystem) {
            fileSystem.close();
        }
    }
    return true;
}

From source file:tv.icntv.recommend.common.HadoopUtils.java

License:Apache License

public static void mkdirIfNotExist(Path path) throws IOException {
    FileSystem fileSystem = null;
    try {/*from  w w  w .j a v a  2 s.c o  m*/
        fileSystem = FileSystem.get(configuration);
        if (!fileSystem.exists(path)) {
            fileSystem.mkdirs(path);
        }
        ;
    } catch (Exception e) {
        return;
    } finally {
        if (null != fileSystem) {
            fileSystem.close();
        }
    }
}