Skip to content

Commit 77d95b1

Browse files
authored
Merge pull request #108726 from sidramadoss/patch-65
Update stream-analytics-javascript-user-defined-functions.md
2 parents 6c9ddd4 + a6bef85 commit 77d95b1

File tree

6 files changed

+36
-70
lines changed

6 files changed

+36
-70
lines changed

articles/stream-analytics/TOC.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@
2323
href: stream-analytics-manage-job.md
2424
- name: 2 - Run Azure Functions
2525
href: stream-analytics-with-azure-functions.md
26-
- name: 3 - Run a JavaScript UDF
27-
href: stream-analytics-javascript-user-defined-functions.md
28-
- name: 4 - Deploy with CI/CD in Azure Pipelines
26+
- name: 3 - Deploy with CI/CD in Azure Pipelines
2927
href: stream-analytics-tools-visual-studio-cicd-vsts.md
30-
- name: 5 - Run a C# UDF
28+
- name: 4 - Run a C# UDF
3129
href: stream-analytics-edge-csharp-udf.md
32-
- name: 6 - Create custom .NET deserializer
30+
- name: 5 - Create custom .NET deserializer
3331
href: custom-deserializer.md
3432
- name: Samples
3533
items:
@@ -67,6 +65,8 @@
6765
href: machine-learning-udf.md
6866
- name: C# UDF
6967
href: stream-analytics-edge-csharp-udf-methods.md
68+
- name: JavaScript UDF
69+
href: stream-analytics-javascript-user-defined-functions.md
7070
- name: Optimize your Stream Analytics job
7171
items:
7272
- name: Understand and adjust Streaming Units
50 KB
Loading
180 KB
Loading

articles/stream-analytics/stream-analytics-introduction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Each job has an output for the transformed data, and you can control what happen
3535

3636
The following image shows how data is sent to Stream Analytics, analyzed, and sent for other actions like storage or presentation:
3737

38-
![Stream Analytics intro pipeline](./media/stream-analytics-introduction/stream-analytics-intro-pipeline.png)
38+
![Stream Analytics intro pipeline](./media/stream-analytics-introduction/stream-analytics-e2e-pipeline.png)
3939

4040
## Key capabilities and benefits
4141

Lines changed: 30 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,77 @@
11
---
22
title: Azure Stream Analytics JavaScript user-defined functions
3-
description: In this tutorial, you perform advanced query mechanics with JavaScript user-defined functions
3+
description: This article is an introduction to JavaScript user-defined functions in Stream Analytics.
44
author: rodrigoaatmicrosoft
55
ms.author: rodrigoa
66
ms.service: stream-analytics
77
ms.topic: tutorial
88
ms.reviewer: mamccrea
99
ms.custom: mvc
10-
ms.date: 04/01/2018
10+
ms.date: 03/23/2020
1111

1212
#Customer intent: "As an IT admin/developer I want to run JavaScript user-defined functions within Stream Analytics jobs."
1313
---
1414

15-
# Tutorial: Azure Stream Analytics JavaScript user-defined functions
15+
# JavaScript user-defined functions in Azure Stream Analytics
1616

1717
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.
1818

19-
In this tutorial, you learn how to:
19+
## Overview
2020

21-
> [!div class="checklist"]
22-
> * Define a JavaScript user-defined function
23-
> * Add the function to the portal
24-
> * Define a query that runs the function
25-
26-
If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin.
27-
28-
## JavaScript user-defined functions
29-
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.
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.
3022

3123
Here are some scenarios where you might find JavaScript user-defined functions useful:
3224
* Parsing and manipulating strings that have regular expression functions, for example, **Regexp_Replace()** and **Regexp_Extract()**
3325
* Decoding and encoding data, for example, binary-to-hex conversion
34-
* Performing mathematic computations with JavaScript **Math** functions
35-
* 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
3628

37-
Here are some things that you cannot do with a JavaScript user-defined function in Stream Analytics:
38-
* 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
3931
* Perform custom event format serialization or deserialization on inputs/outputs
4032
* Create custom aggregates
4133

42-
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.
4335

44-
## Add a JavaScript user-defined function in the Azure portal
45-
To create a simple JavaScript user-defined function under an existing Stream Analytics job, follow these steps:
36+
## Add a JavaScript user-defined function to your job
4637

4738
> [!NOTE]
4839
> 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).
4940
50-
1. In the Azure portal, find your Stream Analytics job.
51-
52-
2. Under the **Job topology** heading, select **Functions**. An empty list of functions appears.
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.
5342

