Hadoop学习之hdfs的操作
1.将HDFS中的文件复制到本地
package com.shujia.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
public class Demo02Download {
FileSystem fileSystem;
@Before
public void getFileSystem() throws IOException {
Configuration entries = new Configuration();
entries.set("fs.defaultFS", "hdfs://master:9000");
fileSystem = FileSystem.get(entries);
}
@Test
public void getData() throws IOException {
String hdfsPath = "/NOTICE.txt";
String localPath = "data/";
fileSystem.copyToLocalFile(new Path(hdfsPath),new Path(localPath));
}
@After
public void close() throws IOException {
fileSystem.close();
}
}
2.上传数据到HDFS中
package com.shujia.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class Demo04PutData {
public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {
putData();
putData2();
}
public static void putData() throws IOException {
Configuration entries = new Configuration();
entries.set("fs.defaultFS","hdfs://master:9000");
FileSystem fileSystem = FileSystem.get(entries);
fileSystem.copyFromLocalFile(new Path("hadoop/data/students.txt"),new Path("/data/"));
fileSystem.close();
}
public static void putData2() throws IOException, URISyntaxException, InterruptedException {
Configuration entries = new Configuration();
entries.set("fs.defaultFS","hdfs://master:9000");
URI uri = new URI("hdfs://master:9000");
FileSystem fileSystem = FileSystem.get(uri,entries,"root");
fileSystem.copyFromLocalFile(new Path("hadoop/data/students.txt"),new Path("/data/"));
fileSystem.close();
}
}
3.在HDFS上创建文件目录
package com.shujia.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class Demo05MakeDir {
public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {
mkdir();
}
public static void mkdir() throws IOException, URISyntaxException, InterruptedException {
Configuration entries = new Configuration();
entries.set("fs.defaultFS","hdfs://master:9000");
URI uri = new URI("hdfs://master:9000");
FileSystem fileSystem = FileSystem.get(uri,entries,"root");
fileSystem.mkdirs(new Path("/api"));
fileSystem.close();
}
}
4.删除HDFS上的文件目录
package com.shujia.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class Demo06Delete {
public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {
delete();
}
public static void delete() throws IOException, URISyntaxException, InterruptedException {
Configuration entries = new Configuration();
entries.set("fs.defaultFS","hdfs://master:9000");
URI uri = new URI("hdfs://master:9000");
FileSystem fileSystem = FileSystem.get(uri,entries,"root");
fileSystem.delete(new Path("/api"),true);
fileSystem.close();
}
}
5.查看HDFS文件系统中文件和目录的元数据
package com.shujia.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class Demo07Liststatus {
public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {
getBlockLocation();
}
public static void getBlockLocation() throws IOException, URISyntaxException, InterruptedException {
Configuration entries = new Configuration();
entries.set("fs.defaultFS","hdfs://master:9000");
URI uri = new URI("hdfs://master:9000");
FileSystem fileSystem = FileSystem.get(uri,entries,"root");
FileStatus fileStatus = fileSystem.getFileStatus(new Path("/hadoop-3.1.3.tar.gz"));
System.out.println("路径:"+fileStatus.getPath());
System.out.println("长度:"+fileStatus.getLen());
System.out.println("副本数:"+fileStatus.getReplication());
BlockLocation[] fileBlockLocations = fileSystem.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
for (BlockLocation fileBlockLocation : fileBlockLocations) {
System.out.println("整个长度:"+fileBlockLocation.getLength());
System.out.println("偏移量,从文件的什么位置开始:"+fileBlockLocation.getOffset());
System.out.println("整个主机:"+fileBlockLocation.getHosts());
System.out.println("整个名称:"+fileBlockLocation.getNames());
}
fileSystem.close();
}
public static void getFileStatus() throws IOException, URISyntaxException, InterruptedException {
Configuration entries = new Configuration();
entries.set("fs.defaultFS","hdfs://master:9000");
URI uri = new URI("hdfs://master:9000");
FileSystem fileSystem = FileSystem.get(uri,entries,"root");
FileStatus fileStatus = fileSystem.getFileStatus(new Path("/hadoop-3.1.3.tar.gz"));
System.out.println("路径:"+fileStatus.getPath());
System.out.println("长度:"+fileStatus.getLen());
System.out.println("副本数:"+fileStatus.getReplication());
fileSystem.close();
}
public static void listStatus() throws IOException, URISyntaxException, InterruptedException {
Configuration entries = new Configuration();
entries.set("fs.defaultFS","hdfs://master:9000");
URI uri = new URI("hdfs://master:9000");
FileSystem fileSystem = FileSystem.get(uri,entries,"root");
FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/"));
for (FileStatus fileStatus : fileStatuses) {
if (fileStatus.isFile()) {
long blockSize = fileStatus.getBlockSize();
System.out.println(fileStatus.getPath());
System.out.println("Block块大小:"+blockSize);
System.out.println("长度:"+fileStatus.getLen());
}else {
System.out.println(fileStatus.getPath());
}
}
fileSystem.close();
}
}