类org.apache.hadoop.io.BoundedByteArrayOutputStream源码实例Demo

下面列出了怎么用org.apache.hadoop.io.BoundedByteArrayOutputStream的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: hadoop   文件: InMemoryMapOutput.java
public InMemoryMapOutput(Configuration conf, TaskAttemptID mapId,
                         MergeManagerImpl<K, V> merger,
                         int size, CompressionCodec codec,
                         boolean primaryMapOutput) {
  super(mapId, (long)size, primaryMapOutput);
  this.conf = conf;
  this.merger = merger;
  this.codec = codec;
  byteStream = new BoundedByteArrayOutputStream(size);
  memory = byteStream.getBuffer();
  if (codec != null) {
    decompressor = CodecPool.getDecompressor(codec);
  } else {
    decompressor = null;
  }
}
 
源代码2 项目: big-c   文件: InMemoryMapOutput.java
public InMemoryMapOutput(Configuration conf, TaskAttemptID mapId,
                         MergeManagerImpl<K, V> merger,
                         int size, CompressionCodec codec,
                         boolean primaryMapOutput) {
  super(mapId, (long)size, primaryMapOutput);
  this.conf = conf;
  this.merger = merger;
  this.codec = codec;
  byteStream = new BoundedByteArrayOutputStream(size);
  memory = byteStream.getBuffer();
  if (codec != null) {
    decompressor = CodecPool.getDecompressor(codec);
  } else {
    decompressor = null;
  }
}
 
源代码3 项目: incubator-tez   文件: MapOutput.java
MapOutput(InputAttemptIdentifier attemptIdentifier, MergeManager merger, int size, 
          boolean primaryMapOutput) {
  this.id = ID.incrementAndGet();
  this.attemptIdentifier = attemptIdentifier;
  this.merger = merger;

  type = Type.MEMORY;
  byteStream = new BoundedByteArrayOutputStream(size);
  memory = byteStream.getBuffer();

  this.size = size;
  
  localFS = null;
  disk = null;
  outputPath = null;
  tmpOutputPath = null;
  
  this.primaryMapOutput = primaryMapOutput;
}
 
源代码4 项目: tez   文件: IFile.java
/**
 * Note that we do not allow compression in in-mem stream.
 * When spilled over to file, compression gets enabled.
 *
 * @param conf
 * @param fs
 * @param taskOutput
 * @param keyClass
 * @param valueClass
 * @param codec
 * @param writesCounter
 * @param serializedBytesCounter
 * @param cacheSize
 * @throws IOException
 */
public FileBackedInMemIFileWriter(Configuration conf, FileSystem fs,
    TezTaskOutput taskOutput, Class keyClass, Class valueClass,
    CompressionCodec codec,
    TezCounter writesCounter,
    TezCounter serializedBytesCounter,
    int cacheSize) throws IOException {
  super(conf, new FSDataOutputStream(createBoundedBuffer(cacheSize), null),
      keyClass, valueClass, null, writesCounter, serializedBytesCounter);
  this.fs = fs;
  this.cacheStream = (BoundedByteArrayOutputStream) this.rawOut.getWrappedStream();
  this.taskOutput = taskOutput;
  this.bufferFull = (cacheStream == null);
  this.totalSize = getBaseCacheSize();
  this.fileCodec = codec;
}
 
源代码5 项目: tez   文件: TestTezMerger.java
private List<TezMerger.Segment> createInMemorySegments(int segmentCount, int keysPerSegment)
    throws IOException {
  List<TezMerger.Segment> segmentList = Lists.newLinkedList();
  Random rnd = new Random();
  DataInputBuffer key = new DataInputBuffer();
  DataInputBuffer value = new DataInputBuffer();
  for (int i = 0; i < segmentCount; i++) {
    BoundedByteArrayOutputStream stream = new BoundedByteArrayOutputStream(10000);
    InMemoryWriter writer = new InMemoryWriter(stream);

    for (int j = 0; j < keysPerSegment; j++) {
      populateData(new IntWritable(rnd.nextInt()), new LongWritable(rnd.nextLong()), key, value);
      writer.append(key, value);
    }
    writer.close();
    InMemoryReader reader = new InMemoryReader(merger, null, stream.getBuffer(), 0, stream.getLimit());

    segmentList.add(new TezMerger.Segment(reader, null));
  }
  return segmentList;
}
 
