Java操作HDFS
导入所需配置文件
代码
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Test;
import java.io.IOException;
public class Demo
{
FileSystem fs = null;
{
Configuration conf = new Configuration();
try
{
fs = FileSystem.get(conf);
}
catch (IOException e)
{
e.printStackTrace();
}
}
@Test
public void mkdir() throws IOException
{
Path path = new Path("/test");
fs.mkdirs(path);
fs.close();
System.out.println("创建成功!");
}
@Test
public void put() throws IOException
{
fs.copyFromLocalFile(new Path("D:\\IntelliJ_IDEA_2019.2.3\\Projects\\Hadoop\\HadoopDemo1\\src\\main\\java\\com\\test1\\getEmp.txt"),
new Path("/test"));
fs.close();
System.out.println("上传完成!");
}
@Test
public void get() throws IOException
{
fs.copyToLocalFile(false, new Path("/test/getEmp.txt"),
new Path("D:\\IntelliJ_IDEA_2019.2.3\\Projects\\Hadoop\\OperationHDFS\\src\\main\\java\\com"), true);
fs.close();
System.out.println("下载成功!");
}
@Test
public void delete() throws IOException
{
fs.delete(new Path("/test"), true);
fs.close();
System.out.println("删除成功!");
}
@Test
public void rename() throws IOException
{
fs.rename(new Path("/test"), new Path("/testdemo"));
fs.close();
System.out.println("更名成功!");
}
@Test
public void listFile() throws IOException
{
RemoteIterator<LocatedFileStatus> listFile = fs.listFiles(new Path("/test/getEmp.txt"), true);
while (listFile.hasNext())
{
LocatedFileStatus file = listFile.next();
BlockLocation[] locations = file.getBlockLocations();
for (BlockLocation location : locations)
{
System.out.println("块信息 : " + location);
String[] hosts = location.getHosts();
for (String host : hosts)
{
System.out.println("端口 : " + host);
}
String[] names = location.getNames();
for (String name : names)
{
System.out.println("地址 : " + name);
}
System.out.println("文件大小 : " + location.getLength());
}
}
}
}