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: articles/stream-analytics/stream-analytics-javascript-user-defined-functions.md
+17-9Lines changed: 17 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,42 +17,48 @@ ms.date: 03/23/2020
17
17
Azure Stream Analytics supports user-defined functions written in JavaScript. With the rich set of **String**, **RegExp**, **Math**, **Array**, and **Date** methods that JavaScript provides, complex data transformations with Stream Analytics jobs become easier to create.
18
18
19
19
## Overview
20
-
JavaScript user-defined functions support stateless, compute-only scalar functions that do not require external connectivity. The return value of a function can only be a scalar (single) value. After you add a JavaScript user-defined function to a job, you can use the function anywhere in the query, like a built-in scalar function.
20
+
21
+
JavaScript user-defined functions support stateless, compute-only scalar functions that don't require external connectivity. The return value of a function can only be a scalar (single) value. After you add a JavaScript user-defined function to a job, you can use the function anywhere in the query, like a built-in scalar function.
21
22
22
23
Here are some scenarios where you might find JavaScript user-defined functions useful:
23
24
* Parsing and manipulating strings that have regular expression functions, for example, **Regexp_Replace()** and **Regexp_Extract()**
24
25
* Decoding and encoding data, for example, binary-to-hex conversion
25
-
*Performing mathematic computations with JavaScript **Math** functions
26
-
*Performing array operations like sort, join, find, and fill
26
+
*Doing mathematic computations with JavaScript **Math** functions
27
+
*Doing array operations like sort, join, find, and fill
27
28
28
-
Here are some things that you cannot do with a JavaScript user-defined function in Stream Analytics:
29
-
* Call out external REST endpoints, for example, performing reverse IP lookup or pulling reference data from an external source
29
+
Here are some things that you can't do with a JavaScript user-defined function in Stream Analytics:
30
+
* Call out external REST endpoints, for example, doing reverse IP lookup or pulling reference data from an external source
30
31
* Perform custom event format serialization or deserialization on inputs/outputs
31
32
* Create custom aggregates
32
33
33
-
Although functions like **Date.GetDate()** or **Math.random()**are not blocked in the functions definition, you should avoid using them. These functions **do not** return the same result every time you call them, and the Azure Stream Analytics service does not keep a journal of function invocations and returned results. If a function returns different result on the same events, repeatability is not guaranteed when a job is restarted by you or by the Stream Analytics service.
34
+
Although functions like **Date.GetDate()** or **Math.random()**aren't blocked in the functions definition, you should avoid using them. These functions **don't** return the same result every time you call them, and the Azure Stream Analytics service doesn't keep a journal of function invocations and returned results. If a function returns different result on the same events, repeatability isn't guaranteed when a job is restarted by you or by the Stream Analytics service.
34
35
35
36
## Add a JavaScript user-defined function to your job
37
+
36
38
> [!NOTE]
37
39
> These steps work on the Stream Analytics jobs configured to run in the cloud. If your Stream Analytics job is configured to run on Azure IoT Edge, instead use Visual Studio and [write the user-defined function using C#](stream-analytics-edge-csharp-udf.md).
38
40
39
41
To create a JavaScript user-defined function in your Stream Analytics job, select **Functions** under **Job Topology**. Then, select **JavaScript UDF** from the **+Add** dropdown menu.
You must then provide the following properties and select **Save**.
46
+
43
47
|Property|Description|
44
48
|--------|-----------|
45
49
|Function alias|Enter a name to invoke the function in your query.|
46
50
|Output type|Type that will be returned by your JavaScript user-defined function to your Stream Analytics query.|
47
-
|Function defintion|Implementation of your Javascript function that will be executed each time your UDF gets invoked from your query.|
51
+
|Function definition|Implementation of your JavaScript function that will be executed each time your UDF gets invoked from your query.|
48
52
49
53
## Test and troubleshoot JavaScript UDFs
54
+
50
55
You can test and debug your JavaScript UDF logic in any browser. Debugging and testing the logic of these user-defined functions is currently not supported in the Stream Analytics portal. Once the function works as expected, you can add it to the Stream Analytics job as mentioned above and then invoke it directly from your query. You can test your query logic with JavaScript UDF using [Stream Analytics tools for Visual Studio](https://docs.microsoft.com/azure/stream-analytics/stream-analytics-tools-for-visual-studio-install).
51
56
52
57
JavaScript runtime errors are considered fatal, and are surfaced through the Activity log. To retrieve the log, in the Azure portal, go to your job and select **Activity log**.
53
58
54
59
## Call a JavaScript user-defined function in a query
55
-
You can easily invoke your JavaScript function in your query using the function alias prefixed with **udf.**. Here is an example of a JavaScript UDF which converts hexadecimal values to integer being invoked in a Stream Analytics query.
60
+
61
+
You can easily invoke your JavaScript function in your query using the function alias prefixed with **udf**. Here is an example of a JavaScript UDF that converts hexadecimal values to integer being invoked in a Stream Analytics query.
56
62
57
63
```SQL
58
64
SELECT
@@ -65,6 +71,7 @@ You can easily invoke your JavaScript function in your query using the function
65
71
```
66
72
67
73
## Supported JavaScript objects
74
+
68
75
Azure Stream Analytics JavaScript user-defined functions support standard, built-in JavaScript objects. For a list of these objects, see [Global Objects](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects).
69
76
70
77
### Stream Analytics and JavaScript type conversion
@@ -93,11 +100,12 @@ Array | Array
93
100
Null, Undefined | NULL
94
101
Any other type (for example, a function or error) | Not supported (results in runtime error)
95
102
96
-
JavaScript language is casesensitive and casing of the object fields in JavaScript code must match the casing of the fields in the incoming data. Please note that jobs with compatibility level 1.0 will convert fields from SQL SELECT statement to be lowercase. Under compatibility level 1.1 and higher, fields from SELECT statement will have the same casing as specified in the SQL query.
103
+
JavaScript language is case-sensitive and casing of the object fields in JavaScript code must match the casing of the fields in the incoming data. Jobs with compatibility level 1.0 will convert fields from SQL SELECT statement to be lowercase. Under compatibility level 1.1 and higher, fields from SELECT statement will have the same casing as specified in the SQL query.
97
104
98
105
## Other JavaScript user-defined function patterns
99
106
100
107
### Write nested JSON to output
108
+
101
109
If you have a follow-up processing step that uses a Stream Analytics job output as input, and it requires a JSON format, you can write a JSON string to output. The next example calls the **JSON.stringify()** function to pack all name/value pairs of the input, and then write them as a single string value in output.
0 commit comments