Java Code Examples for org.apache.hadoop.io.file.tfile.TFile.Reader#createScannerByKey()

The following examples show how to use org.apache.hadoop.io.file.tfile.TFile.Reader#createScannerByKey() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestTFileByteArrays.java    From big-c with Apache License 2.0 6 votes vote down vote up
private void readValueWithoutKey(int recordIndex)
    throws IOException {
  Reader reader = new Reader(fs.open(path), fs.getFileStatus(path).getLen(), conf);

  Scanner scanner =
      reader.createScannerByKey(composeSortedKey(KEY, recordIndex)
          .getBytes(), null);

  byte[] vbuf1 = new byte[BUF_SIZE];
  int vlen1 = scanner.entry().getValueLength();
  scanner.entry().getValue(vbuf1);
  Assert.assertEquals(new String(vbuf1, 0, vlen1), VALUE + recordIndex);

  if (scanner.advance() && !scanner.atEnd()) {
    byte[] vbuf2 = new byte[BUF_SIZE];
    int vlen2 = scanner.entry().getValueLength();
    scanner.entry().getValue(vbuf2);
    Assert.assertEquals(new String(vbuf2, 0, vlen2), VALUE
        + (recordIndex + 1));
  }

  scanner.close();
  reader.close();
}
 
Example 2
Source File: TestTFileUnsortedByteArrays.java    From RDFS with Apache License 2.0 6 votes vote down vote up
public void testFailureScannerWithKeys() throws IOException {
  Reader reader =
      new Reader(fs.open(path), fs.getFileStatus(path).getLen(), conf);
  Assert.assertFalse(reader.isSorted());
  Assert.assertEquals((int) reader.getEntryCount(), 4);

  try {
    Scanner scanner =
        reader.createScannerByKey("aaa".getBytes(), "zzz".getBytes());
    Assert
        .fail("Failed to catch creating scanner with keys on unsorted file.");
  }
  catch (RuntimeException e) {
  }
  finally {
    reader.close();
  }
}
 
Example 3
Source File: TestTFileByteArrays.java    From RDFS with Apache License 2.0 6 votes vote down vote up
private void readValueWithoutKey(int recordIndex)
    throws IOException {
  Reader reader = new Reader(fs.open(path), fs.getFileStatus(path).getLen(), conf);

  Scanner scanner =
      reader.createScannerByKey(composeSortedKey(KEY, recordIndex)
          .getBytes(), null);

  byte[] vbuf1 = new byte[BUF_SIZE];
  int vlen1 = scanner.entry().getValueLength();
  scanner.entry().getValue(vbuf1);
  Assert.assertEquals(new String(vbuf1, 0, vlen1), VALUE + recordIndex);

  if (scanner.advance() && !scanner.atEnd()) {
    byte[] vbuf2 = new byte[BUF_SIZE];
    int vlen2 = scanner.entry().getValueLength();
    scanner.entry().getValue(vbuf2);
    Assert.assertEquals(new String(vbuf2, 0, vlen2), VALUE
        + (recordIndex + 1));
  }

  scanner.close();
  reader.close();
}
 
Example 4
Source File: TestTFileByteArrays.java    From RDFS with Apache License 2.0 6 votes vote down vote up
private void readValueBeforeKey(int recordIndex)
    throws IOException {
  Reader reader =
      new Reader(fs.open(path), fs.getFileStatus(path).getLen(), conf);
  Scanner scanner =
      reader.createScannerByKey(composeSortedKey(KEY, recordIndex)
          .getBytes(), null);

  try {
    byte[] vbuf = new byte[BUF_SIZE];
    int vlen = scanner.entry().getValueLength();
    scanner.entry().getValue(vbuf);
    Assert.assertEquals(new String(vbuf, 0, vlen), VALUE + recordIndex);

    byte[] kbuf = new byte[BUF_SIZE];
    int klen = scanner.entry().getKeyLength();
    scanner.entry().getKey(kbuf);
    Assert.assertEquals(new String(kbuf, 0, klen), composeSortedKey(KEY,
        recordIndex));
  } finally {
    scanner.close();
    reader.close();
  }
}
 
Example 5
Source File: TestTFileUnsortedByteArrays.java    From big-c with Apache License 2.0 6 votes vote down vote up
public void testFailureScannerWithKeys() throws IOException {
  Reader reader =
      new Reader(fs.open(path), fs.getFileStatus(path).getLen(), conf);
  Assert.assertFalse(reader.isSorted());
  Assert.assertEquals((int) reader.getEntryCount(), 4);

  try {
    Scanner scanner =
        reader.createScannerByKey("aaa".getBytes(), "zzz".getBytes());
    Assert
        .fail("Failed to catch creating scanner with keys on unsorted file.");
  }
  catch (RuntimeException e) {
  }
  finally {
    reader.close();
  }
}
 
