Skip to content

Commit

Permalink
Merge pull request #5670 from SasinduDilshara/handling-csv-streams
Browse files Browse the repository at this point in the history
Ad bbe for parsing csv byte streams
  • Loading branch information
gimantha authored Sep 30, 2024
2 parents eca4bea + 11d0b10 commit effcd75
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
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);
}
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

0 comments on commit effcd75

Please sign in to comment.