源代码6 项目: hadoop   文件: TestMergeManager.java
private void fillOutput(InMemoryMapOutput<Text, Text> output) throws IOException {
  BoundedByteArrayOutputStream stream = output.getArrayStream();
  int count = stream.getLimit();
  for (int i=0; i < count; ++i) {
    stream.write(i);
  }
}
 
源代码7 项目: big-c   文件: TestMergeManager.java
private void fillOutput(InMemoryMapOutput<Text, Text> output) throws IOException {
  BoundedByteArrayOutputStream stream = output.getArrayStream();
  int count = stream.getLimit();
  for (int i=0; i < count; ++i) {
    stream.write(i);
  }
}
 
源代码8 项目: incubator-tez   文件: TestIFile.java
@Test
//Test InMemoryWriter
public void testInMemoryWriter() throws IOException {
  InMemoryWriter writer = null;
  BoundedByteArrayOutputStream bout = new BoundedByteArrayOutputStream(1024 * 1024);

  List<KVPair> data = KVDataGen.generateTestData(true, 0);

  //No RLE, No RepeatKeys, no compression
  writer = new InMemoryWriter(bout);
  writeTestFileUsingDataBuffer(writer, false, false, data, null);
  readUsingInMemoryReader(bout.getBuffer(), data);

  //No RLE, RepeatKeys, no compression
  bout.reset();
  writer = new InMemoryWriter(bout);
  writeTestFileUsingDataBuffer(writer, false, true, data, null);
  readUsingInMemoryReader(bout.getBuffer(), data);

  //RLE, No RepeatKeys, no compression
  bout.reset();
  writer = new InMemoryWriter(bout);
  writeTestFileUsingDataBuffer(writer, true, false, data, null);
  readUsingInMemoryReader(bout.getBuffer(), data);

  //RLE, RepeatKeys, no compression
  bout.reset();
  writer = new InMemoryWriter(bout);
  writeTestFileUsingDataBuffer(writer, true, true, data, null);
  readUsingInMemoryReader(bout.getBuffer(), data);
}
 
源代码9 项目: tez   文件: IFile.java
/**
 * Flip over from memory to file based writer.
 *
 * 1. Content format: HEADER + real data + CHECKSUM. Checksum is for real
 * data.
 * 2. Before flipping, close checksum stream, so that checksum is written
 * out.
 * 3. Create relevant file based writer.
 * 4. Write header and then real data.
 *
 * @throws IOException
 */
private void resetToFileBasedWriter() throws IOException {
  // Close out stream, so that data checksums are written.
  // Buf contents = HEADER + real data + CHECKSUM
  this.out.close();

  // Get the buffer which contains data in memory
  BoundedByteArrayOutputStream bout =
      (BoundedByteArrayOutputStream) this.rawOut.getWrappedStream();

  // Create new file based writer
  if (outputPath == null) {
    outputPath = taskOutput.getOutputFileForWrite();
  }
  LOG.info("Switching from mem stream to disk stream. File: " + outputPath);
  FSDataOutputStream newRawOut = fs.create(outputPath);
  this.rawOut = newRawOut;
  this.ownOutputStream = true;

  setupOutputStream(fileCodec);

  // Write header to file
  headerWritten = false;
  writeHeader(newRawOut);

  // write real data
  int sPos = HEADER.length;
  int len = (bout.size() - checksumSize - HEADER.length);
  this.out.write(bout.getBuffer(), sPos, len);

  bufferFull = true;
  bout.reset();
}
 