Example 6
Source File: TestTFileByteArrays.java    From big-c with Apache License 2.0 6 votes vote down vote up
private void readValueBeforeKey(int recordIndex)
    throws IOException {
  Reader reader =
      new Reader(fs.open(path), fs.getFileStatus(path).getLen(), conf);
  Scanner scanner =
      reader.createScannerByKey(composeSortedKey(KEY, recordIndex)
          .getBytes(), null);

  try {
    byte[] vbuf = new byte[BUF_SIZE];
    int vlen = scanner.entry().getValueLength();
    scanner.entry().getValue(vbuf);
    Assert.assertEquals(new String(vbuf, 0, vlen), VALUE + recordIndex);

    byte[] kbuf = new byte[BUF_SIZE];
    int klen = scanner.entry().getKeyLength();
    scanner.entry().getKey(kbuf);
    Assert.assertEquals(new String(kbuf, 0, klen), composeSortedKey(KEY,
        recordIndex));
  } finally {
    scanner.close();
    reader.close();
  }
}
 
Example 7
Source File: TestTFileUnsortedByteArrays.java    From hadoop with Apache License 2.0 6 votes vote down vote up
public void testFailureScannerWithKeys() throws IOException {
  Reader reader =
      new Reader(fs.open(path), fs.getFileStatus(path).getLen(), conf);
  Assert.assertFalse(reader.isSorted());
  Assert.assertEquals((int) reader.getEntryCount(), 4);

  try {
    Scanner scanner =
        reader.createScannerByKey("aaa".getBytes(), "zzz".getBytes());
    Assert
        .fail("Failed to catch creating scanner with keys on unsorted file.");
  }
  catch (RuntimeException e) {
  }
  finally {
    reader.close();
  }
}
 
Example 8
Source File: TestTFileByteArrays.java    From hadoop with Apache License 2.0 6 votes vote down vote up
private void readValueWithoutKey(int recordIndex)
    throws IOException {
  Reader reader = new Reader(fs.open(path), fs.getFileStatus(path).getLen(), conf);

  Scanner scanner =
      reader.createScannerByKey(composeSortedKey(KEY, recordIndex)
          .getBytes(), null);

  byte[] vbuf1 = new byte[BUF_SIZE];
  int vlen1 = scanner.entry().getValueLength();
  scanner.entry().getValue(vbuf1);
  Assert.assertEquals(new String(vbuf1, 0, vlen1), VALUE + recordIndex);

  if (scanner.advance() && !scanner.atEnd()) {
    byte[] vbuf2 = new byte[BUF_SIZE];
    int vlen2 = scanner.entry().getValueLength();
    scanner.entry().getValue(vbuf2);
    Assert.assertEquals(new String(vbuf2, 0, vlen2), VALUE
        + (recordIndex + 1));
  }

  scanner.close();
  reader.close();
}
 
Example 9
Source File: TestTFileByteArrays.java    From hadoop with Apache License 2.0 6 votes vote down vote up
private void readValueBeforeKey(int recordIndex)
    throws IOException {
  Reader reader =
      new Reader(fs.open(path), fs.getFileStatus(path).getLen(), conf);
  Scanner scanner =
      reader.createScannerByKey(composeSortedKey(KEY, recordIndex)
          .getBytes(), null);

  try {
    byte[] vbuf = new byte[BUF_SIZE];
    int vlen = scanner.entry().getValueLength();
    scanner.entry().getValue(vbuf);
    Assert.assertEquals(new String(vbuf, 0, vlen), VALUE + recordIndex);

    byte[] kbuf = new byte[BUF_SIZE];
    int klen = scanner.entry().getKeyLength();
    scanner.entry().getKey(kbuf);
    Assert.assertEquals(new String(kbuf, 0, klen), composeSortedKey(KEY,
        recordIndex));
  } finally {
    scanner.close();
    reader.close();
  }
}
 
