Database modification

Estimated time: 25–35 minutes

This tutorial shows you how to stream data from a Firebase database, translate the text from English to French, then insert the translated data into a new collection in the database. And you do all of this without writing a single line of code.


Create service

This service will subscribe to new content added to a Firebase database and translate the content to French.

  1. Click create new service.
  2. In the service name box, enter translate-messages.

Configure Firebase database stream

The FirebaseStream block is a generator block. This is different from the other Firebase blocks because it does not require an incoming signal. On service start, the block will begin "listening" for updates to the database configured in the block properties. In this case, the database will be the same database where the tweets were sent and stored from in the Database Insertion tutorial.

[info] Configuration shortcut

To simplify the configuration of this block, search for the FirebaseInsert block under the configured tab of the block library. Drag Add Tweets to Firebase block onto the canvas and copy the content from this configuration into the Stream Content Updates block. They require the exact same authorization credentials.

  1. Visit the Firebase Console.
  2. Click the nio-firebase project.
  3. In the menu on the left, click Authentication.
  4. Click Web Setup in the upper right hand corner. Note the apiKey, databaseURL, and projectId (you should have set the apiKey as an environment variable in the previous Database Insertion tutorial).
  5. Open the nio System Designer tab.
  6. Drag a FirebaseStream block onto your canvas.
  7. Configure the block as follows:
    1. Name: Stream Content Updates
    2. Authenticated User Email: <test email>
    3. Authenticated User Password: [[FIREBASE_PASSWORD]]
    4. API Key: [[FIREBASE_API_KEY]]
    5. Database URL: <databaseURL>
    6. Firebase Project ID: <projectId>
    7. Database Collection: /en/
    8. Return Root Data?: False (deselect radio button)
  8. Click accept.

[info] Returning root data

If Return Root Path? is True (selected), on service start, the block will emit the existing content from the database with unique identifiers for each entry. Otherwise, the block will emit only the new content added to the database collection after the service has started.


Check if signal has data

A Filter block will remove signals without a data attribute from the stream. Any insertion or deletion from the database will send a signal through the service. A deletion will not contain a data attribute. This block will separate the signals that contain data from those that do not. In this case, deletions from the database are emitted by the FALSE terminal and therefore removed from the stream of data.

  1. Drag a Filter block onto your canvas.
  2. Configure the block as follows:
    1. Name: Has Data
    2. Click + Filter Conditions
    3. Condition: {{ $data is not None }}
  3. Click accept.

Get text from signal

Create a new signal that includes the inner attributes from the data signal.

  1. Drag a Modifier block onto your canvas.
  2. Configure the block as follows:
    1. Name: Get Text Attribute
    2. Click + Fields two times and enter the following values:
    3. Exclude Existing Fields?: True (select the radio button)
    4. Attribute Name box 1: text
    5. Attribute Value box 1: {{ $data['text'] }}
    6. Attribute Name box 2: created_at
    7. Attribute Value box 2: {{ $data['created_at'] }}
  3. Click accept.

Translate English text to French

The GoogleTranslate block is used to translate the content of the text attribute of any signal. Here, each message will be passed into the block and the text attribute will be translated to French.

  1. Drag a GoogleTranslate block onto your canvas.
  2. Configure the block as follows:
    1. Name: Translate to French
    2. Destination Language: fr
    3. Source Language: en
  3. Click accept.

Publish translated text

The text has been translated! Now, you are ready to publish to a topic.

  1. Drag a Publisher block onto your canvas.
  2. Configure the block as follows:
    1. Name: Publish Translated
    2. Topic: workshops.translate-messages
  3. Click accept.

Connect blocks and start service

This service pulls the tweets from the Firebase database into the service as signals. The stream is then partitioned into two streams—one containing database insertion signals and one containing database deletion signals. Next, the signal is formatted for the GoogleTranslate block and the text is translated. Finally, the data is published to a topic to be consumed by other services.

  1. Connect the blocks as configured in the diagram.
  2. Click save.
  3. Click start.
  4. Connect a Logger block to the GoogleTranslate block and open the logger panel to see the translated tweet each time content is added to the database.




[warning] No new tweets in logger panel?

  • Make sure all services from both the Twitter and Firebase tutorials are running.
  • You might have too many tweets in your database collection. Delete the /en/ collection from your Firebase database and restart the service.

Create service

This service will take the translated content from the translate-messages service and insert it back into a new collection in the Firebase database.

  1. Click create new service.
  2. In the service name box, enter insert-translated-messages.

Add and configure blocks

First, subscribe to the topic that contains the translated messages, then store them in a new collection in the Firebase database.

  1. Drag a Subscriber block onto your canvas.
  2. Configure the block as follows:
    1. Name: Subscribe Translated
    2. Topic: workshops.translate-messages
  3. Click accept.
  4. Drag a FirebaseInsert block onto your canvas.
  5. Configure the block as follows:
    1. Name: Insert Translated Content
    2. Authenticated User Email: <test email>
    3. Authenticated User Password: [[FIREBASE_PASSWORD]]
    4. API Key: [[FIREBASE_API_KEY]]
    5. Database URL: <databaseURL>
    6. Firebase Project ID: <projectId>
    7. Database Collection: /fr/
  6. Click accept.

The /fr/ collection will look similar to the /en/ collection, but with all messages translated to French.


Connect blocks and start service

Each time a new tweet is added to the /en/ collection, you should see a corresponding entry in the /fr/ collection that contains the translated tweet.

  1. Connect the blocks as configured in the diagram.
  2. Click save.
  3. Click start.
  4. Navigate back to the Firebase Console.
  5. In the menu on the left, click Database.
  6. Expand the fr collection to see your translated tweet data.



Summary

This tutorial taught you how to stream data from a database, transform that data using nio, and insert the modified data back into the database as a new collection. nio can streamline the process of refining and contextualizing collections of both real time and historical data. For example, real-time translation could ease communication for global teams or support services.


Extra credit

Here are some ideas to improve or expand the services you just created.

  • The GoogleTranslate block has the ability to translate between multiple languages. How would you create a new collection in Firebase for each additional language a tweet is translated into?
  • How could you process tweet text in different ways? For example: the WatsonToneAnalyzer can detect emotion in each of the tweets in the database.

Getting help

We're always happy to help with any questions you might have about the nio Platform. View the troubleshooting guide, search the documentation, or post your questions in the forum. You can also contact live support by clicking the chat icon in the lower-right corner of the nio System Designer.

Troubleshooting tips related specifically to this tutorial are presented below.

Error Possible Cause Solution
Not getting any messages in Logger Panel and no error messages. Services are not started. Make sure all services from both the Twitter and Firebase tutorials are running.
No logs showing from FirebaseStream block. Too many tweets in database collection. Delete the /en/ collection from your Firebase database and restart the service.

Congratulations, you did it!

Well done! You've successfully completed the nio 101 tutorials.

results matching ""

    No results matching ""