hdfs api

public static FileSystem getHDFS() {
<span style="white-space:pre">		</span>FileSystem fs =null;
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>Configuration conf = new Configuration();
<span style="white-space:pre">		</span>try {
<span style="white-space:pre">			</span>fs= FileSystem.get(conf);
<span style="white-space:pre">		</span>} catch (IOException e) {
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>return fs;
<span style="white-space:pre">	</span>}
</pre><pre name="code" class="java">
</pre><pre name="code" class="java">
</pre><pre name="code" class="java">
</pre><pre name="code" class="java">/**
	 * 上传本地文件
	 */
	@Test
	public void testUpload() {
		try {
			FileSystem hdfs = HDFSUtils.getHDFS();
			hdfs.copyFromLocalFile(localPath, descPath);
			FileStatus[] fileStatus = hdfs.listStatus(descPath);

			for (FileStatus status : fileStatus) {
				Path path = status.getPath();
				System.out.println(path.getName());
			}
			hdfs.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 创建新文件
	 * 
	 * @throws Exception
	 */
	@Test
	public void testCreate() throws Exception {
		byte[] buff = "hello world!".getBytes();

		FileSystem hdfs = HDFSUtils.getHDFS();
		Path dst = new Path(descPath + "/hello4.txt");
		FSDataOutputStream outputStream = null;
		try {
			outputStream = hdfs.create(dst);
			outputStream.write(buff, 0, buff.length);
		} catch (Exception e) {
			e.printStackTrace();

		} finally {
			if (outputStream != null) {
				outputStream.close();
			}
		}

		FileStatus files[] = hdfs.listStatus(dst);
		for (FileStatus file : files) {
			System.out.println(file.getPath());
		}
	}

	/**
	 *  遍历HDFS上的文件和目录
	 */

	@Test
	public void testList() {
		FileSystem hdfs = HDFSUtils.getHDFS();
		Path path = new Path("/");
		try {
			FileStatus[] fileStatus = hdfs.listStatus(path);
			for (FileStatus status : fileStatus) {
				Path p = status.getPath();
				String str = status.isDir() ? "目录" : "文件";
				System.out.println(str + ":" + p);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 文件重命名
	 */
	@Test
	public void testRename() {
		Path oldPath = new Path("/home/hell.txt");
		Path newPath = new Path("/home/new.log");
		FileSystem hdfs = HDFSUtils.getHDFS();
		try {
			boolean falg = hdfs.rename(oldPath, newPath);
			hdfs.close();
			System.out.println(falg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 读取文件的内容
	 */
	@Test
	public void readFile() {
		Path path = new Path("/home/big1.pdf");
		FileSystem hdfs = HDFSUtils.getHDFS();
		try {
			FSDataInputStream in = hdfs.open(path);
			IOUtils.copyBytes(in, System.out, 4096, false); // 复制到标准输出流

		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 删除文件
	 */
	@Test
	public void delete() {
		FileSystem hdfs = HDFSUtils.getHDFS();
		Path path = new Path("/home/new.log");
		// 删除目录 不管是否为空目录
		// boolean falg=hdfs.delete(new Path("/home/test"));
		// 删除目录 不为空目录
		// boolean b=hdfs.delete(new Path("/home/test"),false);
		// 删除目录 不管是否为空目录
		// boolean falg=hdfs.delete(new Path("/home/aa"),true);

		try {
			boolean falg = hdfs.deleteOnExit(path);
			System.out.println(falg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


url


public class HDFSUrl {
	static {
		URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
	}

	@Test
	public void readFile() {

		try {
			URL url = new URL("hdfs://192.168.102.130:9000/home/hello.txt");
			InputStream in = url.openStream();
			IOUtils.copyBytes(in, System.out, 1024);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值