二進制檔案格式, 內含同步標籤因此可分段進行資料壓縮
► Write
以 recooed 為單位的有無壓縮的儲存格式 | 以 Block 為單位的壓縮儲存格式 |
► Write
► Readpublic class SequenceFileWriteDemo { public static void main(String[] args) throws IOException { String uri = "/sequence/sequenceFile.txt"; Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(URI.create(uri), conf); Path path = new Path(uri); IntWritable key = new IntWritable(); Text value = new Text(); SequenceFile.Writer writer = null; try { writer = SequenceFile.createWriter(fs, conf, path, key.getClass(), value.getClass()); for (int i = 0; i < 100; i++) { key.set(i+1); value.set(String.format("item(%03d) Data", key.get())); System.out.printf("len:[%04d] data:%s\n", writer.getLength(), value); writer.append(key, value); } } finally { IOUtils.closeStream(writer); } } }
public class SequenceFileRead { public static void main(String[] args) throws IOException { String uri = "/sequence/sequenceFile.txt"; Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(URI.create(uri), conf); Path path = new Path(uri); SequenceFile.Reader reader = null; try { reader = new SequenceFile.Reader(fs, path, conf); Writable key = (Writable) ReflectionUtils.newInstance(reader.getKeyClass(), conf); Writable value = (Writable) ReflectionUtils.newInstance(reader.getValueClass(), conf); long position = reader.getPosition(); while (reader.next(key, value)) { String syncSeen = reader.syncSeen() ? "Y" : " "; System.out.printf("Pos:%04d SyncSeen[%s] data[%03d]=%s\n", position, syncSeen, ((IntWritable)key).get(), value); position = reader.getPosition(); // 下一筆位置 } } finally { IOUtils.closeStream(reader); } } }