Example 10
Source File: TestTFileByteArrays.java    From big-c with Apache License 2.0 5 votes vote down vote up
private void readKeyWithoutValue(int recordIndex)
    throws IOException {
  Reader reader = new Reader(fs.open(path), fs.getFileStatus(path).getLen(), conf);
  Scanner scanner =
      reader.createScannerByKey(composeSortedKey(KEY, recordIndex)
          .getBytes(), null);

  try {
    // read the indexed key
    byte[] kbuf1 = new byte[BUF_SIZE];
    int klen1 = scanner.entry().getKeyLength();
    scanner.entry().getKey(kbuf1);
    Assert.assertEquals(new String(kbuf1, 0, klen1), composeSortedKey(KEY,
        recordIndex));

    if (scanner.advance() && !scanner.atEnd()) {
      // read the next key following the indexed
      byte[] kbuf2 = new byte[BUF_SIZE];
      int klen2 = scanner.entry().getKeyLength();
      scanner.entry().getKey(kbuf2);
      Assert.assertEquals(new String(kbuf2, 0, klen2), composeSortedKey(KEY,
          recordIndex + 1));
    }
  } finally {
    scanner.close();
    reader.close();
  }
}
 
Example 11
Source File: TestTFileByteArrays.java    From big-c with Apache License 2.0 5 votes vote down vote up
private void readKeyManyTimes(int recordIndex) throws IOException {
  Reader reader = new Reader(fs.open(path), fs.getFileStatus(path).getLen(), conf);

  Scanner scanner =
      reader.createScannerByKey(composeSortedKey(KEY, recordIndex)
          .getBytes(), null);

  // read the indexed key
  byte[] kbuf1 = new byte[BUF_SIZE];
  int klen1 = scanner.entry().getKeyLength();
  scanner.entry().getKey(kbuf1);
  Assert.assertEquals(new String(kbuf1, 0, klen1), composeSortedKey(KEY,
      recordIndex));

  klen1 = scanner.entry().getKeyLength();
  scanner.entry().getKey(kbuf1);
  Assert.assertEquals(new String(kbuf1, 0, klen1), composeSortedKey(KEY,
      recordIndex));

  klen1 = scanner.entry().getKeyLength();
  scanner.entry().getKey(kbuf1);
  Assert.assertEquals(new String(kbuf1, 0, klen1), composeSortedKey(KEY,
      recordIndex));

  scanner.close();
  reader.close();
}
 
Example 12
Source File: TestTFileByteArrays.java    From hadoop with Apache License 2.0 5 votes vote down vote up
private void readKeyManyTimes(int recordIndex) throws IOException {
  Reader reader = new Reader(fs.open(path), fs.getFileStatus(path).getLen(), conf);

  Scanner scanner =
      reader.createScannerByKey(composeSortedKey(KEY, recordIndex)
          .getBytes(), null);

  // read the indexed key
  byte[] kbuf1 = new byte[BUF_SIZE];
  int klen1 = scanner.entry().getKeyLength();
  scanner.entry().getKey(kbuf1);
  Assert.assertEquals(new String(kbuf1, 0, klen1), composeSortedKey(KEY,
      recordIndex));

  klen1 = scanner.entry().getKeyLength();
  scanner.entry().getKey(kbuf1);
  Assert.assertEquals(new String(kbuf1, 0, klen1), composeSortedKey(KEY,
      recordIndex));

  klen1 = scanner.entry().getKeyLength();
  scanner.entry().getKey(kbuf1);
  Assert.assertEquals(new String(kbuf1, 0, klen1), composeSortedKey(KEY,
      recordIndex));

  scanner.close();
  reader.close();
}
 
Example 13
Source File: TestTFileByteArrays.java    From RDFS with Apache License 2.0 5 votes vote down vote up
private void readKeyWithoutValue(int recordIndex)
    throws IOException {
  Reader reader = new Reader(fs.open(path), fs.getFileStatus(path).getLen(), conf);
  Scanner scanner =
      reader.createScannerByKey(composeSortedKey(KEY, recordIndex)
          .getBytes(), null);

  try {
    // read the indexed key
    byte[] kbuf1 = new byte[BUF_SIZE];
    int klen1 = scanner.entry().getKeyLength();
    scanner.entry().getKey(kbuf1);
    Assert.assertEquals(new String(kbuf1, 0, klen1), composeSortedKey(KEY,
        recordIndex));

    if (scanner.advance() && !scanner.atEnd()) {
      // read the next key following the indexed
      byte[] kbuf2 = new byte[BUF_SIZE];
      int klen2 = scanner.entry().getKeyLength();
      scanner.entry().getKey(kbuf2);
      Assert.assertEquals(new String(kbuf2, 0, klen2), composeSortedKey(KEY,
          recordIndex + 1));
    }
  } finally {
    scanner.close();
    reader.close();
  }
}
 
