Hadoop的Java Api操作

hdfs的一些读写。

 

 

/**
	 * 创建一个新文件(此程序可以升级putMerge功能)
	 * @param filename
	 * @param content
	 * @throws IOException
	 */
	public static void newFile(String filename, byte[] content) throws IOException{
		Configuration conf = new Configuration();
		FileSystem fileSystem = FileSystem.get(conf);

		Path filePath = new Path(filename);
		FSDataOutputStream outputStream = fileSystem.create(filePath);

		outputStream.write(content);
		outputStream.close();
		fileSystem.close();
		System.out.println("创建文件成功!");
	}

	/**
	 * 上传文件
	 * @param localPath
	 * @param hdfsPath
	 * @throws IOException
	 */
	public static void uploadFile(String localPath, String hdfsPath) throws IOException{
		Configuration configuration = new Configuration();
		FileSystem fileSystem = FileSystem.get(configuration);

		Path src = new Path(localPath);
		Path dst = new Path(hdfsPath);
		//fileSystem.copyFromLocalFile(src, dst);
		//第一个false,是否删除原文件;第二个false,是否overwrite。
		fileSystem.copyFromLocalFile(false, false, src, dst);
		fileSystem.close();
	}

	/**
	 * 重命名
	 * @param oldName
	 * @param newName
	 * @throws IOException
	 */
	public static void rename(String oldName,String newName) throws IOException{
		Configuration configuration = new Configuration();
		FileSystem fileSystem = FileSystem.get(configuration);

		Path oldPath = new Path(oldName);
		Path newPath = new Path(newName);

		boolean isSuccess = fileSystem.rename(oldPath, newPath);
		if (isSuccess) {
			System.err.println("成功");
		}
		else {
			System.err.println("失败");
		}
		fileSystem.close();
	}

	/**
	 * 删除文件操作
	 * @param filePath
	 * @throws IOException
	 */
	public static void delete(String filePath) throws IOException{
		Configuration configuration = new Configuration();
		FileSystem fileSystem = FileSystem.get(configuration);
		Path hdfspath = new Path(filePath);
		fileSystem.deleteOnExit(hdfspath);
		fileSystem.close();
	}

	/**
	 * 创建目录
	 * @throws IOException
	 */
	public static void mkdir(String dirPath) throws IOException{
		Configuration configuration = new Configuration();
		FileSystem fileSystem = FileSystem.get(configuration);
		Path path = new Path(dirPath);
		boolean isSuccess = fileSystem.mkdirs(path);
		if (isSuccess) {
			System.err.println("创建目录成功!");
		}else {
			System.err.println("创建目录失败!");
		}
		fileSystem.close();
	}

	public static void readFile(String filePath) throws IOException{
		Configuration configuration = new Configuration();
		FileSystem fs = FileSystem.get(configuration);
		Path path = new Path(filePath);
		InputStream in = null;
		in = fs.open(path);
		IOUtils.copyBytes(in, System.out, 4096);
		IOUtils.closeStream(in);
		fs.close();//TODO: ?
	}

 

 

 

One comment

发表评论

电子邮件地址不会被公开。 必填项已用*标注