You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+20-12Lines changed: 20 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,12 +11,14 @@ Full documentation can be found on the Couchbase Developer Portal.
11
11
<hr>
12
12
13
13
## UDF Examples
14
+
14
15
We have two different types of UDFs in Couchbase, using Javascript for the Query service and using Python for the Analytics service. Here, we showcase the same UDF functionality of calculating the distance between two GPS coordinates for the two services.
16
+
15
17
<hr>
16
18
17
19
### Query Service UDF using Javascript
18
20
19
-
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. Here, we create a custom UDF that calculates the distance between two GPS coordinates using the [Haversine formula](https://en.wikipedia.org/wiki/Haversine_formula).
21
+
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. Here, we create a custom UDF that calculates the distance between two GPS coordinates using the [Haversine formula](https://en.wikipedia.org/wiki/Haversine_formula).
20
22
21
23
#### Importing the UDF in Couchbase
22
24
@@ -27,14 +29,15 @@ Couchbase allows you to create User Defined Functions (UDF) with Javascript to i
27
29
28
30
-`EXECUTE FUNCTION distance(51.5, 0, 38.8, -77.1)`
29
31
30
-
-If the UDF is defined properly, there will be an output like the one shown below:
32
+
- If the UDF is defined properly, there will be an output like the one shown below:
31
33
32
-
```sh
33
-
[
34
-
5918.185064088764
35
-
]
36
-
```
37
-
<hr>
34
+
```sh
35
+
[
36
+
5918.185064088764
37
+
]
38
+
```
39
+
40
+
<hr>
38
41
39
42
### Analytics Service UDF using Python
40
43
@@ -44,7 +47,7 @@ Couchbase Analytics supports creating custom User Defined Functions using Python
44
47
45
48
- Couchbase can be run as a Docker container
46
49
47
-
> docker run -d -p 8091-8094:8091-8094 -p 11210:11210 --name db couchbase
50
+
> docker run -d -p 8091-8096:8091-8096 -p 11210:11210 --name db couchbase
48
51
49
52
- Setting Up
50
53
@@ -67,21 +70,26 @@ Couchbase Analytics supports creating custom User Defined Functions using Python
67
70
#### Install requirements
68
71
69
72
-`$ pip install -r requirements.txt`
70
-
73
+
71
74
### UDF Code
75
+
72
76
- The UDF code is [here](analytics-udf-python/distance-lib/distance_library.py).
73
77
74
78
#### Testing the UDF
75
79
76
80
- Run the test [code](analytics-udf-python/distance-lib/test_distance_library.py).
77
-
81
+
78
82
`$ python test_distance_library.py`
79
83
80
84
#### Importing the UDF into Couchbase
81
85
82
86
- The Python module can be packaged including all the dependencies using shiv for any platform. We use Linux here for our container.
0 commit comments