源代码10 项目: tez   文件: TestIFile.java
@Test(timeout = 5000)
//Test InMemoryWriter
public void testInMemoryWriter() throws IOException {
  InMemoryWriter writer = null;
  BoundedByteArrayOutputStream bout = new BoundedByteArrayOutputStream(1024 * 1024);

  List<KVPair> data = KVDataGen.generateTestData(true, 10);

  //No RLE, No RepeatKeys, no compression
  writer = new InMemoryWriter(bout);
  writeTestFileUsingDataBuffer(writer, false, data);
  readUsingInMemoryReader(bout.getBuffer(), data);

  //No RLE, RepeatKeys, no compression
  bout.reset();
  writer = new InMemoryWriter(bout);
  writeTestFileUsingDataBuffer(writer, true, data);
  readUsingInMemoryReader(bout.getBuffer(), data);

  //RLE, No RepeatKeys, no compression
  bout.reset();
  writer = new InMemoryWriter(bout, true);
  writeTestFileUsingDataBuffer(writer, false, data);
  readUsingInMemoryReader(bout.getBuffer(), data);

  //RLE, RepeatKeys, no compression
  bout.reset();
  writer = new InMemoryWriter(bout, true);
  writeTestFileUsingDataBuffer(writer, true, data);
  readUsingInMemoryReader(bout.getBuffer(), data);
}
 
源代码11 项目: hadoop   文件: InMemoryWriter.java
public InMemoryWriter(BoundedByteArrayOutputStream arrayStream) {
  super(null);
  this.out = 
    new DataOutputStream(new IFileOutputStream(arrayStream));
}
 
源代码12 项目: hadoop   文件: InMemoryMapOutput.java
public BoundedByteArrayOutputStream getArrayStream() {
  return byteStream;
}
 
源代码13 项目: hadoop   文件: TFile.java
@Override
public void close() throws IOException {
  if (closed == true) {
    return;
  }

  try {
    ++errorCount;
    byte[] key = currentKeyBufferOS.getBuffer();
    int len = currentKeyBufferOS.size();
    /**
     * verify length.
     */
    if (expectedLength >= 0 && expectedLength != len) {
      throw new IOException("Incorrect key length: expected="
          + expectedLength + " actual=" + len);
    }

    Utils.writeVInt(blkAppender, len);
    blkAppender.write(key, 0, len);
    if (tfileIndex.getFirstKey() == null) {
      tfileIndex.setFirstKey(key, 0, len);
    }

    if (tfileMeta.isSorted() && tfileMeta.getRecordCount()>0) {
      byte[] lastKey = lastKeyBufferOS.getBuffer();
      int lastLen = lastKeyBufferOS.size();
      if (tfileMeta.getComparator().compare(key, 0, len, lastKey, 0,
          lastLen) < 0) {
        throw new IOException("Keys are not added in sorted order");
      }
    }

    BoundedByteArrayOutputStream tmp = currentKeyBufferOS;
    currentKeyBufferOS = lastKeyBufferOS;
    lastKeyBufferOS = tmp;
    --errorCount;
  } finally {
    closed = true;
    state = State.END_KEY;
  }
}
 
源代码14 项目: big-c   文件: InMemoryWriter.java
public InMemoryWriter(BoundedByteArrayOutputStream arrayStream) {
  super(null);
  this.out = 
    new DataOutputStream(new IFileOutputStream(arrayStream));
}
 
源代码15 项目: big-c   文件: InMemoryMapOutput.java
public BoundedByteArrayOutputStream getArrayStream() {
  return byteStream;
}
 
源代码16 项目: big-c   文件: TFile.java
@Override
public void close() throws IOException {
  if (closed == true) {
    return;
  }

  try {
    ++errorCount;
    byte[] key = currentKeyBufferOS.getBuffer();
    int len = currentKeyBufferOS.size();
    /**
     * verify length.
     */
    if (expectedLength >= 0 && expectedLength != len) {
      throw new IOException("Incorrect key length: expected="
          + expectedLength + " actual=" + len);
    }

    Utils.writeVInt(blkAppender, len);
    blkAppender.write(key, 0, len);
    if (tfileIndex.getFirstKey() == null) {
      tfileIndex.setFirstKey(key, 0, len);
    }

    if (tfileMeta.isSorted() && tfileMeta.getRecordCount()>0) {
      byte[] lastKey = lastKeyBufferOS.getBuffer();
      int lastLen = lastKeyBufferOS.size();
      if (tfileMeta.getComparator().compare(key, 0, len, lastKey, 0,
          lastLen) < 0) {
        throw new IOException("Keys are not added in sorted order");
      }
    }

    BoundedByteArrayOutputStream tmp = currentKeyBufferOS;
    currentKeyBufferOS = lastKeyBufferOS;
    lastKeyBufferOS = tmp;
    --errorCount;
  } finally {
    closed = true;
    state = State.END_KEY;
  }
}
 