54-
3. To create a new user-defined function, select **+ Add**.
43+
![Add JavaScript UDF](./media/javascript/stream-analytics-jsudf-add.png)
5544

56-
4. On the **New Function** blade, for **Function Type**, select **JavaScript**. A default function template appears in the editor.
45+
You must then provide the following properties and select **Save**.
5746

58-
5. For the **UDF alias**, enter **hex2Int**, and change the function implementation as follows:
47+
|Property|Description|
48+
|--------|-----------|
49+
|Function alias|Enter a name to invoke the function in your query.|
50+
|Output type|Type that will be returned by your JavaScript user-defined function to your Stream Analytics query.|
51+
|Function definition|Implementation of your JavaScript function that will be executed each time your UDF gets invoked from your query.|
5952

60-
```javascript
61-
// Convert Hex value to integer.
62-
function hex2Int(hexValue) {
63-
return parseInt(hexValue, 16);
64-
}
65-
```
53+
## Test and troubleshoot JavaScript UDFs
6654

67-
6. Select **Save**. Your function appears in the list of functions.
68-
7. Select the new **hex2Int** function, and check the function definition. All functions have a **UDF** prefix added to the function alias. You need to *include the prefix* when you call the function in your Stream Analytics query. In this case, you call **UDF.hex2Int**.
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).
6956

70-
## Testing JavaScript UDFs
71-
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.
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**.
7258

7359
## Call a JavaScript user-defined function in a query
7460

75-
1. In the query editor, under the **Job topology** heading, select **Query**.
76-
2. Edit your query, and then call the user-defined function, like this:
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.
7762

78-
```SQL
63+
```SQL
7964
SELECT
8065
time,
8166
UDF.hex2Int(offset) AS IntOffset
8267
INTO
8368
output
8469
FROM
8570
InputStream
86-
```
87-
88-
3. To upload the sample data file, right-click the job input.
89-
4. To test your query, select **Test**.
90-
71+
```
9172

9273
## Supported JavaScript objects
74+
9375
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).
9476

9577
### Stream Analytics and JavaScript type conversion
@@ -106,10 +88,8 @@ Record | Object
10688
Array | Array
10789
NULL | Null
10890

109-
11091
Here are JavaScript-to-Stream Analytics conversions:
11192

112-
11393
JavaScript | Stream Analytics
11494
--- | ---
11595
Number | Bigint (if the number is round and between long.MinValue and long.MaxValue; otherwise, it's double)
@@ -120,14 +100,12 @@ Array | Array
120100
Null, Undefined | NULL
121101
Any other type (for example, a function or error) | Not supported (results in runtime error)
122102

123-
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. 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.
124-
125-
## Troubleshooting
126-
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**.
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.
127104

128105
## Other JavaScript user-defined function patterns
129106

130107
### Write nested JSON to output
108+
131109
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.
132110

133111
**JavaScript user-defined function definition:**
@@ -151,19 +129,7 @@ FROM
151129
input PARTITION BY PARTITIONID
152130
```
153131

154-
## Clean up resources
155-
156-
When no longer needed, delete the resource group, the streaming job, and all related resources. Deleting the job avoids billing the streaming units consumed by the job. If you're planning to use the job in future, you can stop it and re-start it later when you need. If you are not going to continue to use this job, delete all resources created by this quickstart by using the following steps:
157-
158-
1. From the left-hand menu in the Azure portal, click **Resource groups** and then click the name of the resource you created.
159-
2. On your resource group page, click **Delete**, type the name of the resource to delete in the text box, and then click **Delete**.
160-
161-
## Get help
162-
For additional help, try our [Azure Stream Analytics forum](https://social.msdn.microsoft.com/Forums/azure/home?forum=AzureStreamAnalytics).
163-
164132
## Next steps
165133

166-
In this tutorial, you have created a Stream Analytics job that runs a simple JavaScript user-defined function. To learn more about Stream Analytics, continue to the real-time scenario articles:
167-
168-
> [!div class="nextstepaction"]
169-
> [Real-time Twitter sentiment analysis in Azure Stream Analytics](stream-analytics-twitter-sentiment-analysis-trends.md)
134+
* [Machine Learning UDF](https://docs.microsoft.com/azure/stream-analytics/machine-learning-udf)
135+
* [C# UDF](https://docs.microsoft.com/azure/stream-analytics/stream-analytics-edge-csharp-udf-methods)

0 commit comments

Comments
 (0)