Help me understand the looker action to update the data in Bigquery

Please help me set up the Looker action end to end from setting the infra and getting the permission and also what I am going to do is there is table look  in dashboard and there will be three column where we need to write some notes using the action and then it has to go to cloud function and it has to update the bigquery table

Solved Solved
0 8 691
1 ACCEPTED SOLUTION

Correct, thats specified in the answer above.

View solution in original post

8 REPLIES 8

Hi SachinS

if I understand correctly, you want to be able to write back to BigQuery. I had put together a guide a while back, happy to share that with you. Please be aware: this has been created a while ago, so some things might have changed from wording/functionality, but it should explain the way on how to set this up!

The principle

  • Add a dimension in Looker with a ‘cell’ action
  • This action includes a form where the user adds notes
  • This form is sent to a cloud function
  • The cloud function inserts the form data into a BQ table
  • This BQ table is joined to the Looker data model

Note on joins

  • Assume you are annotating information that takes place for a specific geography and specific date
  • Each note would exist on the grain of one note per geo + day
  • Therefore the notes table needs to join to the original dataset on geo + day as primary key
  • Therefore when you write to BigQuery each row must contain geo + day + note text

LookML for action

Screenshot 2024-08-20 at 15.39.49.png

 

 

BigQuery Schema

  • Create a table called 'annotations' in the ‘public’ schema
  • Can change these but would need to update python code in the cloud function below
  • Columns are ‘geo_id’, ‘date’ and ‘notes’
  • If we use the column names and data types exactly we can use a helpful insert_rows_json method in Python to insert the new rows into BQ

Cloud Function (Python 3)

  • Make a service account with the ability to invoke cloud functions and write to BigQuery
  • Give ‘allUsers’ the ability to invoke cloud functions so we can run this without authentication
  • Requires you to put the project somewhere non-google-owned (like a joonix domain)
  • Make a new Cloud function using Python 3
  • Must be invoked by HTTP without authentication
  • Make sure it’s executed by the service account you just made
  • No need for any special parameters or settings on the function
  • Add the code below. Note that there are two files to add - a requirements.txt file and a main.py file once complete make note of the URL required to invoke the function - we need this in the action LookML above
  • Make sure the ‘entry point’ function is the name of the function you wrote (i.e. ‘annotate’ here)

 

 

 

 

 

## requirements.txt
google-cloud-bigquery==1.27.2
## main.py
from google.cloud import bigquery
client = bigquery.Client()

def annotate(request):
    """Update a BQ table from a Looker action"""
    request_json = request.get_json()
    geo = request_json['data']['geo_id']
    date = request_json['data']['date']
    notes = request_json['form_params']['notes']
    data = [{'geo_id': geo, 'date': date, 'notes': notes}]
    table_ref = client.dataset('public').table('annotations') # Check schema + table name
    table = client.get_table(table_ref)
    try:
        r =  client.insert_rows_json(table, data)
        return {"inserted_data": data[0],
                "looker": {
                    "success": true,
                    "refresh_query": true
                }
            }
    except Exception as e:
        print(e) # Errors will get logged in Stackdriver

 

 

 

 

 

LookML for join

 

 

 

 

 

 

```
 join: annotations { # Or whatever the table name is
    relationship: one_to_one
    type: left_outer
    sql_on: ${annotations.geo_id} = ${table.geo_id} AND ${annotations.date_raw} = ${table.date_raw} ;;
  }
```

 

 

 

 

 

 

and also cloud function should be in allow all traffic state to work on this looker , because in my case looker is one GCP project and cloud function and bigquery is in another GCP project

Correct, thats specified in the answer above.

In our case clients are not ok with allow all traffic , in that case what other alternative steps need to follow for this

and also do we have any supporting document that says that allow all traffic is must for looker action

@marcwo  can you please reply on the above queries

Hi

this is a guide on a way to achieve what you are trying to solve, there is no documentation stating that.
There is no way currently to send authentication through the cell_action. The only way I can think of right now is to imitate some kind of authentication thats user_attribute based and gets send with the action and get compared to something for example stored in secret manager, to ensure only users who are authorized can successfully trigger the endpoint.

Thanks and have a good day. 

@marcwo  is there any other way to achieve my usecase , if we cant use the looker action with allow all traffic cloud function.

Thank you