Is there a way to let users update data on DB right from looks?
I read about actions here but I found it very abstract.
Can someone please explain the process for this?
Thanks,
Krish.
Best answer by jesse.carah
View originalIs there a way to let users update data on DB right from looks?
I read about actions here but I found it very abstract.
Can someone please explain the process for this?
Thanks,
Krish.
Best answer by jesse.carah
View originalHey Krishna,
I’ve been able to write data from Looker to BigQuery using both Data Actions as well as the Looker Action Hub. In either case, you’ll need to push data from Looker to some middleware that will interpret the webhook from Looker and perform the necessary operations to then stream the data to BigQuery.
Luckily, Google has a great service called Google Cloud Functions that makes this really easy. Like AWS’s Lambda, Cloud Functions let you deploy code that gets executed based off of some event. With a data action, you can push JSON containing data from Looker as well as user-defined form parameters to a Cloud Function endpoint. The Cloud Function then parses the JSON, extracts the relevant values, and calls on the BigQuery SDK to stream the results to BigQuery.
Here’s a quick overview of how to use Cloud Functions to stream data from Looker to BigQuery. In this example, we’ll create a data action and cloud function that lets an end user persist an annotation to BigQuery:
In this example, we’re going to attach a data action to field, and allow end-users to mark whether or not a name is a cool name.
dimension: name {
type: string
sql: ${TABLE}.name ;;
action: {
label: "Cool Name?"
url: ""
param: {
name: "name"
value: "{{ value }}"
}
form_param: {
name: "annotation"
type: select
label: "Cool name?"
default: "No"
description: "Do you think that this name is a cool name?"
option: {
name: "No"
}
option: {
name: "Yes"
}
}
}
}
Note: We’re going to leave the url blank for now. Once we’ve spun up the cloud function we’ll paste the endpoint in.
We’re now going to write a simple Python function that writes the user selected annotation to BigQuery, and place it in main.py
import google.cloud.bigquery as bigquery
import datetime
import time
def annotation(request):
r = request.get_json() # Fetch the data action JSON
client = bigquery.Client()
dataset_id = '' # Replace with name of the BQ dataset
table_id = '' # replace with your table ID
table_ref = client.dataset(dataset_id).table(table_id)
table = client.get_table(table_ref) # API request
# request variables
name = r['data']['name']
annotation = r['form_params']['annotation']
# system variables
sys_time = int(time.time())
row_to_insert = [
(
name,
annotation,
datetime.datetime.fromtimestamp(sys_time).strftime('%Y-%m-%d %H:%M:%S')
)
]
row = client.insert_rows(table, row_to_insert) # API request to insert row
return '{"looker": {"success": true,"refresh_query": true}}' # return success response to Looker
Additional things to configure:
google-cloud-bigquery==1.5.0
in requirements.txt
Cheers!
Jesse
Is it possible to push all the data this way into BigQuery? This would be super useful to create custom segments of users for example.
Hello Jesse!
I followed the exact process, but I am seeing an error on Cloud Functions:
NameError: name ‘bigquery’ is not defined
I included google-cloud-bigquery==1.5.0 in requirements.txt but still am seeing the same error.
EDIT:
Fixed this by adding “from google.cloud import bigquery”
Hey Krish, good catch. It looks like I didn’t copy over the first line of the cloud function when I pasted it in. Did you get the action to work?
Dimitri – yes, that is totally possible, but we’ll need to leverage the Action Hub framework to push all results to BigQuery. I’ll take that as a challenge and try to get you a working example sometime next week.
Hey Jesse,
Yes I finally made it work. But there is one issue (which I am trying to fix).
Data is being duplicated whenever I try to update. Any idea why that’s happening?
I tried updating rows with ids 8 and 10, and you can see data being duplicated
Hey Krish,
Check out this discussion about the append-only nature of BigQuery.
I think the best move here is to have your Cloud Function insert a timestamp when each record is created, and create a view that selects only the most recent record. One approach to doing that is explained here.
Cheers!
Jesse
Hey Dimitri,
I just wanted to update you that I’m making progress, but my goal to getting a working POC up and running this week was perhaps too bold.
In the meantime, could you elaborate on the specific use-case? What sort of data are you trying to push back to BigQuery?
Cheers,
Jesse
I think this a very common analytical pattern in general!
Hey
Hi Jesse,
I have been trying to follow your instructions on using Data Action literally step by step, and Looker keeps telling me that “the form was not accepted”. What does the error mean and how can I get around this?
Please let me know! Solving this would be phenomenal for me…!
Thank you,
Hwi Moon
I’m curious if you can see anything in the Cloud Function logs indicating whether the action completed correctly. My code snippet doesn’t do error handling all that well 😛. My guess is that the function is failing and returning some exit condition that Looker doesn’t understand.
Take a look at the logs and let me know what you see – you can access them directly in the cloud function console.
Thanks for your reply! I have just try to re-run and see if I get anything in the log, but there are no new entries. I think I messed up somewhere in LookML(URL) or configuration/permissions. But I think I followed everything 100% in your instructions… and very confused!
The error message is as shown…
I think there is an error in the cloud function (Python 3.7). It seems to me that my “r=request.get_json()” does not have any keys or values in it, causing errors when I do anything like r[‘data’] or r[‘form_params’].
I could not figure out why - please help!
print(r)
below the r = ...
line, you’ll be able to observe what Looker’s sending over in the logs. For example, my function is parsing a dictionary that looks like this:
{'type': 'cell', 'scheduled_plan': None, 'attachment': None, 'data': {'value': 'Emma', 'rendered': 'Emma', 'name': 'Emma'}, 'form_params': {'annotation': 'Yes'}}
Give that a spin and let me know what you see. Also, go ahead and share the LookML for your action with the endpoint redacted.
It seems that the variable “r” is empty (i.e., {}). Very weird.
Here is my LookML code:
dimension: managers {
label: "Manager or BM"
type: string
sql: ${TABLE}.ManagerName ;;
action: {
label: "Test"
url: "https://us-west2-XXXXX.cloudfunctions.net/manager_mtd_input_test"
param: {
name: "manager"
value: "{{ value }}"
}
form_param: {
name: "performance"
type: string
label: "Enter Performance"
}
}
}
I just checked out the link and it does seem like the data is flowing through. At this point, I would think the request is being rejected by the Cloud Function (GCP), which is causing the request to be empty. Do you think it could be some permission related problem?
Here’s one possibility - Between the time the original article was written and now, GCP has changed some defaults for newly deployed Cloud Functions. You’ll want to make sure that you have opted-in to a publicly accessible function.
See here for more details: https://cloud.google.com/functions/docs/securing/managing-access-iam#allowing_unauthenticated_function_invocation
Hey Fabio, thank you so much for your reply! I should have continued to share my progress. I was able to get my Cloud Function working by checking the checkbox “Allow unauthenticated invocations” as shown below when first creating the function.
Very excited that it is working now - however, the word “unauthenticated” concerns me a little bit. It seems that anybody with the trigger URL is able to write to our data, is that correct? If so, how do people safely manage and protect their database while using this “write-back” workflow? Please let me know. Thank you very much!!
Enter your username or e-mail address. We'll send you an e-mail with instructions to reset your password.