-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add parquet file output to the data command
- Loading branch information
1 parent
faf59db
commit 2c3303a
Showing
6 changed files
with
101 additions
and
4 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
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
50 changes: 50 additions & 0 deletions
50
src/main/java/org/apache/camel/jbang/ai/util/ParquetUtil.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,50 @@ | ||
package org.apache.camel.jbang.ai.util; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
import org.apache.avro.Schema; | ||
import org.apache.avro.generic.GenericData; | ||
import org.apache.avro.generic.GenericRecord; | ||
import org.apache.camel.jbang.ai.types.AlpacaRecord; | ||
import org.apache.parquet.avro.AvroParquetWriter; | ||
import org.apache.parquet.io.LocalOutputFile; | ||
|
||
public final class ParquetUtil { | ||
|
||
private static final String SCHEMA_FILE = "/training-set-schema.avsc"; | ||
|
||
private ParquetUtil() { | ||
throw new IllegalStateException("Util final class should be instantiated."); | ||
} | ||
|
||
public static void saveParquet(List<AlpacaRecord> alpacaRecords, Path outputPath) { | ||
|
||
Schema schema; | ||
try (var schemaContent = ParquetUtil.class.getResourceAsStream(SCHEMA_FILE)) { | ||
schema = new Schema.Parser().parse(schemaContent); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to load Parquet schema", e); | ||
} | ||
|
||
var outputFile = new LocalOutputFile(outputPath); | ||
var builder = AvroParquetWriter | ||
.<GenericRecord>builder(outputFile) | ||
.withSchema(schema); | ||
|
||
try (var writer = builder.build()) { | ||
for (AlpacaRecord ar : alpacaRecords) { | ||
GenericRecord r = new GenericData.Record(schema); | ||
r.put("input", ar.getInput()); | ||
r.put("instruction", ar.getInstruction()); | ||
r.put("output", ar.getOutput()); | ||
writer.write(r); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to write Parquet file", e); | ||
} | ||
|
||
} | ||
|
||
} |
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,10 @@ | ||
{ | ||
"namespace": "org.apache.camel", | ||
"type": "record", | ||
"name": "AlpacaRecord", | ||
"fields": [ | ||
{"name": "instruction", "type": "string"}, | ||
{"name": "input", "type": "string"}, | ||
{"name": "output", "type": "string"} | ||
] | ||
} |