Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ad bbe for parsing csv byte streams #5670

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import ballerina/data.csv;
import ballerina/io;

type Book record {
string name;
string author;
decimal price;
string publishedDate;
};

type BookMinimal record {|
string name;
string author;
|};

const csvFilePath = "./files/csv_file.csv";

public function main() returns error? {
// Write a CSV file.
check io:fileWriteCsv(csvFilePath, [["name", "author", "price", "publishedDate"],
["Effective Java", "Joshua Bloch", "45.99", "2018-01-01"],
["Clean Code", "Robert C. Martin", "37.5", "2008-08-01"]]);
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved

// Read the CSV file as a byte stream.
stream<byte[], error?> bookStream = check io:fileReadBlocksAsStream(csvFilePath);

// Parse as CSV byte stream to an array of records.
Book[] bookArray = check csv:parseStream(bookStream);
io:println(bookArray);

bookStream = check io:fileReadBlocksAsStream(csvFilePath);

// Parse the CSV byte stream as an array of records with data projection.
// Here only the fields specified in the target record type (`name` and `author` fields)
// are included in the constructed value.
BookMinimal[] briefBookArray = check csv:parseStream(bookStream);
io:println(briefBookArray);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Parse CSV byte stream to Ballerina record array

The Ballerina `data.csv` library allows parsing the CSV byte streams as arrays of Ballerina records, making it easier to process CSV data directly from `streams`. This functionality supports data projection, enabling developers to extract and map only the required fields to the target `record` type.

::: code csv_streams_to_record_array.bal :::

::: out csv_streams_to_record_array.out :::
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
description: This BBE demonstrates the parsing of CSV byte streams into Ballerina record arrays, both with and without data projection.
keywords: ballerina, ballerina by example, bbe, csv, csv byte streams, csv streams, record, record array, parseStream, csv data module, data.csv, data projection, csv to stream
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$ bal run csv_streams_to_record_array.bal
[{"name":"Effective Java","author":"Joshua Bloch","price":45.99,"publishedDate":"2018-01-01"},{"name":"Clean Code","author":"Robert C. Martin","price":37.5,"publishedDate":"2008-08-01"}]
[{"name":"Effective Java","author":"Joshua Bloch"},{"name":"Clean Code","author":"Robert C. Martin"}]
7 changes: 7 additions & 0 deletions examples/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -4739,6 +4739,13 @@
"verifyBuild": true,
"verifyOutput": true,
"isLearnByExample": true
},
{
"name": "Parse CSV byte streams to records",
"url": "csv-streams-to-record-array",
"verifyBuild": true,
"verifyOutput": true,
"isLearnByExample": true
}
]
},
Expand Down