zeroshade commented on code in PR #1686: URL: https://github.com/apache/arrow-go/pull/1686#discussion_r4721709250
########## 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 := int64(0); i < 3; i++ { + 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) + } + } + + // closing the writer ends the IPC stream, CloseSend tells the server + // that the client has nothing more to send Review Comment: Closing the writer does not end the IPC stream for Flight: `flight.Writer.Close` only releases the writer's state, because the Flight payload writer's `Close` does nothing and nothing is sent. The server's reader stops because `CloseSend` ends the client's side of the stream. Since people will copy this, could you reword it along the lines of "Close releases the writer; CloseSend ends the stream, which is what makes the server's reader stop"? ########## 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 := int64(0); i < 3; i++ { Review Comment: Optional: `for i := range int64(3)`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
