[关闭]
@zhangyy 2018-03-23T16:21:52.000000Z 字数 4232 阅读 116

hadoop 的hdfsapi操作

大数据系列



一: hdfs api 操作

1.1 读取内容:

  1. package it.xuegod.hadoop.hdfs;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.net.MalformedURLException;
  5. import java.net.URL;
  6. import java.net.URLConnection;
  7. import java.nio.file.FileSystem;
  8. import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
  9. import org.junit.Test;
  10. import org.apache.hadoop.conf.Configuration;
  11. public class TestHdfs {
  12. @Test
  13. public void readFile() throws IOException {
  14. URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
  15. URL url = new URL("hdfs://172.17.100.11:8020/input/file1");
  16. URLConnection conn = url.openConnection();
  17. InputStream is = conn.getInputStream();
  18. byte[] buf = new byte[is.available()];
  19. is.read(buf);
  20. is.close();
  21. String str = new String(buf);
  22. System.out.println(str);
  23. }
  24. }

image_1c98o2fl57uadao14f7gvr4j9.png-72kB

1.2 读取hdfs的数据

  1. package it.xuegod.hadoop.hdfs;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import org.apache.hadoop.conf.Configuration;
  5. import org.apache.hadoop.fs.FSDataInputStream;
  6. import org.apache.hadoop.fs.FileSystem;
  7. import org.apache.hadoop.fs.Path;
  8. import org.junit.Test;
  9. /**
  10. *
  11. *
  12. * @author zhangyy
  13. *
  14. *read file
  15. *
  16. */
  17. public class readFileApi {
  18. @Test
  19. public void readFileByApi() throws IOException {
  20. Configuration conf = new Configuration();
  21. conf.set("fs.defaultFS", "hdfs://172.17.100.11:8020");
  22. FileSystem fs = FileSystem.get(conf);
  23. Path p = new Path("/input/file1");
  24. FSDataInputStream fis = fs.open(p);
  25. byte[] buf = new byte[1024];
  26. int len = -1;
  27. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  28. while((len = fis.read(buf)) != -1) {
  29. baos.write(buf, 0, len);
  30. }
  31. fis.close();
  32. baos.close();
  33. System.out.println(new String(baos.toByteArray()));
  34. }
  35. }

image_1c98o5d8p146r14b917r512ek18qvm.png-70.4kB

  1. package it.xuegod.hadoop.hdfs;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import org.apache.hadoop.conf.Configuration;
  5. import org.apache.hadoop.fs.FSDataInputStream;
  6. import org.apache.hadoop.fs.FileSystem;
  7. import org.apache.hadoop.fs.Path;
  8. import org.apache.hadoop.io.IOUtils;
  9. import org.junit.Test;
  10. /**
  11. *
  12. *
  13. * @author zhangyy
  14. *
  15. * readfile
  16. *
  17. */
  18. public class readFileApi2 {
  19. @Test
  20. public void readFileByApi() throws IOException {
  21. Configuration conf = new Configuration();
  22. conf.set("fs.defaultFS", "hdfs://172.17.100.11:8020");
  23. FileSystem fs = FileSystem.get(conf);
  24. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  25. Path p = new Path("/input/file1");
  26. FSDataInputStream fis = fs.open(p);
  27. IOUtils.copyBytes(fis, baos, 1024);
  28. System.out.println(new String(baos.toByteArray()));
  29. }
  30. }

image_1c98o6ms2ubu1dda1sn5t6e1hqm13.png-70.7kB

1.3: 在hdfs上面的建立文件

  1. package it.xuegod.hadoop.hdfs;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import org.apache.hadoop.conf.Configuration;
  5. import org.apache.hadoop.fs.FSDataInputStream;
  6. import org.apache.hadoop.fs.FileSystem;
  7. import org.apache.hadoop.fs.Path;
  8. import org.apache.hadoop.io.IOUtils;
  9. import org.junit.Test;
  10. /**
  11. *
  12. *
  13. * @author zhangyy
  14. *
  15. * readfile
  16. *
  17. */
  18. public class mkdirFileApi {
  19. @Test
  20. public void mkdirFileByApi() throws IOException {
  21. Configuration conf = new Configuration();
  22. conf.set("fs.defaultFS", "hdfs://172.17.100.11:8020");
  23. FileSystem fs = FileSystem.get(conf);
  24. fs.mkdirs(new Path("/myhadoop"));
  25. }
  26. }

image_1c98o9qprs1l1vqshmk1bap1pdp1g.png-75.6kB
image_1c98oaek38hb11bu7vo1ca9skn1t.png-137.4kB

1.4: put写入文件

  1. package it.xuegod.hadoop.hdfs;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import org.apache.hadoop.conf.Configuration;
  5. import org.apache.hadoop.fs.FSDataInputStream;
  6. import org.apache.hadoop.fs.FSDataOutputStream;
  7. import org.apache.hadoop.fs.FileSystem;
  8. import org.apache.hadoop.fs.Path;
  9. import org.apache.hadoop.io.IOUtils;
  10. import org.junit.Test;
  11. /**
  12. *
  13. *
  14. * @author zhangyy
  15. *
  16. * readfile
  17. *
  18. */
  19. public class putFileApi {
  20. @Test
  21. public void putFileByApi() throws IOException {
  22. Configuration conf = new Configuration();
  23. conf.set("fs.defaultFS", "hdfs://172.17.100.11:8020");
  24. FileSystem fs = FileSystem.get(conf);
  25. FSDataOutputStream out = fs.create(new Path("/myhadoop/a.txt"));
  26. out.write("helloword".getBytes());
  27. out.close();
  28. }
  29. }

image_1c98oed6ptc1du91rqvjefimd2a.png-64.1kB
image_1c98oev3f1lv0o2fve41f5mgpq2n.png-114.5kB

1.5 删除文件

  1. package it.xuegod.hadoop.hdfs;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import org.apache.hadoop.conf.Configuration;
  5. import org.apache.hadoop.fs.FSDataInputStream;
  6. import org.apache.hadoop.fs.FSDataOutputStream;
  7. import org.apache.hadoop.fs.FileSystem;
  8. import org.apache.hadoop.fs.Path;
  9. import org.apache.hadoop.io.IOUtils;
  10. import org.junit.Test;
  11. /**
  12. *
  13. *
  14. * @author zhangyy
  15. *
  16. * rmfile
  17. *
  18. */
  19. public class rmFileApi {
  20. @Test
  21. public void rmFileByApi() throws IOException {
  22. Configuration conf = new Configuration();
  23. conf.set("fs.defaultFS", "hdfs://172.17.100.11:8020");
  24. FileSystem fs = FileSystem.get(conf);
  25. Path p = new Path("/myhadoop");
  26. fs.delete(p,true);
  27. }
  28. }

image_1c98oginqra96qihqd1e4p10e834.png-49.9kB
image_1c98oh3uf1lp6ldl137nctm8ns3h.png-102.2kB

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注