0

Creating Azure Stream Analytics job using the python SDK gives an error azure.core.exceptions.HttpResponseError: (BadRequest). The JSON provided in the request body is invalid. The required property 'datasource type' is missing from the request.

Here is the API call I am using to create the ASA job:

response = client.streaming_jobs.begin_create_or_replace(resource_group_name, job_name123, streaming_job={
        "location": "East US", 
        "properties": { 
        "sku": {  
            "name": "standard" 
        },
        "eventsLateArrivalMaxDelayInSeconds": 1, 
        "jobType": "edge", 
        "inputs": [
            {
                "name": "input",
                "properties": {
                    "type": "stream",
                        "serialization": {
                        "type": "JSON",
                        "properties": {
                            "fieldDelimiter": ",",
                            "encoding": "UTF8"
                        }
                    },
                    "datasource": {
                        "type": "GatewayMessageBus",
                        "properties": {
                        }
                    }
                }
            }
        ],
        "transformation": {
            "name": "samplequery",
            "properties": {
                "query": "select * from input"
            }
        },
    "package": {
        "storageAccount" : {
            "accountName": "*******",
            "accountKey": "*******"
        },
        "container": "sample"
    },
        "outputs": [
            {
                "name": "output",
                "properties": {
                    "serialization": {
                        "type": "JSON",
                        "properties": {
                            "fieldDelimiter": ",",
                            "encoding": "UTF8"
                        }
                    },
                    "datasource": {
                        "type": "GatewayMessageBus",
                        "properties": {
                        }
                    }
                }
            }
        ]
    }
})
toyota Supra
  • 3,181
  • 4
  • 15
  • 19

1 Answers1

0

Creating an Azure Stream Analytics job using the Python SDK gives an error

The error shows that your code has not correctly added data source properties.

You can use the below code to create a stream analytics job using azure python SDK.

Code:

from azure.identity import DefaultAzureCredential
from azure.mgmt.streamanalytics import StreamAnalyticsManagementClient

# Replace with your Azure subscription ID and resource group name
subscription_id = "xxxxx"
resource_group_name = "xxxxxx"

# Replace with your specific Stream Analytics job name
job_name = "stream326"

# Set up credentials and Stream Analytics client
credential = DefaultAzureCredential()
client = StreamAnalyticsManagementClient(credential, subscription_id)

response = client.streaming_jobs.begin_create_or_replace(resource_group_name, job_name, streaming_job={
    "location": "East US", 
    "properties": {
    "sku": {
      "name": "Standard"
    },
    "eventsOutOfOrderPolicy": "Drop",
    "outputErrorPolicy": "Drop",
    "eventsOutOfOrderMaxDelayInSeconds": 0,
    "eventsLateArrivalMaxDelayInSeconds": 5,
    "dataLocale": "en-US",
    "compatibilityLevel": "1.0",
    "inputs": [
      {
        "properties": {
          "type": "Stream",
          "datasource": {
            "type": "Microsoft.Devices/IotHubs",
            "properties": {
                "iotHubNamespace": "iothub-name",
                "sharedAccessPolicyName": "iothubowner",
                "sharedAccessPolicyKey": "key",
                "endpoint": "messages/events",
                "consumerGroupName": "$Default"
                }
        },
          "serialization": {
            "type": "Json",
            "properties": {
              "encoding": "UTF8"
            }
          }
        },
        "name": "inputtest"
      }
    ],
    "transformation": {
      "properties": {
        "streamingUnits": 1,
        "query": "SELECT *INTO outputtest FROM inputtest WHERE Temperature > 27"
      },
      "name": "transformationtest"
    },
    "outputs": [
      {
        "properties": {
            "datasource": {
            "type": "Microsoft.Storage/Blob",
            "properties": {
                "storageAccounts": [
                    {
                      "accountName": "your-storage-account-name",
                      "accountKey": "your-account-key"
                    }
                ],
                "container": "test",
                "pathPattern": "",
                "dateFormat": "yyyy/MM/dd",
                "timeFormat": "HH"
            }
            },
            "serialization": {
            "type": "Json",
            "properties": {
              "encoding": "UTF8"
            }
          }
        },
        "name": "outputtest"
      }
    ],
    "functions": []
  },
  "tags": {
    "key1": "value1",
    "randomKey": "randomValue",
    "key3": "value3"
  }
})
print(response.status())

Output:

Succeeded

enter image description here

Portal:

enter image description here

When I start my task it reflected the data in my storage account.

Portal:

enter image description here

Reference:

Quickstart - Create a Stream Analytics job by using the Azure portal - Azure Stream Analytics | Microsoft Learn

Venkatesan
  • 3,748
  • 1
  • 3
  • 15
  • I need to create ASA job with Edge Hub as input and output and not IOT Hub. – Rajiv Nekkanti Aug 21 '23 at 17:27
  • @Rajiv Nekkanti Refer to this document it will create a stream analytics job with edge hub with portal https://learn.microsoft.com/en-us/azure/iot-edge/tutorial-deploy-stream-analytics?view=iotedge-1.4&viewFallbackFrom=iotedge-2020-11 AFAIK, we can't able to create it edge hub using python SDK. – Venkatesan Aug 22 '23 at 05:35