stream.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. package main // import "github.com/apache/arrow/go/arrow/ipc/cmd/arrow-stream-to-file"
  17. import (
  18. "flag"
  19. "io"
  20. "log"
  21. "os"
  22. "github.com/apache/arrow/go/arrow/arrio"
  23. "github.com/apache/arrow/go/arrow/ipc"
  24. "github.com/apache/arrow/go/arrow/memory"
  25. "golang.org/x/xerrors"
  26. )
  27. func main() {
  28. log.SetPrefix("arrow-stream-to-file: ")
  29. log.SetFlags(0)
  30. flag.Parse()
  31. err := processStream(os.Stdout, os.Stdin)
  32. if err != nil {
  33. log.Fatal(err)
  34. }
  35. }
  36. func processStream(w *os.File, r io.Reader) error {
  37. mem := memory.NewGoAllocator()
  38. rr, err := ipc.NewReader(r, ipc.WithAllocator(mem))
  39. if err != nil {
  40. if xerrors.Is(err, io.EOF) {
  41. return nil
  42. }
  43. return err
  44. }
  45. ww, err := ipc.NewFileWriter(w, ipc.WithAllocator(mem), ipc.WithSchema(rr.Schema()))
  46. if err != nil {
  47. return xerrors.Errorf("could not create ARROW file writer: %w", err)
  48. }
  49. defer ww.Close()
  50. _, err = arrio.Copy(ww, rr)
  51. if err != nil {
  52. return xerrors.Errorf("could not copy ARROW stream: %w", err)
  53. }
  54. err = ww.Close()
  55. if err != nil {
  56. return xerrors.Errorf("could not close output ARROW file: %w", err)
  57. }
  58. return nil
  59. }