1、添加依赖
<!-- hadoop -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.5</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.6.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>compile</scope>
</dependency>
2、添加resource
添加core-site.yaml
添加hdfs-site.yaml
3、测试
public class HdfsTest {
private Configuration configuration = null;
private FileSystem fs = null;
@Before
public void connect() throws Exception {
configuration = new Configuration(true);
// fs = FileSystem.get(configuration);
fs = FileSystem.get(URI.create("hdfs://node01:9000"), configuration, "root");
}
@Test
public void mkdir() throws IOException {
Path path = new Path("/msb01");
if (fs.exists(path)) {
fs.delete(path, true);
}
fs.mkdirs(path);
}
@Test
public void upload() throws IOException {
BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(new File("./data/hello.txt")));
Path outfile = new Path("/msb01/out.txt");
FSDataOutputStream outputStream = fs.create(outfile);
IOUtils.copyBytes(inputStream, outputStream, configuration, true);
}
@Test
public void blocks() throws IOException {
Path file = new Path("/user/root/data.txt");
FileStatus fileStatus = fs.getFileStatus(file);
BlockLocation[] fileBlockLocations = fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
for (BlockLocation b : fileBlockLocations) {
System.out.println(b);
}
FSDataInputStream in = fs.open(file);
in.seek(1048576);
System.out.println((char)in.readByte());
System.out.println((char)in.readByte());
System.out.println((char)in.readByte());
System.out.println((char)in.readByte());
System.out.println((char)in.readByte());
System.out.println((char)in.readByte());
System.out.println((char)in.readByte());
System.out.println((char)in.readByte());
System.out.println((char)in.readByte());
System.out.println((char)in.readByte());
System.out.println((char)in.readByte());
System.out.println((char)in.readByte());
}
@After
public void close() throws IOException {
fs.close();
}
}