步骤一:在虚拟机上安装好hadoop并启动,在本地Windows配置好本地hadoop插件。(篇幅过长略可自行查询有关操作)。
步骤二:启动IDEA新建Maven工程。配置pom.xml文件(导入相关依赖)具体如下:
4.0.0 com.qiang.hdfs HdfsClient1.0-SNAPSHOT junit junit4.12 org.apache.logging.log4j log4j-slf4j-impl2.12.0 org.apache.hadoop hadoop-client3.1.3
步骤三:编写java代码如下:
package com.qiang.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
public class HdfsClientTest {
private FileSystem fs;
@Before
public void init() throws IOException, InterruptedException {
//HDFS访问路径
URI uri = URI.create("hdfs://hadoop1:9000");
//conf配置对象
Configuration conf = new Configuration();
//conf.set("dfs.replication","1");
//操作用户
String user="root";
//获取HDFS的客户端连接对象(文件系统对象)
fs= FileSystem.get(uri, conf, user);
}
@After
public void close() throws IOException {
fs.close();
}
//上传文件
@Test
public void testCopyFromLocal() throws IOException {
fs.copyFromLocalFile(false,true,
new Path("D:\a.txt"),
new Path("/wen"));
}
//下载文件
@Test
public void testCopyToLocal() throws IOException {
fs.copyToLocalFile(false,
new Path("/wen/a.txt"),
new Path("C:\Users\23996\Desktop\新建文件夹 (2)"),
true);
}
//删除文件和目录
@Test
public void testDelete() throws IOException {
fs.delete(new Path("/wen/a.txt"),
true);
}
//移动文件或更名
@Test
public void testRename() throws IOException {
fs.rename(new Path("/user/wen/wordcount.txt"),
new Path("/user/"));
}
//查看文件
@Test
public void testListFiles() throws IOException {
RemoteIterator locatedFile =
fs.listFiles(new Path("/"), true);
while (locatedFile.hasNext()){
LocatedFileStatus fileStatus = locatedFile.next();
System.out.println("文件名:" + fileStatus.getPath().getName());
System.out.println("块大小:" + fileStatus.getBlockSize());
System.out.println("副本数:" + fileStatus.getReplication());
System.out.println("权限信息:" + fileStatus.getPermission());
}
}
//判断是文件还是文件夹
@Test
public void testListStatus() throws IOException {
FileStatus[] fileStatuses = fs.listStatus(new Path("/user"));
for (FileStatus status : fileStatuses){
if (status.isDirectory()){
System.out.println("文件夹:" + status);
}
else {
System.out.println("文件:" + status);
}
}
}
}
以上仅供初学者参考:不喜勿喷!!!



