使用hadoop文件系统org.apache.hadoop.fs.FileSystem写二进制文件

时间:2016-06-23 16:24:14

标签: hadoop binary hdfs nio

我曾经使用MappedByteBuffer和FileChannel生成二进制文件:

  final Path fullPath = ..
  final File parent = fullPath.toFile();
  FileChannel out=null;
  out = new RandomAccessFile(new File(parent, "myFileName"), "rw").getChannel();
  MappedByteBuffer buffer=null;
  buffer = out.map(FileChannel.MapMode.READ_WRITE, 0, size);
  buffer.order(ByteOrder.LITTLE_ENDIAN);
  buffer.putInt(12);
  buffer.putFloat(25);
  buffer.putFloat(32);

我尝试使用hadoop api生成相同的二进制文件,如下所示:

  FileSystem hadoopFileSys= hadoopDataSource.getFileSystem();

   FSDataOutputStream outputStream=null;
   outputStream = hadoopFileSys.create(hadoopPath);
   outputStream.writeInt(12);
   outputStream.writeFloat(25);
   outputStream.writeFloat(32);

我的问题是使用hadoop api生成的文件与使用nio api生成的文件不相同。

我需要两个生成的文件是相同的,因为它们将由相同的工具解析。

1 个答案:

答案 0 :(得分:0)

Hadoop FS使用大端。所以你真的需要使用:

    buffer.order(ByteOrder.BIG_ENDIAN);

NIO:

00 00 00 0C 41 C8 00 00 42 00 00 00

的hadoop:

00 00 00 0C 41 C8 00 00 42 00 00 00
相关问题