源代码17 项目: attic-apex-malhar   文件: DTFile.java
@Override
public void close() throws IOException {
  if (closed == true) {
    return;
  }

  try {
    ++errorCount;
    byte[] key = currentKeyBufferOS.getBuffer();
    int len = currentKeyBufferOS.size();
    /**
     * verify length.
     */
    if (expectedLength >= 0 && expectedLength != len) {
      throw new IOException("Incorrect key length: expected="
          + expectedLength + " actual=" + len);
    }

    Utils.writeVInt(blkAppender, len);
    blkAppender.write(key, 0, len);
    if (tfileIndex.getFirstKey() == null) {
      tfileIndex.setFirstKey(key, 0, len);
    }

    if (tfileMeta.isSorted() && tfileMeta.getRecordCount()>0) {
      byte[] lastKey = lastKeyBufferOS.getBuffer();
      int lastLen = lastKeyBufferOS.size();
      if (tfileMeta.getComparator().compare(key, 0, len, lastKey, 0,
          lastLen) < 0) {
        throw new IOException("Keys are not added in sorted order");
      }
    }

    BoundedByteArrayOutputStream tmp = currentKeyBufferOS;
    currentKeyBufferOS = lastKeyBufferOS;
    lastKeyBufferOS = tmp;
    --errorCount;
  } finally {
    closed = true;
    state = State.END_KEY;
  }
}
 
源代码18 项目: incubator-tez   文件: MemoryFetchedInput.java
public MemoryFetchedInput(long actualSize, long compressedSize,
    InputAttemptIdentifier inputAttemptIdentifier,
    FetchedInputCallback callbackHandler) {
  super(Type.MEMORY, actualSize, compressedSize, inputAttemptIdentifier, callbackHandler);
  this.byteStream = new BoundedByteArrayOutputStream((int) actualSize);
}
 
源代码19 项目: incubator-tez   文件: InMemoryWriter.java
public InMemoryWriter(BoundedByteArrayOutputStream arrayStream) {
  super(null, null);
  this.out =
    new DataOutputStream(new IFileOutputStream(arrayStream));
}
 
源代码20 项目: incubator-tez   文件: MapOutput.java
public BoundedByteArrayOutputStream getArrayStream() {
  return byteStream;
}
 
源代码21 项目: tez   文件: InMemoryWriter.java
public InMemoryWriter(BoundedByteArrayOutputStream arrayStream) {
  this(arrayStream, false);
}
 
源代码22 项目: tez   文件: InMemoryWriter.java
public InMemoryWriter(BoundedByteArrayOutputStream arrayStream, boolean rle) {
  super(null, null, rle);
  this.out = new NonSyncDataOutputStream(new IFileOutputStream(arrayStream));
}
 
源代码23 项目: tez   文件: TestValuesIterator.java
/**
 * create inmemory segments
 *
 * @return
 * @throws IOException
 */
