- 第2关 HDFS-JAVA接口之读取文件
- 第3关 HDFS-JAVA接口之上传文件
- 第4关 HDFS-JAVA接口之删除文件
package step2; import java.io.IOException; import java.io.InputStream; import java.net.URI; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IOUtils; public class FileSystemCat { public static void main(String[] args) throws IOException { //请在Begin-End之间添加你的代码,完成任务要求。 URI uri = URI.create("hdfs://localhost:9000/user/hadoop/task.txt"); Configuration config = new Configuration(); FileSystem fs = FileSystem.get(uri, config); InputStream in = null; try { in = fs.open(new Path(uri)); IOUtils.copyBytes(in, System.out, 2048, false); } catch (Exception e) { IOUtils.closeStream(in); } } }第3关 HDFS-JAVA接口之上传文件
package step3; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.io.File; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IOUtils; import org.apache.hadoop.util.Progressable; public class FileSystemUpload { public static void main(String[] args) throws IOException { //请在 Begin-End 之间添加代码,完成任务要求。 File localPath = new File("/develop/input/hello.txt"); String hdfsPath = "hdfs://localhost:9000/user/tmp/hello.txt"; InputStream in = new BufferedInputStream(new FileInputStream(localPath)); Configuration config = new Configuration(); FileSystem fs = FileSystem.get(URI.create(hdfsPath), config); long fileSize = localPath.length() > 65536 ? localPath.length() / 65536 : 1; FSDataOutputStream out = fs.create(new Path(hdfsPath), new Progressable() { long fileCount = 0; public void progress() { System.out.println("总进度" + (fileCount / fileSize) * 100 +"%"); fileCount++; } }); IOUtils.copyBytes(in, out, 2048, true); } }第4关 HDFS-JAVA接口之删除文件
String uri ="hdfs://localhost:9000/"; String path1="hdfs://localhost:9000/tmp"; String path2="hdfs://localhost:9000/tmp/test"; Configuration config = new Configuration(); FileSystem fs = FileSystem.get(URI.create(uri),config); fs.delete(new Path(path2),true); Path[] paths = {new Path(uri),new Path(path1)}; FileStatus[] status = fs.listStatus(paths); Path[] listPaths = FileUtil.stat2Paths(status); for (Path path : listPaths){ System.out.println(path); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)