Example 14
Source File: TestTFileByteArrays.java    From hadoop with Apache License 2.0 5 votes vote down vote up
private void readKeyWithoutValue(int recordIndex)
    throws IOException {
  Reader reader = new Reader(fs.open(path), fs.getFileStatus(path).getLen(), conf);
  Scanner scanner =
      reader.createScannerByKey(composeSortedKey(KEY, recordIndex)
          .getBytes(), null);

  try {
    // read the indexed key
    byte[] kbuf1 = new byte[BUF_SIZE];
    int klen1 = scanner.entry().getKeyLength();
    scanner.entry().getKey(kbuf1);
    Assert.assertEquals(new String(kbuf1, 0, klen1), composeSortedKey(KEY,
        recordIndex));

    if (scanner.advance() && !scanner.atEnd()) {
      // read the next key following the indexed
      byte[] kbuf2 = new byte[BUF_SIZE];
      int klen2 = scanner.entry().getKeyLength();
      scanner.entry().getKey(kbuf2);
      Assert.assertEquals(new String(kbuf2, 0, klen2), composeSortedKey(KEY,
          recordIndex + 1));
    }
  } finally {
    scanner.close();
    reader.close();
  }
}
 
Example 15
Source File: TestTFileByteArrays.java    From RDFS with Apache License 2.0 5 votes vote down vote up
private void readKeyManyTimes(int recordIndex) throws IOException {
  Reader reader = new Reader(fs.open(path), fs.getFileStatus(path).getLen(), conf);

  Scanner scanner =
      reader.createScannerByKey(composeSortedKey(KEY, recordIndex)
          .getBytes(), null);

  // read the indexed key
  byte[] kbuf1 = new byte[BUF_SIZE];
  int klen1 = scanner.entry().getKeyLength();
  scanner.entry().getKey(kbuf1);
  Assert.assertEquals(new String(kbuf1, 0, klen1), composeSortedKey(KEY,
      recordIndex));

  klen1 = scanner.entry().getKeyLength();
  scanner.entry().getKey(kbuf1);
  Assert.assertEquals(new String(kbuf1, 0, klen1), composeSortedKey(KEY,
      recordIndex));

  klen1 = scanner.entry().getKeyLength();
  scanner.entry().getKey(kbuf1);
  Assert.assertEquals(new String(kbuf1, 0, klen1), composeSortedKey(KEY,
      recordIndex));

  scanner.close();
  reader.close();
}
 
Example 16
Source File: TestTFile.java    From big-c with Apache License 2.0 4 votes vote down vote up
/**
 * test none codecs
 */
void basicWithSomeCodec(String codec) throws IOException {
  Path ncTFile = new Path(ROOT, "basic.tfile");
  FSDataOutputStream fout = createFSOutput(ncTFile);
  Writer writer = new Writer(fout, minBlockSize, codec, "memcmp", conf);
  writeRecords(writer);
  fout.close();
  FSDataInputStream fin = fs.open(ncTFile);
  Reader reader =
      new Reader(fs.open(ncTFile), fs.getFileStatus(ncTFile).getLen(), conf);

  Scanner scanner = reader.createScanner();
  readAllRecords(scanner);
  scanner.seekTo(getSomeKey(50));
  assertTrue("location lookup failed", scanner.seekTo(getSomeKey(50)));
  // read the key and see if it matches
  byte[] readKey = readKey(scanner);
  assertTrue("seeked key does not match", Arrays.equals(getSomeKey(50),
      readKey));

  scanner.seekTo(new byte[0]);
  byte[] val1 = readValue(scanner);
  scanner.seekTo(new byte[0]);
  byte[] val2 = readValue(scanner);
  assertTrue(Arrays.equals(val1, val2));
  
  // check for lowerBound
  scanner.lowerBound(getSomeKey(50));
  assertTrue("locaton lookup failed", scanner.currentLocation
      .compareTo(reader.end()) < 0);
  readKey = readKey(scanner);
  assertTrue("seeked key does not match", Arrays.equals(readKey,
      getSomeKey(50)));

  // check for upper bound
  scanner.upperBound(getSomeKey(50));
  assertTrue("location lookup failed", scanner.currentLocation
      .compareTo(reader.end()) < 0);
  readKey = readKey(scanner);
  assertTrue("seeked key does not match", Arrays.equals(readKey,
      getSomeKey(51)));

  scanner.close();
  // test for a range of scanner
  scanner = reader.createScannerByKey(getSomeKey(10), getSomeKey(60));
  readAndCheckbytes(scanner, 10, 50);
  assertFalse(scanner.advance());
  scanner.close();
  reader.close();
  fin.close();
  fs.delete(ncTFile, true);
}
 
