<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>
Configuration configuration = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102"), configuration, "pitaya"); fs.close()
@Test public void testCopyFromLocalFIle() throws IOException, URISyntaxException, InterruptedException { Configuration configuration = new Configuration(); //统一资源标识符(Uniform Resource Identifier,URI) FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "pitaya"); fs.copyFromLocalFile(new Path("/Users/eric/blogs/code/uploadFiles/test.txt"), new Path("/")); fs.close(); }
Permission denied: user=ptaya, access=WRITE, inode="/":pitaya:supergroup:drwxr-xr-x
@Test public void testDownload() throws IOException, URISyntaxException, InterruptedException { Configuration configuration = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102"), configuration, "pitaya"); fs.copyToLocalFile(true,new Path("/test.txt"),new Path("/Users/eric/blogs/code/downloadFiles")); fs.close(); }
fs.rename(new Path("/test.txt"),new Path("/success.txt"));
fs.delete(new Path("/success.txt"),true);
使用时发现FileSystem.get()返回的其实是DistributedFileSystem
public class DistributedFileSystem extends FileSystem implements KeyProviderTokenIssuer { } //FileSystem里定义 public abstract boolean delete(Path var1, boolean var2) throws IOException;
RemoteIterator<LocatedFileStatus> locatedFileStatusRemoteIterator = fs.listFiles(new Path("/"), true); while(locatedFileStatusRemoteIterator.hasNext()){ LocatedFileStatus fileStatus = locatedFileStatusRemoteIterator.next(); System.out.println("======="+fileStatus.getPath()+"======"); System.out.println(fileStatus.getOwner()); System.out.println(fileStatus.getModificationTime()); System.out.println(fileStatus.getBlockSize()); System.out.println(Arrays.toString(fileStatus.getBlockLocations())); }
fs.mkdirs(new Path("newcreate")); FSDataOutputStream fsDataOutputStream = fs.create(new Path("/newcreate"));