-
Notifications
You must be signed in to change notification settings - Fork 191
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 #5670 from SasinduDilshara/handling-csv-streams
Ad bbe for parsing csv byte streams
- Loading branch information
Showing
5 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
examples/csv-streams-to-record-arrays/csv_streams_to_record_array.bal
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,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"]]); | ||
|
||
// 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); | ||
} |
7 changes: 7 additions & 0 deletions
7
examples/csv-streams-to-record-arrays/csv_streams_to_record_array.md
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,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 ::: |
2 changes: 2 additions & 0 deletions
2
examples/csv-streams-to-record-arrays/csv_streams_to_record_array.metatags
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,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 |
3 changes: 3 additions & 0 deletions
3
examples/csv-streams-to-record-arrays/csv_streams_to_record_array.out
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,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"}] |
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