Example 17
Source File: TestTFile.java    From hadoop with Apache License 2.0 4 votes vote down vote up
/**
 * test none codecs
 */
void basicWithSomeCodec(String codec) throws IOException {
  Path ncTFile = new Path(ROOT, "basic.tfile");
  FSDataOutputStream fout = createFSOutput(ncTFile);
  Writer writer = new Writer(fout, minBlockSize, codec, "memcmp", conf);
  writeRecords(writer);
  fout.close();
  FSDataInputStream fin = fs.open(ncTFile);
  Reader reader =
      new Reader(fs.open(ncTFile), fs.getFileStatus(ncTFile).getLen(), conf);

  Scanner scanner = reader.createScanner();
  readAllRecords(scanner);
  scanner.seekTo(getSomeKey(50));
  assertTrue("location lookup failed", scanner.seekTo(getSomeKey(50)));
  // read the key and see if it matches
  byte[] readKey = readKey(scanner);
  assertTrue("seeked key does not match", Arrays.equals(getSomeKey(50),
      readKey));

  scanner.seekTo(new byte[0]);
  byte[] val1 = readValue(scanner);
  scanner.seekTo(new byte[0]);
  byte[] val2 = readValue(scanner);
  assertTrue(Arrays.equals(val1, val2));
  
  // check for lowerBound
  scanner.lowerBound(getSomeKey(50));
  assertTrue("locaton lookup failed", scanner.currentLocation
      .compareTo(reader.end()) < 0);
  readKey = readKey(scanner);
  assertTrue("seeked key does not match", Arrays.equals(readKey,
      getSomeKey(50)));

  // check for upper bound
  scanner.upperBound(getSomeKey(50));
  assertTrue("location lookup failed", scanner.currentLocation
      .compareTo(reader.end()) < 0);
  readKey = readKey(scanner);
  assertTrue("seeked key does not match", Arrays.equals(readKey,
      getSomeKey(51)));

  scanner.close();
  // test for a range of scanner
  scanner = reader.createScannerByKey(getSomeKey(10), getSomeKey(60));
  readAndCheckbytes(scanner, 10, 50);
  assertFalse(scanner.advance());
  scanner.close();
  reader.close();
  fin.close();
  fs.delete(ncTFile, true);
}
 
Example 18
Source File: TestTFile.java    From RDFS with Apache License 2.0 4 votes vote down vote up
/**
 * test none codecs
 */
void basicWithSomeCodec(String codec) throws IOException {
  Path ncTFile = new Path(ROOT, "basic.tfile");
  FSDataOutputStream fout = createFSOutput(ncTFile);
  Writer writer = new Writer(fout, minBlockSize, codec, "memcmp", conf);
  writeRecords(writer);
  fout.close();
  FSDataInputStream fin = fs.open(ncTFile);
  Reader reader =
      new Reader(fs.open(ncTFile), fs.getFileStatus(ncTFile).getLen(), conf);

  Scanner scanner = reader.createScanner();
  readAllRecords(scanner);
  scanner.seekTo(getSomeKey(50));
  assertTrue("location lookup failed", scanner.seekTo(getSomeKey(50)));
  // read the key and see if it matches
  byte[] readKey = readKey(scanner);
  assertTrue("seeked key does not match", Arrays.equals(getSomeKey(50),
      readKey));

  scanner.seekTo(new byte[0]);
  byte[] val1 = readValue(scanner);
  scanner.seekTo(new byte[0]);
  byte[] val2 = readValue(scanner);
  assertTrue(Arrays.equals(val1, val2));
  
  // check for lowerBound
  scanner.lowerBound(getSomeKey(50));
  assertTrue("locaton lookup failed", scanner.currentLocation
      .compareTo(reader.end()) < 0);
  readKey = readKey(scanner);
  assertTrue("seeked key does not match", Arrays.equals(readKey,
      getSomeKey(50)));

  // check for upper bound
  scanner.upperBound(getSomeKey(50));
  assertTrue("location lookup failed", scanner.currentLocation
      .compareTo(reader.end()) < 0);
  readKey = readKey(scanner);
  assertTrue("seeked key does not match", Arrays.equals(readKey,
      getSomeKey(51)));

  scanner.close();
  // test for a range of scanner
  scanner = reader.createScannerByKey(getSomeKey(10), getSomeKey(60));
  readAndCheckbytes(scanner, 10, 50);
  assertFalse(scanner.advance());
  scanner.close();
  reader.close();
  fin.close();
  fs.delete(ncTFile, true);
}