Firebase is a mobile- and web application development platform that helps you deliver richer app experiences. You can send direct notifications to your mobile app and website through Firebase Cloud Messaging.

The Realtime Database and Cloud Firestore can stock document-structured data and synchronize the corresponding apps in milliseconds whenever a data transformation occurs. The app and its database listen to each other, providing your customers with a more reactive app experience. Using Firebase Cloud Functions you can write backend code to respond to events happening in the Firebase platform without having to deal with any servers. 


Prerequisites

Use the TD Console to Create Your Connection

Create a New Connection

When you configure a data connection, you provide authentication to access the integration. In Treasure Data, you configure the authentication and then specify the source information.

  1. Open TD Console.

  2. Navigate to Integrations Hub > Catalog.

  3. Search for and select Firebase.

    The following dialog opens.


  4. Type or paste the credential from your Google Developer Console service account.

    For example:

    {
      "type": "service_account",
      "project_id": "megmida-95",
      "private_key_id": "e793538838c9daa051b2b2f48eea31e10cf46f75",
      "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADA..........br8RqYJePR6sU+68QfAF\n-----END PRIVATE KEY-----\n",
      "client_email": "firebase-adminsdk-ui5s9@megmida-95.iam.gserviceaccount.com",
      "client_id": "114220230186840911629",
      "auth_uri": "https://accounts.google.com/o/oauth2/auth",
      "token_uri": "https://oauth2.googleapis.com/token",
      "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
      "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-ui5s9@megmida-95.iam.gserviceaccount.com"
    }


  5. Select Done.

Example Service Account JSON File

A sample JSON file:

{
"type": "service_account",
"project_id": "[PROJECT-ID]",
"private_key_id": "[KEY-ID]",
"private_key": "-----BEGIN PRIVATE KEY-----\n[PRIVATE-KEY]\n-----END PRIVATE KEY-----\n",
"client_email": "[SERVICE-ACCOUNT-EMAIL]",
"client_id": "[CLIENT-ID]",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/[SERVICE-ACCOUNT-EMAIL]"
}

Configure Export Results in Your Data Connection

In this step, you create or reuse a query. In the query, you configure the data connection.

Configure the Connection by Specifying the Parameters

  1. Open the TD Console.

  2. Navigate to Data Workbench > Queries.

  3. Select the query that you plan to use to export data.

  4. Select Export Results located at top of your query editor. The Choose Integration dialog opens. You have two options when selecting a connection to use to export the results, using an existing connection or creating a new one.


  5. Use an existing connection

    1. Type the connection name in the search box to filter.

    2. Select your connection

  6. Specify the export details.


 The following is a sample template configuration: 

{
	"message": {
		"notification": {
			"title": "___title___",
			"body": "___body___"
		},
		"topic": "weather"
	}
}

Example of a Query to Populate Firebase

From Treasure Data, run the following query and export results to a connection for Firebase: 

Select 'test_body' as body, 'test_title' as title

Optionally Use Scheduled Jobs for Export

You can use Scheduled Jobs with Result Export, to periodically write the output result to a target destination that you specify. 

Optionally Configure Export Results in Workflow

Within Treasure Workflow, you can specify the use of this data connector to export data.

timezone: UTC

_export:
  td:
    database: sample_datasets

+td-result-into-firebase:
  td>: queries/sample.sql
  result_connection: your_connections_name
  result_settings:
    service_account_json: '{"type":"service_account","project_id":"project_id","private_key_id":"####"}'
    project_id: project_id
    message_template: {"message":{"notification":{"title":"___title___","body":"___body___"},"topic":"weather"}}
    fast_mode: true
    retry_count: 7
    retry_initial_wait_millis: 500
    max_retry_wait_millis: 300000

Optionally, read more information on using data connectors in workflow to export data.