-
Notifications
You must be signed in to change notification settings - Fork 596
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created AvroFileReader and unittest, Update ExtractCohort and Extract…
…CohortEngine (#7174) -Created AvroFileReader Update ExtractCohort and ExtractCohortEngine to accept a AvroFile Testing: Created a test for AvroFileReader Co-authored-by: Marianie-Simeon <msimeon@broadinstitue.org>
- Loading branch information
1 parent
98a024d
commit a9c1885
Showing
5 changed files
with
130 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/main/java/org/broadinstitute/hellbender/utils/bigquery/AvroFileReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.broadinstitute.hellbender.utils.bigquery; | ||
|
||
import org.apache.avro.file.DataFileStream; | ||
import org.apache.avro.generic.GenericDatumReader; | ||
import org.apache.avro.generic.GenericRecord; | ||
import org.apache.avro.io.BinaryDecoder; | ||
import org.apache.avro.io.DatumReader; | ||
import org.apache.avro.io.DecoderFactory; | ||
import org.broadinstitute.hellbender.exceptions.GATKException; | ||
import org.broadinstitute.hellbender.utils.bigquery.GATKAvroReader; | ||
import org.broadinstitute.hellbender.utils.io.IOUtils; | ||
import org.apache.avro.Schema; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Iterator; | ||
|
||
public class AvroFileReader implements GATKAvroReader { | ||
private DatumReader<GenericRecord> datumReader; | ||
private DataFileStream<GenericRecord> dataFileStream; | ||
private org.apache.avro.Schema schema; | ||
|
||
// Decoder object will be reused to avoid re-allocation and too much garbage collection. | ||
private BinaryDecoder decoder = null; | ||
|
||
// GenericRecord object will be reused. | ||
private GenericRecord nextRow = null; | ||
|
||
public AvroFileReader(final String avroFileURI ) { | ||
try { | ||
final Path avroFilePath = IOUtils.getPath(avroFileURI); | ||
|
||
datumReader = new GenericDatumReader<>(); | ||
dataFileStream = new DataFileStream<>(Files.newInputStream(avroFilePath), datumReader); | ||
schema = dataFileStream.getSchema(); | ||
} catch (IOException e ) { | ||
throw new GATKException("I/O Error", e); | ||
} | ||
} | ||
|
||
@Override | ||
public Schema getSchema() { | ||
return schema; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return dataFileStream.hasNext(); | ||
} | ||
|
||
@Override | ||
public GenericRecord next() { | ||
return dataFileStream.next(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
try { | ||
dataFileStream.close(); | ||
} catch (IOException e ) { | ||
throw new GATKException("Error closing stream", e); | ||
} | ||
} | ||
|
||
@Override | ||
public Iterator<GenericRecord> iterator() { | ||
return this; | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
src/test/java/org/broadinstitute/hellbender/utils/bigquery/AvroFileReaderUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.broadinstitute.hellbender.utils.bigquery; | ||
|
||
|
||
import org.apache.avro.generic.GenericRecord; | ||
import org.broadinstitute.hellbender.GATKBaseTest; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
import org.apache.avro.Schema; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* A class to test the functionality of {@link AvroFileReader}. | ||
*/ | ||
public class AvroFileReaderUnitTest extends GATKBaseTest { | ||
|
||
private static final String avroFileName = "src/test/java/org/broadinstitute/hellbender/utils/bigquery/avro_test_avro_test_file.avro"; | ||
private static final AvroFileReader avroFile = new AvroFileReader(avroFileName); | ||
|
||
@Test() | ||
public void testGetSchema() { | ||
Schema avroFileSchema = avroFile.getSchema(); | ||
String testAvroFileSchema = "{\"type\":\"record\",\"name\":\"Root\",\"fields\":[{\"name\":\"test_string\",\"type\":[\"null\",\"string\"]},{\"name\":\"test_float\",\"type\":[\"null\",\"double\"]},{\"name\":\"test_integer\",\"type\":[\"null\",\"long\"]}]}"; | ||
Assert.assertEquals(avroFileSchema.toString(), testAvroFileSchema, "AvroFileSchema did not match."); | ||
} | ||
|
||
@Test() | ||
public void testAvroFileHasNext() { | ||
boolean avroFileHasNext = avroFile.hasNext(); | ||
Assert.assertTrue(avroFileHasNext, "Arvo File Reader didn't detect"); | ||
} | ||
|
||
@Test() | ||
public void testAvroFileNext() { | ||
GenericRecord avroFileNext = avroFile.next(); | ||
String testAvroFileNext = "{\"test_string\": \"one\", \"test_float\": 1111111.0, \"test_integer\": 1}"; | ||
Assert.assertEquals(avroFileNext.toString(), testAvroFileNext, "AvroFile Next did not match."); | ||
} | ||
|
||
} |
Binary file added
BIN
+408 Bytes
src/test/java/org/broadinstitute/hellbender/utils/bigquery/avro_test_avro_test_file.avro
Binary file not shown.