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


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

Alt + / 提示补全信息(设置全字符补全:https://www.cnblogs.com/firstcsharp/p/4025689.html)

Ctrl + 2 松手 L new一个对象,前面接收的对象信息快速补全

在windows下的hdfs客户端编写的更多相关文章

  1. windows下的mysql客户端mysqlworkbench链接虚拟机上CentOS的mysql服务器

    本人在虚拟机上CentOS的Linux环境下安装了mysql服务器,在本地Windows下安装了mysql的客户端mysqlworkbench ,所以就想让windows下的mysql客户端mysql ...

  2. Windows下安装Redis客户端

    Redis是有名的NoSql数据库,一般Linux都会默认支持.但在Windows环境中,可能需要手动安装设置才能有效使用.这里就简单介绍一下Windows下Redis服务的安装方法,希望能够帮到你. ...

  3. windows 下使用github客户端报错:Failed to publish this branch

    在windows系统下使用github客户端同步的时候报错“Failed to publish this branch”,查找原因,发现结果是安装vscode的时候没有检查到git,然后安装git后库 ...

  4. windows下安装oracle客户端和php扩展

    先来抱怨下 ,按这玩楞费了我大半天的时间,一路的坑! 我的电脑是win7 64位的 第一步  打开php.ini  把 extension=php_oci8_12c.dll extension=php ...

  5. Mqtt开发笔记:windows下C++ ActiveMQ客户端介绍、编译和使用

    前话   项目需求,需要使用到mqtt协议,之前编译QtMqtt库,不支持队列模式queue(点对点),只支持订阅/发布者模式.,所以使用C++ ActiveMQ实现.   MQTT协议 简介   M ...

  6. windows下注册表脚本编写

    Reg文件就是我今天所说的注册表脚本文件,双击可将其中的数据写入注册表.利用注册表脚本文件可以对注册表进行关于键值的任何操作,而且还不受注册表被禁用的限制.     我们平常对注册表的修改大体上可以分 ...

  7. windows下使用eclipse远程编写hadoop配置

    1.按照一般方法配置好hadoop伪分布式,注意core-site.xml和mapred-site.xml用IP,不要用localhost 2.格式化hdfs文件系统hadoop namenode - ...

  8. Windows 下的SSH客户端

    在日常Linux系统管理中,会使用SSH工具连接服务器,之所以SSH连接主要是为了安全,传统的telnet连接方式是以明文传输,很不安全,网络中如果又热窃听抓包,密码将要泄露.在众多SSH连接中,Pu ...

  9. 推荐Windows下SVN服务器端和客户端工具软件

    相信很多人使用过Windows下的SVN客户端软件TortoiseSVN或者也有过Linux下.MAC下的SVN命令行使用经验,另外MAC下还有以一款就做Vesions的SVN客户端软件,不过个人感觉 ...

随机推荐

  1. Object类上的方法

    1.getClass: public final native Class<?> getClass(); 返回当前对象运行时的类的对象. 2.hashCode: public native ...

  2. vs2015 活动解决方案或项目由选择的源代码管理插件以外的插件管理

    1.vs2015切换源代码管理插件,svn无法切换到git,点击是将关闭项目 解决方案: 找到项目中.sln 文件,使用编辑器打开,将Svn-Managed = true 设置为false

  3. 纪念Vamai

      知道Vamei这位博主去世的消息有些日子啦,在他的豆瓣主页也留下了只言片语,他写的协议森林让我印象深刻,在博客园也是我关注列表里的一位. 本来没打算写一篇文来说Vamei去世的事情,不过意外之是加 ...

  4. 将windows共享文件夹挂载在linux机器的/mnt/windows/ 目录下进行访问

    将windows共享文件夹挂载在linux机器的/mnt/windows/ 目录下进行访问.windows机器ip:192.168.1.101,用户名:XXXX密码:XXXXlinux机器ip:ip2 ...

  5. Effective C++ Placement new

    #include <iostream> #include <cstdlib> using namespace std; class Card { private: int m_ ...

  6. PHP报错Deprecated: Function ereg_replace() is deprecated in

    可能用了PHP5.3乃至更高的PHP版本,目前DEDE中有很多地方的正则函数都用的ereg_replace,而这个函数现在在PHP5.3中已经被废止了. 解决办法: 如果一定要用php5.3,请修改p ...

  7. BZOJ1935: [Shoi2007]Tree 园丁的烦恼(树状数组 二维数点)

    题意 题目链接 Sol 二维数点板子题 首先把询问拆成四个矩形 然后离散化+树状数组统计就可以了 // luogu-judger-enable-o2 #include<bits/stdc++.h ...

  8. .net开发环境搭建

    本地开发环境下载网址:https://msdn.itellyou.cn/,选择个人免费版本 下载工具 下载安装win7系统选择asp.net 和web开发,右侧可选全部选择,大约11GB左右

  9. 612.1.004 ALGS4 | Elementary Sorts - 基础排序算法

    sublime编辑器写代码,命令行编译 减少对ide的依赖//可以提示缺少什么依赖import 所有示例代码动手敲一遍 Graham's Scan是经典的计算几何算法 shffule 与 map-re ...

  10. linux 挂载命令mount、umount

    mount /bin/mount语法:mount [-t文件系统] 设备文件名 挂载点mount -t iso9660 /dev/sr0 /mnt/cdromiso9660是固定的,光盘:所以 -t ...