在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:\windows\system32
  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的参考资料:

  • https://blog.csdn.net/nightwizard2030/article/details/53142069
  • https://blog.csdn.net/darkdragonking/article/details/72636917

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

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

转载于:https://www.cnblogs.com/thousfeet/p/8658233.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值