This is an automated email from the ASF dual-hosted git repository.
zeroshade pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-go.git
The following commit(s) were added to refs/heads/main by this push:
new 3ffe5507 docs(arrow/flight): add a DoPut example (#1686)
3ffe5507 is described below
commit 3ffe55077291ddec6da93f0e839420d20ea16c4f
Author: Vanshaj Poonia <[email protected]>
AuthorDate: Sat Nov 28 00:20:17 2026 +0530
docs(arrow/flight): add a DoPut example (#1686)
### Rationale for this change
There is no example of `DoPut` in `arrow/flight`.
`ExampleRegisterFlightServiceServer` only says in a comment to override
methods "like DoGet, DoPut, etc." The issue mentioned is asking for
this.
Closes #143.
### What changes are included in this PR?
A new `Example_doPut` in `arrow/flight/example_doput_test.go`. The
server embeds `BaseFlightServer` and overrides `DoPut`. It reads the
stream with `flight.NewRecordReader` then counts the rows and answers
with a `PutResult`. The flow is that the client opens `DoPut`, sets a
path descriptor on a `flight.NewRecordWriter` then writes three record
batches, closes the writer and the send side, then reads the result.
### Are these changes tested?
The example has an `// Output:` block, so `go test` checks it. It passes
with `go test ./arrow/flight/ -run Example -race -count=1` (3 runs) and
with `-tags assert,test`. `go vet` and `gofmt` are clean. Like every
test in this package it is skipped under `-short` (see `main_test.go`).
### Are there any user-facing changes?
No. It is a new example that shows up on pkg.go.dev.
### Was AI used for this PR?
**PR code and description written by:**
- [x] Human
- [x] AI
**Reviewed before submission by:**
- [x] Human
- [ ] AI
- [ ] Not reviewed
AI drafted the example code and I reviewed it. I co-edited the
description with AI.
---
arrow/flight/example_doput_test.go | 130 +++++++++++++++++++++++++++++++++++++
1 file changed, 130 insertions(+)
diff --git a/arrow/flight/example_doput_test.go
b/arrow/flight/example_doput_test.go
new file mode 100644
index 00000000..efbe0b0e
--- /dev/null
+++ b/arrow/flight/example_doput_test.go
@@ -0,0 +1,130 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package flight_test
+
+import (
+ "context"
+ "fmt"
+ "io"
+ "log"
+ "strings"
+
+ "github.com/apache/arrow-go/v18/arrow"
+ "github.com/apache/arrow-go/v18/arrow/array"
+ "github.com/apache/arrow-go/v18/arrow/flight"
+ "github.com/apache/arrow-go/v18/arrow/ipc"
+ "github.com/apache/arrow-go/v18/arrow/memory"
+ "google.golang.org/grpc"
+ "google.golang.org/grpc/credentials/insecure"
+)
+
+// putServer counts the rows of every dataset it receives through DoPut.
+type putServer struct {
+ flight.BaseFlightServer
+}
+
+func (s *putServer) DoPut(stream flight.FlightService_DoPutServer) error {
+ rdr, err := flight.NewRecordReader(stream)
+ if err != nil {
+ return err
+ }
+ defer rdr.Release()
+
+ // the descriptor only arrives with the first message of the stream
+ path := strings.Join(rdr.LatestFlightDescriptor().GetPath(), "/")
+
+ var rows int64
+ for rdr.Next() {
+ rows += rdr.RecordBatch().NumRows()
+ }
+ if rdr.Err() != nil {
+ return rdr.Err()
+ }
+
+ return stream.Send(&flight.PutResult{
+ AppMetadata: []byte(fmt.Sprintf("stored %d rows in %s", rows,
path)),
+ })
+}
+
+func Example_doPut() {
+ server := flight.NewFlightServer()
+ server.Init("localhost:0")
+ server.RegisterFlightService(&putServer{})
+
+ go server.Serve()
+ defer server.Shutdown()
+
+ client, err := flight.NewFlightClient(server.Addr().String(), nil,
+ grpc.WithTransportCredentials(insecure.NewCredentials()))
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer client.Close()
+
+ schema := arrow.NewSchema([]arrow.Field{
+ {Name: "id", Type: arrow.PrimitiveTypes.Int64},
+ }, nil)
+
+ bldr := array.NewRecordBuilder(memory.DefaultAllocator, schema)
+ defer bldr.Release()
+
+ // DoPut is a bidirectional stream: the client sends record batches
+ // and the server answers with a PutResult once it has read them all.
+ stream, err := client.DoPut(context.Background())
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ wr := flight.NewRecordWriter(stream, ipc.WithSchema(schema))
+ wr.SetFlightDescriptor(&flight.FlightDescriptor{
+ Type: flight.DescriptorPATH,
+ Path: []string{"example", "ids"},
+ })
+
+ for i := range int64(3) {
+ bldr.Field(0).(*array.Int64Builder).AppendValues([]int64{i *
10, i*10 + 1}, nil)
+ rec := bldr.NewRecordBatch()
+ err = wr.Write(rec)
+ rec.Release()
+ if err != nil {
+ log.Fatal(err)
+ }
+ }
+
+ // Close releases the writer. CloseSend ends the stream, which is what
+ // makes the server's reader stop.
+ if err = wr.Close(); err != nil {
+ log.Fatal(err)
+ }
+ if err = stream.CloseSend(); err != nil {
+ log.Fatal(err)
+ }
+
+ res, err := stream.Recv()
+ if err != nil {
+ log.Fatal(err)
+ }
+ fmt.Println(string(res.AppMetadata))
+
+ // the server is done after sending its result, so the stream ends
+ if _, err = stream.Recv(); err != io.EOF {
+ log.Fatal(err)
+ }
+
+ // Output:
+ // stored 6 rows in example/ids
+}