/**
* 判断是否为文件
* @author julong
* @date 2016-11-24 下午04:44:31
*/
public static void isFile(){
try {
//创建文件配置对象
Configuration configuration = new Configuration();
//定义URL
URI uri = URI.create("hdfs://192.168.205.110:9000/");
//获取文件系统
FileSystem fileSystem = FileSystem.get(uri,configuration);
Path path = new Path("/user/input/test");
boolean result = fileSystem.isFile(path);
logger.info("是否为文件:"+result);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}

/**
* 判断是否存在此文件或者目录
* @author julong
* @date 2016-11-24 下午04:47:12
*/
public static void exists(){
try {
//创建文件配置对象
Configuration configuration = new Configuration();
//定义URL
URI uri = URI.create("hdfs://192.168.205.110:9000/");
//获取文件系统
FileSystem fileSystem = FileSystem.get(uri,configuration);
Path path = new Path("/user/input/test");
boolean result = fileSystem.exists(path);
logger.info("判断是否存在此文件或者目录:"+result);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}

/**
* 复制本地文件到HDFS
* @author julong
* @date 2016-11-24 下午05:06:27
*/
public static void copyFromLocalFile(){
try {
//创建文件配置对象
Configuration configuration = new Configuration();
//定义URL
URI uri = URI.create("hdfs://192.168.205.110:9000/");
//获取文件系统
FileSystem fileSystem = FileSystem.get(uri,configuration);
fileSystem.copyFromLocalFile( new Path("C://maven.txt"),new Path("/user/input/maven.txt"));
logger.info("本地复制文件到HDFS成功!");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}

/**
* 复制文件到本地
* @author julong
* @date 2016-11-24 下午05:26:56
*/
public static void copyToLocalFile(){
try {
//创建文件配置对象
Configuration configuration = new Configuration();
//定义URL
URI uri = URI.create("hdfs://192.168.205.110:9000/");
//获取文件系统
FileSystem fileSystem = FileSystem.get(uri,configuration);
FSDataInputStream fsDataInputStream = fileSystem.open(new Path("/user/input/LICENSE.txt"));
OutputStream outputStream = new FileOutputStream(new File("C:\\LICENSE.txt"));
IOUtils.copyBytes(fsDataInputStream, outputStream, 4096);
logger.info("完成文件复制");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}