How to correctly stream data into Apache Arrow JS over Flight RPC: 1. Releasing resources, and 2. Reading the RecordBatch into a Table

I have a simple Arrow Flight JS client that’s reading Flight RPC data from a Python server over grpc-Web. I can retrieve the payload of bytes from the server fine, but am facing a couple of issues:

  1. Converting Raw Bytes to Table: I can’t work out how to convert the raw bytes into a Table format correctly. At the moment, I can get the bytes, but when I run tableFromIPC, it doesn’t produce a table.
  2. Releasing Stream on the Server: When the stream finishes, the Python server doesn’t get released from its stream (doGet). So it keeps eating up RAM until it crashes (e.g., 40 GB worth for a table with 20 rows).
  3. Optimisations to Avoid Memory Copies: If you have any additional feedback regarding optimisations that can be used to avoid memory copies, it would be much appreciated. Particularly in terms of not copying the data into JS arrays if possible. While the PyArrow documentation is extensive, the JS documentation is mostly the API endpoints and function definitions, so this will be really helpful.

Thanks heaps for your help.

Regards,
PB

client.ts:

 import * as grpcWeb from 'grpc-web';
    import { FlightServiceClient } from '../proto/arrow_flight/Arrow_flightServiceClientPb';
    import { Ticket, FlightData, FlightDescriptor, FlightInfo, Criteria } from '../proto/arrow_flight/arrow_flight_pb';
    import { tableFromIPC } from 'apache-arrow';
    
    export class FlightClient {
        private client: FlightServiceClient;
    
        constructor(serverUrl: string) {
            const grpcOptions = {
                withCredentials: true,
            };
            this.client = new FlightServiceClient(serverUrl, null, grpcOptions);
        }
    
        private handleStreamEvents<T>(stream: grpcWeb.ClientReadableStream<T>, processData: (response: T) => void, onEnd?: () => void): void {
            stream.on('data', (response: T) => {
                processData(response);
            });
    
            stream.on('status', (status: grpcWeb.Status) => {
                if (status.code === grpcWeb.StatusCode.OK) {
                    console.log('Stream finished successfully');
                } else {
                    console.error('Stream finished with error:', status);
                }
            });
    
            stream.on('error', (err: grpcWeb.RpcError) => {
                console.error('Stream encountered an error:', err);
                console.error('Error code:', err.code);
                console.error('Error message:', err.message);
                console.error('Error metadata:', err.metadata);
            });
    
            stream.on('end', () => {
                console.log('Stream ended');
                if (onEnd) {
                    onEnd();
                }
            });
        }
    
        public doGet(ticketValue: string): void {
            const ticket = new Ticket();
            ticket.setTicket(ticketValue);
    
            console.log('Sending ticket:', ticketValue);
    
            const stream = this.client.doGet(ticket);
            const chunks: Uint8Array[] = [];
    
            this.handleStreamEvents(
                stream,
                (response: FlightData) => {
                    const dataBody = response.getDataBody_asU8();
                    chunks.push(dataBody);
                },
                () => {
                    const totalLength = chunks.reduce((acc, chunk) => acc + chunk.length, 0);
                    const completeData = new Uint8Array(totalLength);
                    let offset = 0;
    
                    for (const chunk of chunks) {
                        completeData.set(chunk, offset);
                        offset += chunk.length;
                    }
    
                    try {
                        const table = tableFromIPC(completeData);
                        console.log('Received table:', table);
                    } catch (error) {
                        console.error('Failed to decode table:', error);
                    }
                }
            );
        }
    }

index.ts:

import { FlightClient } from './client';

// Configure XMLHttpRequest for grpc-web in Node.js environment
global.XMLHttpRequest = require('xhr2');

const client = new FlightClient('http://localhost:8080');  // Pointing to Envoy proxy

// Test fetchFlightData
client.doGet(`1`);  // Hack to get the data immediately

Then on the server side, my do_get method is like:

flight_server.py
def do_get(self, context, ticket):
df = pd.DataFrame({‘col1’: [1, 2], ‘col2’: [3, 4]})
table = pa.Table.from_pandas(df)

def generate_batches(table):
    for batch in table.to_batches():
        yield batch

return pa.flight.GeneratorStream(table.schema, generate_batches(table))

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật