Calling Google Cloud Machine Learning APIs from Batch and Stream ETL pipelines

Apache Beam 2.20 includes some handy PTransforms

Lak Lakshmanan
Google Cloud - Community
3 min readApr 28, 2020

--

Google Cloud AI has some very handy “building block” APIs for Natural Language Processing, Vision (e.g. OCR, image classification, logo identification, etc.), and Video Intelligence.

You will often to need call these APIs on a bunch of documents, images, or videos. Sometimes, this is on already collected data (“batch processing”) and sometimes, it is on streaming data. Invoking online APIs one-at-a-time from batch and stream pipelines requires quite a bit of care so that you don’t hit networking, throughput, or throttling limits.

Using Apache Beam to call the Cloud Natural Language API

Fortunately, Apache Beam 2.20 now provides a handy PTransform that does all the heavy lifting for you. To use it, first install Apache Beam:

Get it? Image by Peter Lomas from Pixabay

Here’s a complete program that will run Apache Beam on three sentences locally:

These are the steps in the above pipeline:

  1. Create the in-memory collection
  2. Change each sentence to a NLP Document object
  3. Invoke the NLP API to annotate the Document. Here (look at the features being passed in), we are extracting entities and the document sentiment.
  4. Parse the output of the NLP API (see below)
  5. Write the output to a text file

The output of the NLP API looks like this:

That’s why I’m able to extract the pieces I want as response.sentences[0].text.content and response.document_sentiment.score. Here’s what the output of the pipeline looks like:

Changing input to BigQuery and running on the Cloud

In the above snippet, I ran the pipeline on an in-memory set of sentences and used the DirectRunner, which runs locally. Let’s change the input to BigQuery and run it in Cloud Dataflow:

https://gist.github.com/lakshmanok/a07d488a0b8006c26bdee0a7effd6245

The pipeline now runs on Dataflow off the hackernews comments table in BigQuery:

The result looks like this:

How easy was that, to get the sentiment of a boatload of comments?

Enjoy!

Next steps:

--

--

Lak Lakshmanan
Google Cloud - Community

articles are personal observations and not investment advice.