ip2country¶
ip2country(ip)
Description¶
Get country_code
of ip
This functions uses IP address data powered by IPinfo and released under Creative Commons Attribution-ShareAlike 4.0 International License. You are required to attribute IPinfo to use their free datasets. The attribution requirements can be met by giving their service credit as your data source. Simply place a link to IPinfo on the website, application, or social media account that uses their data.
Usage¶
Call or Deploy ip2country
?
Call ip2country
directly
The easiest way to use bigfunctions
ip2country
function is deployed in 39 public datasets for all of the 39 BigQuery regions.- It can be called by anyone. Just copy / paste examples below in your BigQuery console. It just works!
- (You need to use the dataset in the same region as your datasets otherwise you may have a function not found error)
Public BigFunctions Datasets
Region | Dataset |
---|---|
eu |
bigfunctions.eu |
us |
bigfunctions.us |
europe-west1 |
bigfunctions.europe_west1 |
asia-east1 |
bigfunctions.asia_east1 |
... | ... |
Deploy ip2country
in your project
Why deploy?
- You may prefer to deploy
ip2country
in your own project to build and manage your own catalog of functions. - This is particularly useful if you want to create private functions (for example calling your internal APIs).
- Get started by reading the framework page
Deployment
ip2country
function can be deployed with:
pip install bigfunctions
bigfun get ip2country
bigfun deploy ip2country
Requirements
ip2country
uses the following secrets. Get them by reading the documentation link and store them in Google Secret Manager in the project where you deploy the function (and give Accessor role to the service account of the function):
name | description | documentation to get the secret |
---|---|---|
ipinfo_token |
IP Info Token | doc |
Examples¶
select bigfunctions.eu.ip2country("152.216.7.110")
select bigfunctions.us.ip2country("152.216.7.110")
select bigfunctions.europe_west1.ip2country("152.216.7.110")
+--------------+
| country_code |
+--------------+
| US |
+--------------+
Use cases¶
You have a table of website access logs, and each record includes the IP address of the visitor. You want to analyze website traffic by country. The ip2country
function can be used to determine the country code associated with each IP address, allowing you to aggregate and analyze traffic patterns based on visitor location.
SELECT
bigfunctions.us.ip2country(client_ip) AS country_code,
COUNT(*) AS visit_count
FROM
`your_project.your_dataset.website_logs`
GROUP BY
country_code
ORDER BY
visit_count DESC;
This query uses the ip2country
function to add a country_code
column to the website_logs
table. Then, it groups the data by country_code
and counts the number of visits from each country, providing a summary of website traffic by country of origin. Remember to replace your_project.your_dataset.website_logs
with the actual path to your website logs table and choose the correct BigFunctions dataset according to your region (e.g., bigfunctions.eu
, bigfunctions.asia_northeast1
).
Need help or Found a bug?
Get help using ip2country
The community can help! Engage the conversation on Slack
We also provide professional suppport.
Report a bug about ip2country
If the function does not work as expected, please
- report a bug so that it can be improved.
- or open the discussion with the community on Slack.
We also provide professional suppport.