csv.go 822 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/apache/arrow/go/arrow"
  6. "github.com/apache/arrow/go/arrow/csv"
  7. )
  8. func main() {
  9. f := bytes.NewBufferString(`## a simple set of data: int64;float64;string
  10. 0;0;str-0
  11. 1;1;str-1
  12. 2;2;str-2
  13. 3;3;str-3
  14. 4;4;str-4
  15. 5;5;str-5
  16. 6;6;str-6
  17. 7;7;str-7
  18. 8;8;str-8
  19. 9;9;str-9
  20. `)
  21. schema := arrow.NewSchema(
  22. []arrow.Field{
  23. {Name: "i64", Type: arrow.PrimitiveTypes.Int64},
  24. {Name: "f64", Type: arrow.PrimitiveTypes.Float64},
  25. {Name: "str", Type: arrow.BinaryTypes.String},
  26. },
  27. nil, // no metadata
  28. )
  29. r := csv.NewReader(
  30. f, schema,
  31. csv.WithComment('#'), csv.WithComma(';'),
  32. csv.WithChunk(4),
  33. )
  34. defer r.Release()
  35. n := 0
  36. for r.Next() {
  37. rec := r.Record()
  38. for i, col := range rec.Columns() {
  39. fmt.Printf("rec[%d][%q]: %v\n", n, rec.ColumnName(i), col)
  40. }
  41. n++
  42. }
  43. }