Last active
January 9, 2022 22:47
-
-
Save michaeloyer/6ed37e70d2b46c2c812318f1e1274cf8 to your computer and use it in GitHub Desktop.
Example Showing how CSV Provider will use chunks of a stream while iterating
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
#r "nuget:FSharp.Data" | |
open System.IO | |
open System.Text | |
open FSharp.Data | |
type Csv = CsvProvider<"A,B,C\n1,2,3", CacheRows=false> | |
let csvStream = | |
let stream = new MemoryStream() | |
stream.Write(Encoding.UTF8.GetBytes("A,B,C\n")) | |
for i in 1..500 do | |
stream.Write(Encoding.UTF8.GetBytes($"{i},{i+1},{i+2}\n")) | |
stream.Position <- 0 | |
stream | |
let mutable position: int64 = 0 | |
for row in Csv.Load(csvStream).Rows do | |
if position <> csvStream.Position then | |
position <- csvStream.Position | |
printfn $"A = {row.A}; B = {row.B}; C = {row.C}; StreamPosition = {csvStream.Position}" |
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
A = 1; B = 2; C = 3; StreamPosition = 1024 | |
A = 112; B = 113; C = 114; StreamPosition = 2048 | |
A = 197; B = 198; C = 199; StreamPosition = 3072 | |
A = 283; B = 284; C = 285; StreamPosition = 4096 | |
A = 368; B = 369; C = 370; StreamPosition = 5120 | |
A = 453; B = 454; C = 455; StreamPosition = 5688 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://fsprojects.github.io/FSharp.Data/library/CsvProvider.html#Handling-big-datasets