第一步
- eclipse新建maven项目,pom.xml中添加hadoop依赖
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.3</version>
</dependency> - 新建测试代码,为了解hdfs java api大概(有什么功能,怎么用)
public class HdfsClient {
FileSystem fs = null;
@Before
public void init() throws Exception {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS", "hdfs://master1:9000");
fs = FileSystem.get(configuration);
}
/*
* 上传一个文件
*/
@Test
public void upload() throws Exception {
fs.copyFromLocalFile(new Path("e://scala/npp_6.9.2_Installer.exe"), new Path("/"));
fs.close();
}
/*
* 下载一个文件
*/
@Test
public void download() throws IllegalArgumentException, IOException {
fs.copyToLocalFile(new Path("/npp_6.9.2_Installer.exe"), new Path("e://scala"));
fs.close();
}
/*
* 删除文件
*/
@Test
public void delete() throws Exception{
fs.deleteOnExit(new Path("/npp_6.9.2_Installer.exe"));
fs.close();
}
@Test
public void somefunction() throws Exception {
FsStatus status = fs.getStatus();
System.out.println(status.getCapacity()+"-"+status.getUsed()/1024);
}
}
test 下载function时报错
(null) entry in command string: null chmod 0644
解决过程
- 百度搜
http://blog.youkuaiyun.com/hqwang4/article/details/54669165 去hadoop官网下载hadoop2.7.3 binary版本,解压–增加系统变量–按上面的文章黏贴libwinutils.lib和winutils.exe到%HADOOP_HOME%/bin下
至此,下载function可以使用。同时发现copyToLocalFile function会在下载时,如果目标目录中已经存在所copy的文件时,会先执行删除把该文件删除,然后在执行copy操作
总结
在windows下使用hadoop 的java api需要注意兼容性
1,具体解决兼容性的原理
2,可以尝试windows下编译hadoop