@SuppressWarnings("unchecked")
public List<TezMerger.Segment> createInMemStreams() throws IOException {
  int numberOfStreams = Math.max(2, rnd.nextInt(10));
  LOG.info("No of streams : " + numberOfStreams);

  SerializationFactory serializationFactory = new SerializationFactory(conf);
  Serializer keySerializer = serializationFactory.getSerializer(keyClass);
  Serializer valueSerializer = serializationFactory.getSerializer(valClass);

  LocalDirAllocator localDirAllocator =
      new LocalDirAllocator(TezRuntimeFrameworkConfigs.LOCAL_DIRS);
  InputContext context = createTezInputContext();
  MergeManager mergeManager = new MergeManager(conf, fs, localDirAllocator,
      context, null, null, null, null, null, 1024 * 1024 * 10, null, false, -1);

  DataOutputBuffer keyBuf = new DataOutputBuffer();
  DataOutputBuffer valBuf = new DataOutputBuffer();
  DataInputBuffer keyIn = new DataInputBuffer();
  DataInputBuffer valIn = new DataInputBuffer();
  keySerializer.open(keyBuf);
  valueSerializer.open(valBuf);

  List<TezMerger.Segment> segments = new LinkedList<TezMerger.Segment>();
  for (int i = 0; i < numberOfStreams; i++) {
    BoundedByteArrayOutputStream bout = new BoundedByteArrayOutputStream(1024 * 1024);
    InMemoryWriter writer =
        new InMemoryWriter(bout);
    Map<Writable, Writable> data = createData();
    //write data
    for (Map.Entry<Writable, Writable> entry : data.entrySet()) {
      keySerializer.serialize(entry.getKey());
      valueSerializer.serialize(entry.getValue());
      keyIn.reset(keyBuf.getData(), 0, keyBuf.getLength());
      valIn.reset(valBuf.getData(), 0, valBuf.getLength());
      writer.append(keyIn, valIn);
      originalData.put(entry.getKey(), entry.getValue());
      keyBuf.reset();
      valBuf.reset();
      keyIn.reset();
      valIn.reset();
    }
    IFile.Reader reader = new InMemoryReader(mergeManager, null, bout.getBuffer(), 0,
        bout.getBuffer().length);
    segments.add(new TezMerger.Segment(reader, null));

    data.clear();
    writer.close();
  }
  return segments;
}
 
源代码24 项目: hadoop   文件: TFile.java
/**
 * Constructor
 * 
 * @param fsdos
 *          output stream for writing. Must be at position 0.
 * @param minBlockSize
 *          Minimum compressed block size in bytes. A compression block will
 *          not be closed until it reaches this size except for the last
 *          block.
 * @param compressName
 *          Name of the compression algorithm. Must be one of the strings
 *          returned by {@link TFile#getSupportedCompressionAlgorithms()}.
 * @param comparator
 *          Leave comparator as null or empty string if TFile is not sorted.
 *          Otherwise, provide the string name for the comparison algorithm
 *          for keys. Two kinds of comparators are supported.
 *          <ul>
 *          <li>Algorithmic comparator: binary comparators that is language
 *          independent. Currently, only "memcmp" is supported.
 *          <li>Language-specific comparator: binary comparators that can
 *          only be constructed in specific language. For Java, the syntax
 *          is "jclass:", followed by the class name of the RawComparator.
 *          Currently, we only support RawComparators that can be
 *          constructed through the default constructor (with no
 *          parameters). Parameterized RawComparators such as
 *          {@link WritableComparator} or
 *          {@link JavaSerializationComparator} may not be directly used.
 *          One should write a wrapper class that inherits from such classes
 *          and use its default constructor to perform proper
 *          initialization.
 *          </ul>
 * @param conf
 *          The configuration object.
 * @throws IOException
 */
public Writer(FSDataOutputStream fsdos, int minBlockSize,
    String compressName, String comparator, Configuration conf)
    throws IOException {
  sizeMinBlock = minBlockSize;
  tfileMeta = new TFileMeta(comparator);
  tfileIndex = new TFileIndex(tfileMeta.getComparator());

  writerBCF = new BCFile.Writer(fsdos, compressName, conf);
  currentKeyBufferOS = new BoundedByteArrayOutputStream(MAX_KEY_SIZE);
  lastKeyBufferOS = new BoundedByteArrayOutputStream(MAX_KEY_SIZE);
  this.conf = conf;
}
 
