• 在windows下的hdfs客户端编写


    在windows下的hdfs客户端编写

    新建一个工程,右键 properties -> java build path -> libraries 和之前一样的操作,这次 new 一个 user libiary 把整个 hadoop3.0.0 的jar包全部导入

    upload()运行成功,运行download()的时候出现报错

    HADOOP_HOME and hadoop.home.dir are unset.

    解决方案:

    1. 从linux上把解压的hadoop-3.0.0文件夹下载到本地
    2. 下载hadoop3.0.0的 winutils,替换掉bin目录
    3. 将hadoop.dll 拷贝至C:windowssystem32
    4. 配置环境变量HADOOP_HOME,并且在系统变量的path中附加 %HADOOP_HOME%/bin

    成功运行代码:

    package cn.thousfeet.hadoop.hdfs;
    
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.net.URI;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FileStatus;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.LocatedFileStatus;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.fs.RemoteIterator;
    import org.junit.Before;
    import org.junit.Test;
    
    public class HdfsUtils {
    
    	FileSystem fs = null;
    	
    	@Before
    	public void init() throws Exception {
    		
    		// 读取classpath下的xxx-site.xml配置文件,解析内容封装到conf对象中
    		Configuration conf = new Configuration();
    		
    		// 可以手动设置conf的配置信息(覆盖原配置文件中的值)
    		conf.set("fs.defaultFS", "hdfs://node01:9000/");
    		
    		// 获取文件系统的客户端操作实例对象。设置username为之前上传文件的linux用户信息,以防没有读写权限
    		fs = FileSystem.get(new URI("hdfs://node01:9000/"), conf, "thousfeet");
    	}
    	
    	/**
    	 * 上传文件
    	 * @throws Exception 
    	 * @throws IllegalArgumentException 
    	 */
    	@Test
    	public void upload() throws IllegalArgumentException, Exception {
    		
    		fs.copyFromLocalFile(new Path("D:/eclipse-workspace/test.txt"), new Path("hdfs://node01:9000/a/b/c/test.txt"));
    	}
    	
    	/**
    	 * 下载文件
    	 * @throws Exception 
    	 * @throws IllegalArgumentException 
    	 */
    	@Test
    	public void download() throws IllegalArgumentException, Exception {
    		
    		fs.copyToLocalFile(new Path("hdfs://node01:9000/aa/test.txt"), new Path("D:/eclipse-workspace/test2.txt"));
    	}
    	
    	/**
    	 * 查看文件信息
    	 * @throws Exception 
    	 * @throws IllegalArgumentException 
    	 * @throws FileNotFoundException 
    	 */
    	@Test
    	public void listFiles() throws FileNotFoundException, IllegalArgumentException, Exception {
    		
    		// listFile() 列出的是文件信息,并且提供递归遍历(参数为true时 能递归查看文件夹内的文件)
    		RemoteIterator<LocatedFileStatus> files =  fs.listFiles(new Path("/"), true);
    		while(files.hasNext()) 
    		{
    			LocatedFileStatus file = files.next();
    			Path filePath = file.getPath();
    			String fileName = filePath.getName();
    			System.out.println(fileName);
    		}
    		/**打印结果
    		 *  test.txt
    		 *	core-site.xml
    		 *	hadoop-core-1.2.1.jar
    		 *	jdk-8u161-linux-x64.tar.gz
    		 */
    		
    		System.out.println("-----------------------");
    			
    		// listStatus() 列出文件和文件夹的信息,但是不提供递归遍历(需要自行去做递归)
    		FileStatus[] listStatus = fs.listStatus(new Path("/"));
    		for(FileStatus status : listStatus) 
    		{
    			String name = status.getPath().getName();
    			System.out.println(name + (status.isDirectory() ? " - is a dir": " - is a file"));
    		}
    		/**
    		 * 打印结果
    		 *  a - is a dir
    		 *  core-site.xml - is a file
    		 *  input - is a dir
    		 *  jdk-8u161-linux-x64.tar.gz - is a file
    		 */
    	}
    	
    	/**
    	 * 创建文件夹
    	 * @throws Exception 
    	 * @throws IllegalArgumentException 
    	 */
    	@Test
    	public void mkdir() throws IllegalArgumentException, Exception {
    		
    		fs.mkdirs(new Path("/a/b/c"));
    	}
    	
    	/**
    	 * 删除文件或文件夹
    	 * @throws Exception 
    	 * @throws IllegalArgumentException 
    	 */
    	@Test
    	public void deleteFiles() throws IllegalArgumentException, Exception {
    		
    		fs.delete(new Path("hdfs://node01:9000/aa"), true);
    	}
    }
    
    

    所以说还是windows的eclipse用的舒服啊!(

    关于win下调试hadoop的参考资料:


    附:eclipse使用的一些小技巧快捷键

    Alt + / 提示补全信息(设置全字符补全:https://www.cnblogs.com/firstcsharp/p/4025689.html)
    Ctrl + 2 松手 L new一个对象,前面接收的对象信息快速补全

  • 相关阅读:
    [C#]画图全攻略(饼图与柱状图)(转)
    ZedGraph控件的使用
    android PopupWindow实现从底部弹出或滑出选择菜单或窗口
    那两年炼就的Android内功修养
    android添加edittext后布局就不好用
    第三方应用分享到微信朋友圈功能
    android的微信签名
    Android开源库
    Android开发之Intent跳转到系统应用中的拨号界面、联系人界面、短信界面
    android 显示特殊符号
  • 原文地址:https://www.cnblogs.com/thousfeet/p/8658233.html
Copyright © 2020-2023  润新知