HDFS API提供了一种二进制文件支持,直接将<key,value>对序列化到文件中,该文件格式是不能直接查看的,可以通过hadoop  dfs -text命令查看,后面跟上SequenceFile的HDFS路径

     通过写入SequenceFile和读入SequenceFile文件,打成jar包在Hadoop环境中运行。

     1.写入SequenceFile代码:

 1 package Hdfs;
 2 
 3 import java.io.IOException;
 4 import java.net.URI;
 5 
 6 import org.apache.hadoop.conf.Configuration;
 7 import org.apache.hadoop.fs.FileSystem;
 8 import org.apache.hadoop.fs.Path;
 9 import org.apache.hadoop.io.IOUtils;
10 import org.apache.hadoop.io.IntWritable;
11 import org.apache.hadoop.io.SequenceFile;
12 import org.apache.hadoop.io.Text;
13 
14 public class SequenceFileWriter {
15     private static final String[] text={
16         "床前明月光",
17         "疑似地上霜",
18         "举头望明月",
19         "低头思故乡"
20     };
21     public static void main(String[] args) {
22         String uri="hdfs://neusoft-master:9000/user/root/test/demo1";
23         Configuration conf=new Configuration();
24         SequenceFile.Writer writer=null;
25         
26         try {
27             FileSystem fs= FileSystem.get(URI.create(uri), conf);
28             Path path = new Path(uri);
29             IntWritable key = new IntWritable();
30             Text value = new Text();
31             writer = SequenceFile.createWriter(fs, conf, path, key.getClass(), value.getClass());
32             for (int i = 0; i < 100; i++) {
33                 key.set(100-i);
34                 value.set(text[i%text.length]);
35                 writer.append(key, value);
36             }
37         } catch (IOException e) {
38             e.printStackTrace();
39         }finally{
40             IOUtils.closeStream(writer);
41         }
42     }
43 }
SequenceFileWriter

相关文章:

  • 2021-05-07
  • 2021-08-08
  • 2021-07-26
  • 2021-07-05
  • 2022-12-23
  • 2022-12-23
  • 2021-10-05
猜你喜欢
  • 2021-12-22
  • 2022-02-13
  • 2021-07-18
  • 2022-12-23
  • 2021-11-22
  • 2021-12-14
  • 2022-12-23
相关资源
相似解决方案