源代码25 项目: big-c   文件: TFile.java
/**
 * Constructor
 * 
 * @param fsdos
 *          output stream for writing. Must be at position 0.
 * @param minBlockSize
 *          Minimum compressed block size in bytes. A compression block will
 *          not be closed until it reaches this size except for the last
 *          block.
 * @param compressName
 *          Name of the compression algorithm. Must be one of the strings
 *          returned by {@link TFile#getSupportedCompressionAlgorithms()}.
 * @param comparator
 *          Leave comparator as null or empty string if TFile is not sorted.
 *          Otherwise, provide the string name for the comparison algorithm
 *          for keys. Two kinds of comparators are supported.
 *          <ul>
 *          <li>Algorithmic comparator: binary comparators that is language
 *          independent. Currently, only "memcmp" is supported.
 *          <li>Language-specific comparator: binary comparators that can
 *          only be constructed in specific language. For Java, the syntax
 *          is "jclass:", followed by the class name of the RawComparator.
 *          Currently, we only support RawComparators that can be
 *          constructed through the default constructor (with no
 *          parameters). Parameterized RawComparators such as
 *          {@link WritableComparator} or
 *          {@link JavaSerializationComparator} may not be directly used.
 *          One should write a wrapper class that inherits from such classes
 *          and use its default constructor to perform proper
 *          initialization.
 *          </ul>
 * @param conf
 *          The configuration object.
 * @throws IOException
 */
public Writer(FSDataOutputStream fsdos, int minBlockSize,
    String compressName, String comparator, Configuration conf)
    throws IOException {
  sizeMinBlock = minBlockSize;
  tfileMeta = new TFileMeta(comparator);
  tfileIndex = new TFileIndex(tfileMeta.getComparator());

  writerBCF = new BCFile.Writer(fsdos, compressName, conf);
  currentKeyBufferOS = new BoundedByteArrayOutputStream(MAX_KEY_SIZE);
  lastKeyBufferOS = new BoundedByteArrayOutputStream(MAX_KEY_SIZE);
  this.conf = conf;
}
 
源代码26 项目: attic-apex-malhar   文件: DTFile.java
/**
 * Constructor
 *
 * @param fsdos
 *          output stream for writing. Must be at position 0.
 * @param minBlockSize
 *          Minimum compressed block size in bytes. A compression block will
 *          not be closed until it reaches this size except for the last
 *          block.
 * @param compressName
 *          Name of the compression algorithm. Must be one of the strings
 *          returned by {@link DTFile#getSupportedCompressionAlgorithms()}.
 * @param comparator
 *          Leave comparator as null or empty string if TFile is not sorted.
 *          Otherwise, provide the string name for the comparison algorithm
 *          for keys. Two kinds of comparators are supported.
 *          <ul>
 *          <li>Algorithmic comparator: binary comparators that is language
 *          independent. Currently, only "memcmp" is supported.
 *          <li>Language-specific comparator: binary comparators that can
 *          only be constructed in specific language. For Java, the syntax
 *          is "jclass:", followed by the class name of the RawComparator.
 *          Currently, we only support RawComparators that can be
 *          constructed through the default constructor (with no
 *          parameters). Parameterized RawComparators such as
 *          {@link WritableComparator} or
 *          {@link JavaSerializationComparator} may not be directly used.
 *          One should write a wrapper class that inherits from such classes
 *          and use its default constructor to perform proper
 *          initialization.
 *          </ul>
 * @param conf
 *          The configuration object.
 * @throws IOException
 */
public Writer(FSDataOutputStream fsdos, int minBlockSize,
    String compressName, String comparator, Configuration conf)
    throws IOException {
  sizeMinBlock = minBlockSize;
  tfileMeta = new TFileMeta(comparator);
  tfileIndex = new TFileIndex(tfileMeta.getComparator());

  writerBCF = new DTBCFile.Writer(fsdos, compressName, conf);
  currentKeyBufferOS = new BoundedByteArrayOutputStream(MAX_KEY_SIZE);
  lastKeyBufferOS = new BoundedByteArrayOutputStream(MAX_KEY_SIZE);
  this.conf = conf;
}
 
源代码27 项目: tez   文件: IFile.java
/**
 * Create in mem stream. In it is too small, adjust it's size
 *
 * @param size
 * @return in memory stream
 */
public static BoundedByteArrayOutputStream createBoundedBuffer(int size) {
  int resize = Math.max(getBaseCacheSize(), size);
  return new BoundedByteArrayOutputStream(resize);
}
 
 类所在包
 同包方法