验证Hadoop环境变量是否正常。双击winutils.exe,如果报如下错误。说明缺少微软运行库(正版系统往往有这个问题)。再资料包里面有对应的微软运行库安装包双击安装即可。
xml<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.30</version>
</dependency>
</dependencies>
在项目的src/main/resources目录下,新建一个文件,命名为“log4j.properties”,在文件中填入
propertieslog4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n log4j.appender.logfile=org.apache.log4j.FileAppender log4j.appender.logfile.File=target/spring.log log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
javapackage com.suhm.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;
import java.net.URISyntaxException;
import java.util.Arrays;
public class HDFSClient {
private FileSystem fs;
//获取连接
@Before
public void init() throws IOException, InterruptedException, URISyntaxException {
//创建连接地址,访问nameNode内部访问端口
URI url = new URI("hdfs://192.168.59.102/:8020");
//创建核心配置对象
Configuration configuration = new Configuration();
//设置副本数量
configuration.set("dfs.replication", "3");
//操作用户
String user = "hadoop";
//获取客户端操作对象
fs = FileSystem.get(url, configuration, user);
}
//关闭资源
@After
public void close() throws IOException {
//关闭资源
fs.close();
}
//创建文件夹
@Test
public void testMkdir() throws IOException {
//创建文件夹
boolean mkdirs = fs.mkdirs(new Path("/xiyou"));
if (mkdirs) {
System.out.println("创建成功");
} else {
System.out.println("创建失败");
}
}
//windows文件拷贝到hdfs中
@Test
public void testPutFile() throws IOException {
//拷贝本地文件到hdfs中
fs.copyFromLocalFile(false, false,
new Path("E:\\gaoyangchijiji.txt"), new Path("/xiyou"));
}
//hdfs中文件重命名和移动
@Test
public void testMv() throws IOException {
//重名文件
fs.rename(new Path("/samguo"), new Path("/test/sanguo"));
}
//删除文件夹
@Test
public void testDelFile() throws IOException {
//删除空文件夹
//fs.delete(new Path("/gaoyang"),false);
//删除非空文件夹
fs.delete(new Path("/test"), true);
}
//获取指定路径下的文件夹和目录
@Test
public void testPrintFileInfo() throws IOException {
//获取路径下面的所有文件和目录 得到一个迭代器
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"),true);
//进行迭代循环
while(listFiles.hasNext()){
//获得当前迭代文件的信息
LocatedFileStatus fileStatus = listFiles.next();
//打印文件路径
System.out.println("============================"+ fileStatus.getPath() +"===============");
System.out.println(fileStatus.getPermission()); //文件权限
System.out.println(fileStatus.getOwner()); //文件所属者
System.out.println(fileStatus.getGroup()); //文件所属组
System.out.println(fileStatus.getLen()); //文件大小
System.out.println(fileStatus.getModificationTime()); //文件上次修改的时间
System.out.println(fileStatus.getReplication()); //文件副本存储数
System.out.println(fileStatus.getBlockSize()); //块大小
System.out.println(fileStatus.getPath().getName()); //获取文件名
BlockLocation[] blockLocations = fileStatus.getBlockLocations(); //获取块存储的所有信息
System.out.println(Arrays.toString(blockLocations));
}
}
//判断是否是文件或者文件夹
@Test
public void testIsFileOrIsDir() throws IOException {
FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
for (FileStatus fileStatus : fileStatuses) {
if (fileStatus.isFile()){
System.out.println("文件:"+fileStatus.getPath().getName());
}else {
System.out.println("文件夹:"+fileStatus.getPath().getName() );
}
}
}
}
本文作者:苏皓明
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!