forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eugenp#17840 from AndreiBranza/BAEL-8046-get-the-s…
…chema-from-an-avro-file BAEL-8046 | Article code
- Loading branch information
Showing
3 changed files
with
107 additions
and
1 deletion.
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
41 changes: 41 additions & 0 deletions
41
...raries/src/main/java/com/baeldung/apache/avro/schemafromavrofile/AvroSchemaExtractor.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,41 @@ | ||
package com.baeldung.apache.avro.schemafromavrofile; | ||
|
||
import org.apache.avro.Schema; | ||
import org.apache.avro.file.DataFileReader; | ||
import org.apache.avro.generic.GenericDatumReader; | ||
import org.apache.avro.generic.GenericRecord; | ||
import org.apache.avro.io.DatumReader; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class AvroSchemaExtractor { | ||
|
||
public static Schema extractSchema(String avroFilePath) throws IOException { | ||
|
||
File avroFile = new File(avroFilePath); | ||
DatumReader<GenericRecord> datumReader = new GenericDatumReader<>(); | ||
|
||
try (DataFileReader<GenericRecord> dataFileReader = new DataFileReader<>(avroFile, datumReader)) { | ||
return dataFileReader.getSchema(); | ||
} | ||
} | ||
|
||
public static List<GenericRecord> readAvroData(String avroFilePath) throws IOException { | ||
|
||
File avroFile = new File(avroFilePath); | ||
DatumReader<GenericRecord> datumReader = new GenericDatumReader<>(); | ||
List<GenericRecord> records = new ArrayList<>(); | ||
|
||
try (DataFileReader<GenericRecord> dataFileReader = new DataFileReader<>(avroFile, datumReader)) { | ||
GenericRecord record = null; | ||
while (dataFileReader.hasNext()) { | ||
record = dataFileReader.next(record); | ||
records.add(record); | ||
} | ||
} | ||
return records; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...rc/test/java/com/baeldung/apache/avro/schemafromavrofile/AvroSchemaExtractorUnitTest.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,65 @@ | ||
package com.baeldung.apache.avro.schemafromavrofile; | ||
|
||
import org.apache.avro.Schema; | ||
import org.apache.avro.file.DataFileWriter; | ||
import org.apache.avro.generic.GenericData; | ||
import org.apache.avro.generic.GenericDatumWriter; | ||
import org.apache.avro.generic.GenericRecord; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
public class AvroSchemaExtractorUnitTest { | ||
|
||
@TempDir | ||
Path tempDir; | ||
|
||
private File avroFile; | ||
private Schema schema; | ||
|
||
@BeforeEach | ||
void setUp() throws IOException { | ||
schema = new Schema.Parser().parse(""" | ||
{ | ||
"type": "record", | ||
"name": "User", | ||
"fields": [ | ||
{"name": "name", "type": "string"}, | ||
{"name": "age", "type": "int"} | ||
] | ||
} | ||
"""); | ||
avroFile = tempDir.resolve("test.avro").toFile(); | ||
|
||
GenericRecord user1 = new GenericData.Record(schema); | ||
user1.put("name", "John Doe"); | ||
user1.put("age", 30); | ||
|
||
try (DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<>(new GenericDatumWriter<>(schema))) { | ||
dataFileWriter.create(schema, avroFile); | ||
dataFileWriter.append(user1); | ||
} | ||
} | ||
|
||
@Test | ||
void whenSchemaIsExistent_thenItIsExtractedCorrectly() throws IOException { | ||
Schema extractedSchema = AvroSchemaExtractor.extractSchema(avroFile.getPath()); | ||
|
||
assertEquals(schema, extractedSchema); | ||
} | ||
|
||
@Test | ||
void whenAvroFileHasContent_thenItIsReadCorrectly() throws IOException { | ||
List<GenericRecord> records = AvroSchemaExtractor.readAvroData(avroFile.getPath()); | ||
|
||
assertEquals("John Doe", records.get(0).get(0).toString()); | ||
} | ||
} |