工程的test包中java->com.imooc.bigdata->hadoop.hdfs.HDFSApp
注意包:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.net.URI;
/**
* 使用JAVA API操作HDFS文件系统
* 1)创建Configuration
* 2)获取FileSystem
* 3)HDFS API操作
*/
public class HDFSApp {
public static void main(String[] args) throws Exception {
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop000:8020"),configuration,"root");
Path path = new Path("/hdfsapi/test");
boolean result = fileSystem.mkdirs(path);
System.out.println(result);
}
}
提示错误:
解决:
C:\WINDOWS\system32\drivers\etc\hosts中
改为:
测试:
——————————————————————————————
使用Junit改写:
public class HDFSApp {
public static final String HDFS_PATH = "hdfs://hadoop000:8020";
FileSystem fileSystem = null;
Configuration configuration = null;
@Before
public void setUp() throws Exception {
System.out.println("---------setUp----------");
configuration = new Configuration();
fileSystem = FileSystem.get(new URI(HDFS_PATH),configuration,"root");
}
@After
public void tearDown(){
configuration = null;
fileSystem = null;
System.out.println("-------tearDown----------");
}
@Test
//创建HDFS文件夹
public void mkdir() throws Exception {
fileSystem.mkdirs(new Path("/hdfsapi/test2"));
}
}
添加方法:
@Test
public void text() throws IOException {
FSDataInputStream in = fileSystem.open(new Path("/README.txt"));
IOUtils.copyBytes(in,System.out,1024);
}
包选:
import org.apache.hadoop.io.IOUtils;
@Test
public void create() throws IOException {
FSDataOutputStream out = fileSystem.create(new Path("/hdfsapi/test/a.txt"));
out.writeUTF("hello pk");
out.flush();
out.close();
}
http://192.168.61.129:50070/explorer.html#/
现在的副本系数为3
因为设置了一个空的Configuration对象,所以会自动加载依赖包中hdfs-default.xml中的配置
设置:
configuration.set(“dfs.replication”,“1”);
此时:
查看idea编码:
修改为:
暂定:
应当是Windows系统和Centos系统方面的问题
@Test
public void rename() throws IOException {
Path oldPath = new Path("/hdfsapi/test/a.txt");
Path newPath = new Path("/hdfsapi/test/aaaaa.txt");
boolean result = fileSystem.rename(oldPath, newPath);
System.out.println(result);
}
@Test
public void copyFromLocalFile() throws IOException {
Path src = new Path("C:\\Users\\DELL\\Desktop\\新建文本文档 (2).txt");
Path dst = new Path("/hdfsapi/test/");
fileSystem.copyFromLocalFile(src,dst);
}
测试:
虽然出现了转义的问题,但是加上’'单引号就没问题了呢~
@Test
public void copyFromBigLocalFile() throws IOException {
InputStream in = new BufferedInputStream(new FileInputStream(new File("G:\\jdk-8u231-linux-x64.tar.gz")));
FSDataOutputStream out = fileSystem.create(new Path("/hdfsapi/test/jdk.tgz"), new Progressable() {
@Override
public void progress() {
System.out.print(".");
}
});
IOUtils.copyBytes(in,out,4096);
}
@Test
public void copyToLocalFile() throws IOException {
Path src = new Path("/README.txt");
Path dst = new Path("C:\\Users\\DELL\\Desktop\\");
fileSystem.copyToLocalFile(false,src,dst,true);
}
fileSystem.copyToLocalFile(false,src,dst,true);
@Test
public void listFiles() throws IOException {
FileStatus[] statuses = fileSystem.listStatus(new Path("/hdfsapi/test"));
for(FileStatus file : statuses){
String isDir = file.isDirectory() ? "文件夹" : "文件";
String permission = file.getPermission().toString();
short replication = file.getReplication();
long len = file.getLen();
String path = file.getPath().toString();
System.out.println(isDir + "\t" + permission + "\t"
+ replication + "\t" + len + "\t" + path
);
}
}
public void listFilesRecursive() throws IOException {
RemoteIterator<LocatedFileStatus> files = fileSystem.listFiles(new Path("/"), true);
while(files.hasNext()){
LocatedFileStatus file = files.next();
String isDir = file.isDirectory() ? "文件夹" : "文件";
String permission = file.getPermission().toString();
short replication = file.getReplication();
long len = file.getLen();
String path = file.getPath().toString();
System.out.println(isDir + "\t" + permission + "\t"
+ replication + "\t" + len + "\t" + path
);
}
}
@Test
public void getFileBlockLocations() throws IOException {
FileStatus fileStatus = fileSystem.getFileStatus(new Path("/hdfsapi/test/jdk.tgz"));
BlockLocation[] blocks = fileSystem.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
for(BlockLocation block : blocks){
for (String name : block.getNames()){
System.out.println(name + " : " + block.getOffset() + " : " + block.getLength() + " : " + block.getHosts());
}
}
}
@Test
public void delete() throws IOException {
boolean result = fileSystem.delete(new Path("/hdfsapi/test/jdk.tgz"));
System.out.println(result);
}