question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement writable streams for natively streaming inserts

See original GitHub issue

Is your feature request related to a problem? Please describe. It would be really useful to be able to natively stream records directly into a table (via /insertAll). This would make it much more accessible to transfer significant amounts of data on low-resource platforms like cloud functions.

Describe the solution you’d like A Table method similar to createWriteStream() in the @google-cloud/storage package. Being able to just call mySourceStream.pipe(myTable.createWriteStream()) would be super helpful.

Describe alternatives you’ve considered I’ve managed to get roughly the functionality I’m looking for by calling insert() in arbitrarily sized batches, i.e.

class TableWriteableStream extends Writable {
  constructor (destinationTable) {
    super({ objectMode: true })
    this.destinationTable = destinationTable
    this.records = []
  }

  _write (chunk, encoding, callback) {
    this.records.push(chunk)
    // Every 500 records, call insert()
    if (this.records.length >= 500) {
      const recordsToInsert = this.records
      this.records = []
      this.destinationTable
        .insert(recordsToInsert)
        .then(() => { callback() })
    } else {
      callback()
    }
  }

  _final (callback) {
      // Insert any remaining records ...

Additional context This is somewhat related to #75, though that seems focused on compression (which would be a great option for this feature, just like the ‘gzip’ option for the @google-cloud/storage function).

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
guanzocommented, Jun 9, 2021

Possibly related, would be nice for this lib to implement https://cloud.google.com/bigquery/docs/write-api

1reaction
tswastcommented, Sep 12, 2019

I like this proposal. Having the client do some automatic batching by time / number of records comes with a lot of complexity, though. Also, the streaming API returns errors in the response, so there’d have to be some logic to check for that as well as true HTTP error responses.

Since it’s a complex feature, we should write up a full design doc for this. Ideally we’d share similar semantics with the pub/sub API.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Work with Files Using Streams in Node.js - DigitalOcean
Streams are an efficient way to handle files in Node.js. In this tutorial, you'll create a command-line program, and then use it with ......
Read more >
Using writable streams - Web APIs | MDN
To create a writable stream, we use the WritableStream() constructor; the syntax looks complex at first, but actually isn't too bad.
Read more >
Streams Standard
A transform stream consists of a pair of streams: a writable stream, known as its writable side , and a readable stream, known...
Read more >
Bufferizing data from stream in nodeJS for perfoming bulk insert
The nodejs native stream api sounds like the perfect fit, you should look into using a Writable. The size of the buffer may...
Read more >
Using _writev() to Create a Fast, Writable Stream for ...
We all know how great Node streams are. But it wasn't until I recently needed to create (yet another) writable stream wrapper for ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found