davisusanibar commented on a change in pull request #137:
URL: https://github.com/apache/arrow-cookbook/pull/137#discussion_r824951693



##########
File path: java/source/flight.rst
##########
@@ -0,0 +1,511 @@
+.. 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.
+
+============
+Arrow Flight
+============
+
+This section contains a number of recipes for working with Arrow Flight.
+For more detail about Flight please take a look at `Arrow Flight RPC`_.
+
+.. contents::
+
+Simple Key-Value Storage Service with Arrow Flight
+==================================================
+
+We'll implement a service that provides a key-value store for data, using 
Flight to handle uploads/requests
+and data in memory to store the actual data.
+
+Flight Client and Server
+************************
+
+.. testcode::
+
+    import org.apache.arrow.flight.Action;
+    import org.apache.arrow.flight.AsyncPutListener;
+    import org.apache.arrow.flight.CallStatus;
+    import org.apache.arrow.flight.Criteria;
+    import org.apache.arrow.flight.FlightClient;
+    import org.apache.arrow.flight.FlightDescriptor;
+    import org.apache.arrow.flight.FlightEndpoint;
+    import org.apache.arrow.flight.FlightInfo;
+    import org.apache.arrow.flight.FlightServer;
+    import org.apache.arrow.flight.FlightStream;
+    import org.apache.arrow.flight.Location;
+    import org.apache.arrow.flight.NoOpFlightProducer;
+    import org.apache.arrow.flight.PutResult;
+    import org.apache.arrow.flight.Result;
+    import org.apache.arrow.flight.Ticket;
+    import org.apache.arrow.memory.RootAllocator;
+    import org.apache.arrow.vector.VarCharVector;
+    import org.apache.arrow.vector.VectorLoader;
+    import org.apache.arrow.vector.VectorSchemaRoot;
+    import org.apache.arrow.vector.VectorUnloader;
+    import org.apache.arrow.vector.ipc.message.ArrowRecordBatch;
+    import org.apache.arrow.vector.types.pojo.ArrowType;
+    import org.apache.arrow.vector.types.pojo.Field;
+    import org.apache.arrow.vector.types.pojo.FieldType;
+    import org.apache.arrow.vector.types.pojo.Schema;
+
+    import java.io.IOException;
+    import java.nio.charset.StandardCharsets;
+    import java.util.ArrayList;
+    import java.util.Arrays;
+    import java.util.Collections;
+    import java.util.Iterator;
+    import java.util.List;
+    import java.util.concurrent.ConcurrentHashMap;
+
+    class DataInMemory {
+        private List<ArrowRecordBatch> listArrowRecordBatch;
+        private Schema schema;
+        private long rows;
+        private NoOpFlightProducer cookbookProducer;
+        public DataInMemory(List<ArrowRecordBatch> listArrowRecordBatch, 
Schema schema, long rows) {
+            this.listArrowRecordBatch = listArrowRecordBatch;
+            this.schema = schema;
+            this.rows = rows;
+        }
+        public DataInMemory(List<ArrowRecordBatch> listArrowRecordBatch, 
Schema schema, long rows, NoOpFlightProducer cookbookProducer) {
+            this.listArrowRecordBatch = listArrowRecordBatch;
+            this.schema = schema;
+            this.rows = rows;
+            this.cookbookProducer = cookbookProducer;
+        }
+        public List<ArrowRecordBatch> getListArrowRecordBatch() {
+            return listArrowRecordBatch;
+        }
+        public Schema getSchema() {
+            return schema;
+        }
+        public long getRows() {
+            return rows;
+        }
+        public NoOpFlightProducer getCookbookProducer() {
+            return cookbookProducer;
+        }
+        public void setCookbookProducer(NoOpFlightProducer cookbookProducer) {
+            this.cookbookProducer = cookbookProducer;
+        }
+    }
+    class CookbookProducer extends NoOpFlightProducer {
+        private RootAllocator allocator;
+        private Location location;
+
+        public CookbookProducer(RootAllocator allocator, Location location) {
+            this.allocator = allocator;
+            this.location = location;
+        }
+
+        ConcurrentHashMap<FlightDescriptor, DataInMemory> dataInMemory = new 
ConcurrentHashMap<>();
+        @Override
+        public Runnable acceptPut(CallContext context, FlightStream 
flightStream, StreamListener<PutResult> ackStream) {
+            List<ArrowRecordBatch> listArrowRecordBatch = new ArrayList<>();
+            return () -> {
+                long rows = 0;
+                while (flightStream.next()) {
+                    VectorUnloader unloader = new 
VectorUnloader(flightStream.getRoot());
+                    try (final ArrowRecordBatch arb = 
unloader.getRecordBatch()) {
+                        listArrowRecordBatch.add(arb);
+                        rows += flightStream.getRoot().getRowCount();
+                    }
+                }
+                DataInMemory pojoFlightDataInMemory = new 
DataInMemory(listArrowRecordBatch, flightStream.getSchema(), rows);
+                dataInMemory.put(flightStream.getDescriptor(), 
pojoFlightDataInMemory);
+                ackStream.onCompleted();
+            };
+        }
+
+        @Override
+        public void getStream(CallContext context, Ticket ticket, 
ServerStreamListener listener) {
+            FlightDescriptor flightDescriptor = FlightDescriptor.path(new 
String(ticket.getBytes(), StandardCharsets.UTF_8)); // Recover data for key 
configured
+            DataInMemory dataInMemory = 
this.dataInMemory.get(flightDescriptor);
+            if (dataInMemory == null) {
+                throw CallStatus.NOT_FOUND.withDescription("Unknown 
descriptor").toRuntimeException();
+            } else {
+                VectorSchemaRoot vectorSchemaRoot = 
VectorSchemaRoot.create(this.dataInMemory.get(flightDescriptor).getSchema(), 
allocator);
+                listener.start(vectorSchemaRoot);
+                for (ArrowRecordBatch arrowRecordBatch : 
this.dataInMemory.get(flightDescriptor).getListArrowRecordBatch()) {
+                    VectorLoader loader = new VectorLoader(vectorSchemaRoot);
+                    loader.load(arrowRecordBatch.cloneWithTransfer(allocator));
+                    listener.putNext();
+                }
+                listener.completed();
+            }
+        }
+
+        @Override
+        public void doAction(CallContext context, Action action, 
StreamListener<Result> listener) {
+            FlightDescriptor flightDescriptor = FlightDescriptor.path(new 
String(action.getBody(), StandardCharsets.UTF_8)); // For recover data for key 
configured
+            switch (action.getType()) {
+                case "DELETE":
+                    if (dataInMemory.remove(flightDescriptor) != null) {
+                        Result result = new Result("Delete 
completed".getBytes(StandardCharsets.UTF_8));
+                        listener.onNext(result);
+                    } else {
+                        Result result = new Result("Delete not completed. 
Reason: Key did not exist.".getBytes(StandardCharsets.UTF_8));
+                        listener.onNext(result);
+                    }
+                    listener.onCompleted();
+            }
+        }
+
+        @Override
+        public FlightInfo getFlightInfo(CallContext context, FlightDescriptor 
descriptor) {
+            FlightEndpoint flightEndpoint = new FlightEndpoint(new 
Ticket(descriptor.getPath().get(0).getBytes(StandardCharsets.UTF_8)), location);
+            return new FlightInfo(
+                    dataInMemory.get(descriptor).getSchema(),
+                    descriptor,
+                    Collections.singletonList(flightEndpoint), // Configure a 
key to map back and forward your data using Ticket argument

Review comment:
       Deleted

##########
File path: java/source/flight.rst
##########
@@ -0,0 +1,511 @@
+.. 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.
+
+============
+Arrow Flight
+============
+
+This section contains a number of recipes for working with Arrow Flight.
+For more detail about Flight please take a look at `Arrow Flight RPC`_.
+
+.. contents::
+
+Simple Key-Value Storage Service with Arrow Flight
+==================================================
+
+We'll implement a service that provides a key-value store for data, using 
Flight to handle uploads/requests
+and data in memory to store the actual data.
+
+Flight Client and Server
+************************
+
+.. testcode::
+
+    import org.apache.arrow.flight.Action;
+    import org.apache.arrow.flight.AsyncPutListener;
+    import org.apache.arrow.flight.CallStatus;
+    import org.apache.arrow.flight.Criteria;
+    import org.apache.arrow.flight.FlightClient;
+    import org.apache.arrow.flight.FlightDescriptor;
+    import org.apache.arrow.flight.FlightEndpoint;
+    import org.apache.arrow.flight.FlightInfo;
+    import org.apache.arrow.flight.FlightServer;
+    import org.apache.arrow.flight.FlightStream;
+    import org.apache.arrow.flight.Location;
+    import org.apache.arrow.flight.NoOpFlightProducer;
+    import org.apache.arrow.flight.PutResult;
+    import org.apache.arrow.flight.Result;
+    import org.apache.arrow.flight.Ticket;
+    import org.apache.arrow.memory.RootAllocator;
+    import org.apache.arrow.vector.VarCharVector;
+    import org.apache.arrow.vector.VectorLoader;
+    import org.apache.arrow.vector.VectorSchemaRoot;
+    import org.apache.arrow.vector.VectorUnloader;
+    import org.apache.arrow.vector.ipc.message.ArrowRecordBatch;
+    import org.apache.arrow.vector.types.pojo.ArrowType;
+    import org.apache.arrow.vector.types.pojo.Field;
+    import org.apache.arrow.vector.types.pojo.FieldType;
+    import org.apache.arrow.vector.types.pojo.Schema;
+
+    import java.io.IOException;
+    import java.nio.charset.StandardCharsets;
+    import java.util.ArrayList;
+    import java.util.Arrays;
+    import java.util.Collections;
+    import java.util.Iterator;
+    import java.util.List;
+    import java.util.concurrent.ConcurrentHashMap;
+
+    class DataInMemory {
+        private List<ArrowRecordBatch> listArrowRecordBatch;
+        private Schema schema;
+        private long rows;
+        private NoOpFlightProducer cookbookProducer;
+        public DataInMemory(List<ArrowRecordBatch> listArrowRecordBatch, 
Schema schema, long rows) {
+            this.listArrowRecordBatch = listArrowRecordBatch;
+            this.schema = schema;
+            this.rows = rows;
+        }
+        public DataInMemory(List<ArrowRecordBatch> listArrowRecordBatch, 
Schema schema, long rows, NoOpFlightProducer cookbookProducer) {
+            this.listArrowRecordBatch = listArrowRecordBatch;
+            this.schema = schema;
+            this.rows = rows;
+            this.cookbookProducer = cookbookProducer;
+        }
+        public List<ArrowRecordBatch> getListArrowRecordBatch() {
+            return listArrowRecordBatch;
+        }
+        public Schema getSchema() {
+            return schema;
+        }
+        public long getRows() {
+            return rows;
+        }
+        public NoOpFlightProducer getCookbookProducer() {
+            return cookbookProducer;
+        }
+        public void setCookbookProducer(NoOpFlightProducer cookbookProducer) {
+            this.cookbookProducer = cookbookProducer;
+        }
+    }
+    class CookbookProducer extends NoOpFlightProducer {
+        private RootAllocator allocator;
+        private Location location;
+
+        public CookbookProducer(RootAllocator allocator, Location location) {
+            this.allocator = allocator;
+            this.location = location;
+        }
+
+        ConcurrentHashMap<FlightDescriptor, DataInMemory> dataInMemory = new 
ConcurrentHashMap<>();
+        @Override
+        public Runnable acceptPut(CallContext context, FlightStream 
flightStream, StreamListener<PutResult> ackStream) {
+            List<ArrowRecordBatch> listArrowRecordBatch = new ArrayList<>();
+            return () -> {
+                long rows = 0;
+                while (flightStream.next()) {
+                    VectorUnloader unloader = new 
VectorUnloader(flightStream.getRoot());
+                    try (final ArrowRecordBatch arb = 
unloader.getRecordBatch()) {
+                        listArrowRecordBatch.add(arb);
+                        rows += flightStream.getRoot().getRowCount();
+                    }
+                }
+                DataInMemory pojoFlightDataInMemory = new 
DataInMemory(listArrowRecordBatch, flightStream.getSchema(), rows);
+                dataInMemory.put(flightStream.getDescriptor(), 
pojoFlightDataInMemory);
+                ackStream.onCompleted();
+            };
+        }
+
+        @Override
+        public void getStream(CallContext context, Ticket ticket, 
ServerStreamListener listener) {
+            FlightDescriptor flightDescriptor = FlightDescriptor.path(new 
String(ticket.getBytes(), StandardCharsets.UTF_8)); // Recover data for key 
configured
+            DataInMemory dataInMemory = 
this.dataInMemory.get(flightDescriptor);
+            if (dataInMemory == null) {
+                throw CallStatus.NOT_FOUND.withDescription("Unknown 
descriptor").toRuntimeException();
+            } else {
+                VectorSchemaRoot vectorSchemaRoot = 
VectorSchemaRoot.create(this.dataInMemory.get(flightDescriptor).getSchema(), 
allocator);
+                listener.start(vectorSchemaRoot);
+                for (ArrowRecordBatch arrowRecordBatch : 
this.dataInMemory.get(flightDescriptor).getListArrowRecordBatch()) {
+                    VectorLoader loader = new VectorLoader(vectorSchemaRoot);
+                    loader.load(arrowRecordBatch.cloneWithTransfer(allocator));
+                    listener.putNext();
+                }
+                listener.completed();
+            }
+        }
+
+        @Override
+        public void doAction(CallContext context, Action action, 
StreamListener<Result> listener) {
+            FlightDescriptor flightDescriptor = FlightDescriptor.path(new 
String(action.getBody(), StandardCharsets.UTF_8)); // For recover data for key 
configured
+            switch (action.getType()) {
+                case "DELETE":
+                    if (dataInMemory.remove(flightDescriptor) != null) {
+                        Result result = new Result("Delete 
completed".getBytes(StandardCharsets.UTF_8));
+                        listener.onNext(result);
+                    } else {
+                        Result result = new Result("Delete not completed. 
Reason: Key did not exist.".getBytes(StandardCharsets.UTF_8));
+                        listener.onNext(result);
+                    }
+                    listener.onCompleted();
+            }
+        }
+
+        @Override
+        public FlightInfo getFlightInfo(CallContext context, FlightDescriptor 
descriptor) {
+            FlightEndpoint flightEndpoint = new FlightEndpoint(new 
Ticket(descriptor.getPath().get(0).getBytes(StandardCharsets.UTF_8)), location);

Review comment:
       Updated

##########
File path: java/source/flight.rst
##########
@@ -0,0 +1,511 @@
+.. 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.
+
+============
+Arrow Flight
+============
+
+This section contains a number of recipes for working with Arrow Flight.
+For more detail about Flight please take a look at `Arrow Flight RPC`_.
+
+.. contents::
+
+Simple Key-Value Storage Service with Arrow Flight
+==================================================
+
+We'll implement a service that provides a key-value store for data, using 
Flight to handle uploads/requests
+and data in memory to store the actual data.
+
+Flight Client and Server
+************************
+
+.. testcode::
+
+    import org.apache.arrow.flight.Action;
+    import org.apache.arrow.flight.AsyncPutListener;
+    import org.apache.arrow.flight.CallStatus;
+    import org.apache.arrow.flight.Criteria;
+    import org.apache.arrow.flight.FlightClient;
+    import org.apache.arrow.flight.FlightDescriptor;
+    import org.apache.arrow.flight.FlightEndpoint;
+    import org.apache.arrow.flight.FlightInfo;
+    import org.apache.arrow.flight.FlightServer;
+    import org.apache.arrow.flight.FlightStream;
+    import org.apache.arrow.flight.Location;
+    import org.apache.arrow.flight.NoOpFlightProducer;
+    import org.apache.arrow.flight.PutResult;
+    import org.apache.arrow.flight.Result;
+    import org.apache.arrow.flight.Ticket;
+    import org.apache.arrow.memory.RootAllocator;
+    import org.apache.arrow.vector.VarCharVector;
+    import org.apache.arrow.vector.VectorLoader;
+    import org.apache.arrow.vector.VectorSchemaRoot;
+    import org.apache.arrow.vector.VectorUnloader;
+    import org.apache.arrow.vector.ipc.message.ArrowRecordBatch;
+    import org.apache.arrow.vector.types.pojo.ArrowType;
+    import org.apache.arrow.vector.types.pojo.Field;
+    import org.apache.arrow.vector.types.pojo.FieldType;
+    import org.apache.arrow.vector.types.pojo.Schema;
+
+    import java.io.IOException;
+    import java.nio.charset.StandardCharsets;
+    import java.util.ArrayList;
+    import java.util.Arrays;
+    import java.util.Collections;
+    import java.util.Iterator;
+    import java.util.List;
+    import java.util.concurrent.ConcurrentHashMap;
+
+    class DataInMemory {
+        private List<ArrowRecordBatch> listArrowRecordBatch;
+        private Schema schema;
+        private long rows;
+        private NoOpFlightProducer cookbookProducer;
+        public DataInMemory(List<ArrowRecordBatch> listArrowRecordBatch, 
Schema schema, long rows) {
+            this.listArrowRecordBatch = listArrowRecordBatch;
+            this.schema = schema;
+            this.rows = rows;
+        }
+        public DataInMemory(List<ArrowRecordBatch> listArrowRecordBatch, 
Schema schema, long rows, NoOpFlightProducer cookbookProducer) {
+            this.listArrowRecordBatch = listArrowRecordBatch;
+            this.schema = schema;
+            this.rows = rows;
+            this.cookbookProducer = cookbookProducer;
+        }
+        public List<ArrowRecordBatch> getListArrowRecordBatch() {
+            return listArrowRecordBatch;
+        }
+        public Schema getSchema() {
+            return schema;
+        }
+        public long getRows() {
+            return rows;
+        }
+        public NoOpFlightProducer getCookbookProducer() {
+            return cookbookProducer;
+        }
+        public void setCookbookProducer(NoOpFlightProducer cookbookProducer) {
+            this.cookbookProducer = cookbookProducer;
+        }
+    }
+    class CookbookProducer extends NoOpFlightProducer {
+        private RootAllocator allocator;
+        private Location location;
+
+        public CookbookProducer(RootAllocator allocator, Location location) {
+            this.allocator = allocator;
+            this.location = location;
+        }
+
+        ConcurrentHashMap<FlightDescriptor, DataInMemory> dataInMemory = new 
ConcurrentHashMap<>();
+        @Override
+        public Runnable acceptPut(CallContext context, FlightStream 
flightStream, StreamListener<PutResult> ackStream) {
+            List<ArrowRecordBatch> listArrowRecordBatch = new ArrayList<>();
+            return () -> {
+                long rows = 0;
+                while (flightStream.next()) {
+                    VectorUnloader unloader = new 
VectorUnloader(flightStream.getRoot());
+                    try (final ArrowRecordBatch arb = 
unloader.getRecordBatch()) {
+                        listArrowRecordBatch.add(arb);
+                        rows += flightStream.getRoot().getRowCount();
+                    }
+                }
+                DataInMemory pojoFlightDataInMemory = new 
DataInMemory(listArrowRecordBatch, flightStream.getSchema(), rows);
+                dataInMemory.put(flightStream.getDescriptor(), 
pojoFlightDataInMemory);
+                ackStream.onCompleted();
+            };
+        }
+
+        @Override
+        public void getStream(CallContext context, Ticket ticket, 
ServerStreamListener listener) {
+            FlightDescriptor flightDescriptor = FlightDescriptor.path(new 
String(ticket.getBytes(), StandardCharsets.UTF_8)); // Recover data for key 
configured
+            DataInMemory dataInMemory = 
this.dataInMemory.get(flightDescriptor);
+            if (dataInMemory == null) {
+                throw CallStatus.NOT_FOUND.withDescription("Unknown 
descriptor").toRuntimeException();
+            } else {
+                VectorSchemaRoot vectorSchemaRoot = 
VectorSchemaRoot.create(this.dataInMemory.get(flightDescriptor).getSchema(), 
allocator);
+                listener.start(vectorSchemaRoot);
+                for (ArrowRecordBatch arrowRecordBatch : 
this.dataInMemory.get(flightDescriptor).getListArrowRecordBatch()) {
+                    VectorLoader loader = new VectorLoader(vectorSchemaRoot);
+                    loader.load(arrowRecordBatch.cloneWithTransfer(allocator));
+                    listener.putNext();
+                }
+                listener.completed();
+            }
+        }
+
+        @Override
+        public void doAction(CallContext context, Action action, 
StreamListener<Result> listener) {
+            FlightDescriptor flightDescriptor = FlightDescriptor.path(new 
String(action.getBody(), StandardCharsets.UTF_8)); // For recover data for key 
configured
+            switch (action.getType()) {
+                case "DELETE":
+                    if (dataInMemory.remove(flightDescriptor) != null) {
+                        Result result = new Result("Delete 
completed".getBytes(StandardCharsets.UTF_8));
+                        listener.onNext(result);
+                    } else {
+                        Result result = new Result("Delete not completed. 
Reason: Key did not exist.".getBytes(StandardCharsets.UTF_8));
+                        listener.onNext(result);
+                    }
+                    listener.onCompleted();
+            }
+        }
+
+        @Override
+        public FlightInfo getFlightInfo(CallContext context, FlightDescriptor 
descriptor) {
+            FlightEndpoint flightEndpoint = new FlightEndpoint(new 
Ticket(descriptor.getPath().get(0).getBytes(StandardCharsets.UTF_8)), location);
+            return new FlightInfo(
+                    dataInMemory.get(descriptor).getSchema(),
+                    descriptor,
+                    Collections.singletonList(flightEndpoint), // Configure a 
key to map back and forward your data using Ticket argument
+                    /*bytes=*/-1,
+                    dataInMemory.get(descriptor).getRows()
+            );
+        }
+
+        @Override
+        public void listFlights(CallContext context, Criteria criteria, 
StreamListener<FlightInfo> listener) {
+            dataInMemory.forEach((k, v) -> {
+                        FlightInfo flightInfo = getFlightInfo(null, k);
+                        listener.onNext(flightInfo);
+                    }
+            );

Review comment:
       Added

##########
File path: java/source/flight.rst
##########
@@ -0,0 +1,511 @@
+.. 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.
+
+============
+Arrow Flight
+============
+
+This section contains a number of recipes for working with Arrow Flight.
+For more detail about Flight please take a look at `Arrow Flight RPC`_.
+
+.. contents::
+
+Simple Key-Value Storage Service with Arrow Flight
+==================================================
+
+We'll implement a service that provides a key-value store for data, using 
Flight to handle uploads/requests
+and data in memory to store the actual data.
+
+Flight Client and Server
+************************
+
+.. testcode::
+
+    import org.apache.arrow.flight.Action;
+    import org.apache.arrow.flight.AsyncPutListener;
+    import org.apache.arrow.flight.CallStatus;
+    import org.apache.arrow.flight.Criteria;
+    import org.apache.arrow.flight.FlightClient;
+    import org.apache.arrow.flight.FlightDescriptor;
+    import org.apache.arrow.flight.FlightEndpoint;
+    import org.apache.arrow.flight.FlightInfo;
+    import org.apache.arrow.flight.FlightServer;
+    import org.apache.arrow.flight.FlightStream;
+    import org.apache.arrow.flight.Location;
+    import org.apache.arrow.flight.NoOpFlightProducer;
+    import org.apache.arrow.flight.PutResult;
+    import org.apache.arrow.flight.Result;
+    import org.apache.arrow.flight.Ticket;
+    import org.apache.arrow.memory.RootAllocator;
+    import org.apache.arrow.vector.VarCharVector;
+    import org.apache.arrow.vector.VectorLoader;
+    import org.apache.arrow.vector.VectorSchemaRoot;
+    import org.apache.arrow.vector.VectorUnloader;
+    import org.apache.arrow.vector.ipc.message.ArrowRecordBatch;
+    import org.apache.arrow.vector.types.pojo.ArrowType;
+    import org.apache.arrow.vector.types.pojo.Field;
+    import org.apache.arrow.vector.types.pojo.FieldType;
+    import org.apache.arrow.vector.types.pojo.Schema;
+
+    import java.io.IOException;
+    import java.nio.charset.StandardCharsets;
+    import java.util.ArrayList;
+    import java.util.Arrays;
+    import java.util.Collections;
+    import java.util.Iterator;
+    import java.util.List;
+    import java.util.concurrent.ConcurrentHashMap;
+
+    class DataInMemory {
+        private List<ArrowRecordBatch> listArrowRecordBatch;
+        private Schema schema;
+        private long rows;
+        private NoOpFlightProducer cookbookProducer;
+        public DataInMemory(List<ArrowRecordBatch> listArrowRecordBatch, 
Schema schema, long rows) {
+            this.listArrowRecordBatch = listArrowRecordBatch;
+            this.schema = schema;
+            this.rows = rows;
+        }
+        public DataInMemory(List<ArrowRecordBatch> listArrowRecordBatch, 
Schema schema, long rows, NoOpFlightProducer cookbookProducer) {
+            this.listArrowRecordBatch = listArrowRecordBatch;
+            this.schema = schema;
+            this.rows = rows;
+            this.cookbookProducer = cookbookProducer;
+        }
+        public List<ArrowRecordBatch> getListArrowRecordBatch() {
+            return listArrowRecordBatch;
+        }
+        public Schema getSchema() {
+            return schema;
+        }
+        public long getRows() {
+            return rows;
+        }
+        public NoOpFlightProducer getCookbookProducer() {
+            return cookbookProducer;
+        }
+        public void setCookbookProducer(NoOpFlightProducer cookbookProducer) {
+            this.cookbookProducer = cookbookProducer;
+        }
+    }
+    class CookbookProducer extends NoOpFlightProducer {
+        private RootAllocator allocator;
+        private Location location;
+
+        public CookbookProducer(RootAllocator allocator, Location location) {
+            this.allocator = allocator;
+            this.location = location;
+        }
+
+        ConcurrentHashMap<FlightDescriptor, DataInMemory> dataInMemory = new 
ConcurrentHashMap<>();
+        @Override
+        public Runnable acceptPut(CallContext context, FlightStream 
flightStream, StreamListener<PutResult> ackStream) {
+            List<ArrowRecordBatch> listArrowRecordBatch = new ArrayList<>();
+            return () -> {
+                long rows = 0;
+                while (flightStream.next()) {
+                    VectorUnloader unloader = new 
VectorUnloader(flightStream.getRoot());
+                    try (final ArrowRecordBatch arb = 
unloader.getRecordBatch()) {
+                        listArrowRecordBatch.add(arb);
+                        rows += flightStream.getRoot().getRowCount();
+                    }
+                }
+                DataInMemory pojoFlightDataInMemory = new 
DataInMemory(listArrowRecordBatch, flightStream.getSchema(), rows);
+                dataInMemory.put(flightStream.getDescriptor(), 
pojoFlightDataInMemory);
+                ackStream.onCompleted();
+            };
+        }
+
+        @Override
+        public void getStream(CallContext context, Ticket ticket, 
ServerStreamListener listener) {
+            FlightDescriptor flightDescriptor = FlightDescriptor.path(new 
String(ticket.getBytes(), StandardCharsets.UTF_8)); // Recover data for key 
configured
+            DataInMemory dataInMemory = 
this.dataInMemory.get(flightDescriptor);
+            if (dataInMemory == null) {
+                throw CallStatus.NOT_FOUND.withDescription("Unknown 
descriptor").toRuntimeException();
+            } else {
+                VectorSchemaRoot vectorSchemaRoot = 
VectorSchemaRoot.create(this.dataInMemory.get(flightDescriptor).getSchema(), 
allocator);
+                listener.start(vectorSchemaRoot);
+                for (ArrowRecordBatch arrowRecordBatch : 
this.dataInMemory.get(flightDescriptor).getListArrowRecordBatch()) {
+                    VectorLoader loader = new VectorLoader(vectorSchemaRoot);
+                    loader.load(arrowRecordBatch.cloneWithTransfer(allocator));
+                    listener.putNext();
+                }
+                listener.completed();
+            }
+        }
+
+        @Override
+        public void doAction(CallContext context, Action action, 
StreamListener<Result> listener) {
+            FlightDescriptor flightDescriptor = FlightDescriptor.path(new 
String(action.getBody(), StandardCharsets.UTF_8)); // For recover data for key 
configured
+            switch (action.getType()) {
+                case "DELETE":
+                    if (dataInMemory.remove(flightDescriptor) != null) {
+                        Result result = new Result("Delete 
completed".getBytes(StandardCharsets.UTF_8));
+                        listener.onNext(result);
+                    } else {
+                        Result result = new Result("Delete not completed. 
Reason: Key did not exist.".getBytes(StandardCharsets.UTF_8));
+                        listener.onNext(result);
+                    }
+                    listener.onCompleted();
+            }
+        }
+
+        @Override
+        public FlightInfo getFlightInfo(CallContext context, FlightDescriptor 
descriptor) {
+            FlightEndpoint flightEndpoint = new FlightEndpoint(new 
Ticket(descriptor.getPath().get(0).getBytes(StandardCharsets.UTF_8)), location);
+            return new FlightInfo(
+                    dataInMemory.get(descriptor).getSchema(),
+                    descriptor,
+                    Collections.singletonList(flightEndpoint), // Configure a 
key to map back and forward your data using Ticket argument
+                    /*bytes=*/-1,
+                    dataInMemory.get(descriptor).getRows()
+            );
+        }
+
+        @Override
+        public void listFlights(CallContext context, Criteria criteria, 
StreamListener<FlightInfo> listener) {
+            dataInMemory.forEach((k, v) -> {
+                        FlightInfo flightInfo = getFlightInfo(null, k);
+                        listener.onNext(flightInfo);
+                    }
+            );
+            listener.onCompleted();
+        }
+    }
+
+    // Server
+    Location location = Location.forGrpcInsecure("0.0.0.0", 33333);
+    try (RootAllocator allocator = new RootAllocator(Long.MAX_VALUE)){
+        FlightServer flightServer = FlightServer.builder(allocator, location, 
new CookbookProducer(allocator, location)).build();
+        try {
+            flightServer.start();
+            System.out.println("S1: Server (Location): Listening on port " + 
flightServer.getPort());
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }

Review comment:
       Updated




-- 
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]


Reply via email to