|
1 | | -# user-defined-functions |
| 1 | +# Create User Defined Functions in Couchbase with Javascript and Python |
| 2 | + |
2 | 3 | Learn to create User Defined Functions(UDF) in Couchbase using Javascript and Python |
| 4 | + |
| 5 | +> This repo is designed to teach you how to create UDFs using Javascript for the Query service and UDFs using Python for the Analytics service in Couchbase. |
| 6 | +
|
| 7 | +Full documentation can be found on the Couchbase Developer Portal. |
| 8 | + |
| 9 | +- [SQL++ UDFs using Javascript](https://developer.couchbase.com/tutorial-user-defined-functions-with-javascript) |
| 10 | +- [Analytics UDFs using Python](https://developer.couchbase.com/tutorial-analytics-user-defined-functions-with-python) |
| 11 | +<hr> |
| 12 | + |
| 13 | +## UDF Examples |
| 14 | + |
| 15 | +<hr> |
| 16 | +### Javascript UDF |
| 17 | + |
| 18 | +Couchbase allows you to create User Defined Functions (UDF) with Javascript to include custom business logic inside your SQL++ queries while querying the data stored in Couchbase. |
| 19 | + |
| 20 | +#### Importing the UDF in Couchbase |
| 21 | + |
| 22 | +- The Javascript [code](javascript-udf/distance.js) here can be imported directly using the `add function library` in the Couchbase Query Workbench under the UDF pane as a library. |
| 23 | +- The UDFs can be defined using the `add function` in the Couchbase Query Workbench under the UDF pane as a library. |
| 24 | +<hr> |
| 25 | + |
| 26 | +#### Testing the UDF in Couchbase |
| 27 | + |
| 28 | +> EXECUTE FUNCTION distance(51.5, 0, 38.8, -77.1) |
| 29 | +
|
| 30 | +If the UDF is defined properly, there will be an output like the one shown below: |
| 31 | + |
| 32 | +```sh |
| 33 | +[ |
| 34 | + 5918.185064088764 |
| 35 | +] |
| 36 | +``` |
| 37 | + |
| 38 | +### Analytics UDF using Python |
| 39 | + |
| 40 | +Couchbase Analytics supports creating custom User Defined Functions using Python. Here, we create a custom UDF that calculates the distance between two GPS coordinates using the [Geodesic distance](https://en.wikipedia.org/wiki/Geodesics_on_an_ellipsoid). |
| 41 | + |
| 42 | +#### Couchbase Setup |
| 43 | + |
| 44 | +- Couchbase can be run as a Docker container |
| 45 | + |
| 46 | + > docker run -d -p 8091-8094:8091-8094 -p 11210:11210 --name db couchbase |
| 47 | +
|
| 48 | +- Setting Up |
| 49 | + |
| 50 | + - Access Couchbase Web Console at http://localhost:8091/ |
| 51 | + |
| 52 | + - While setting up, enable Analytics, Data, Query and Indexing Service in Couchbase. |
| 53 | + |
| 54 | + - More information on setting up can be found [here](https://docs.couchbase.com/server/current/getting-started/do-a-quick-install.html). |
| 55 | + |
| 56 | +- Import Data using Web Console |
| 57 | + |
| 58 | + The `travel-sample` bucket needs to be imported. |
| 59 | + |
| 60 | + More information on importing data can be found [here](https://docs.couchbase.com/server/current/manage/manage-settings/install-sample-buckets.html). |
| 61 | + |
| 62 | +- Enabling Python UDFs on Couchbase by setting the Couchbase cluster into [Developer Preview Mode](https://docs.couchbase.com/server/current/developer-preview/preview-mode.html#how-do-i-enable-the-developer-preview-mode). |
| 63 | + |
| 64 | + > docker exec -it db bash /opt/couchbase/bin/couchbase-cli enable-developer-preview -c localhost:8091 -u <username> -p <password> --enable |
| 65 | +
|
| 66 | +#### Install requirements |
| 67 | + |
| 68 | +`$ pip install -r requirements.txt` |
| 69 | + |
| 70 | +#### Testing the UDF |
| 71 | + |
| 72 | +Run the test [code](analytics-udf/test_distance_library.py). |
| 73 | +`$ python test_distance_library.py` |
| 74 | + |
| 75 | +#### Importing the UDF into Couchbase |
| 76 | + |
| 77 | +- The Python module can be packaged including all the dependencies using shiv for any platform. We use Linux here for our container. |
| 78 | + |
| 79 | + > $ shiv -o distance-lib.pyz --site-packages . --platform manylinux2010_x86_64 --python-version 39 --only-binary=:all: geopy |
| 80 | +
|
| 81 | +- Copy the package into the Couchbase Container |
| 82 | + |
| 83 | + `$ docker cp distance-lib.pyz db:/tmp/` |
| 84 | + |
| 85 | +- Deploy the library into Couchbase Analytics Service |
| 86 | + |
| 87 | + > $ curl -X POST -u <username>:<password> -F "type=python" -F "data=@./tmp/distance-lib.pyz" localhost:8095/analytics/library/Default/distlib |
| 88 | +
|
| 89 | +- Create UDF in Couchbase Analytics Service |
| 90 | + |
| 91 | + > CREATE ANALYTICS FUNCTION distance_in_km(lat1, lon1, lat2, lon2) AS "distance_library", "Distance.calculate_geodesic_distance" AT distlib; |
| 92 | +
|
| 93 | +- Testing the UDF |
| 94 | + |
| 95 | + > SELECT distance_in_km(51.5, 0, 38.8, -77.1) as distance |
| 96 | +
|
| 97 | + If the UDF is defined properly, there will be an output like the one shown below: |
| 98 | + |
| 99 | + ```json |
| 100 | + [ |
| 101 | + { |
| 102 | + "distance": 5933.5299530300545 |
| 103 | + } |
| 104 | + ] |
| 105 | + ``` |
| 106 | + |
| 107 | +## Conclusion |
| 108 | + |
| 109 | +We walked through an example of how to create User Defined Functions (UDF) in Javascript and Python to calculate distances between two GPS coordinates, to import them into Couchbase and to use them in queries. |
0 commit comments