简体   繁体   English

解析作为 Object 传递到 C# UDF 的数组,用于 Azure ZEAE8326229233337

[英]Parsing an Array passed as an Object to a C# UDF for Azure Stream Analytics

Here is a part of the JSON file that's relevant.这是相关的 JSON 文件的一部分。 I am trying the pass Values to a user defined script for processing我正在尝试将值传递给用户定义的脚本进行处理

{
"FragCount": 63,
"ValueMapping": 7,
"DataType": 19,
"BurstId": 85,
"SensorNodeId": "bd8e8077",
"Values": [
  23,
  -3,
  20,
  31,
  51,
  -3,
  -14,
  -4,
  47,
  31,
  52,
  -3,
  2,
  -3,
  42,
  31,
  49,
  -3,
  -18,
  -4,
  -10,
  30,
  47,
  -3,
  -29,
  -4,
  55,
  31,
  27,
  -3,
  -24,
  -4,
  11,
  31,
  -32,
  -4,
  -38,
  -4,
  -18,
  30,
  -20,
  -4,
  -76,
  -4,
  -42,
  30,
  -59,
  -4,
  -81,
  -4,
  45,
  31,
  -79,
  -4,
  -75,
  -4,
  19,
  31,
  -93,
  -4,
  -99,
  -4,
  -40,
  30,
  -122,
  -4,
  -90,
  -4,
  -70,
  30,
  -128,
  -4,
  -92,
  -4,
  -112,
  30,
  119,
  -4,
  -91,
  -4,
  -46,
  30,
  120,
  -4,
  -49,
  -4,
  -61,
  30,
  87,
  -4,
  -43,
  -4,
  -27,
  30,
  61,
  -4
],

The Stream Analytics Query looks like this Stream 分析查询如下所示

WITH ReaderQuery AS (
SELECT
    *
FROM
    [IoT-Hub]
)


SELECT
       SensorNodeId AS SensorID,
       FragCount AS FragCount,
       ValueMapping AS ValueMapping,
       DataType AS DataType,
       BurstId AS BurstId,
       udf.FFTDecompressor_Class1_FFTDecomFunc(try_cast([Values] as Array)) AS [Values],
       MeasurementId AS MeasurementId

  INTO
       [FFT]
  FROM
       ReaderQuery
       WHERE ( DataType = 19 )

This is the user defined function.这是用户定义的 function。 I really can't figure out how I could parse Values in Object b into an integer list or array for processing.我真的不知道如何将 Object b 中的值解析为 integer 列表或数组进行处理。

 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using System.Text;

   namespace FFTDecompressor
   {
    public class Class1
     {


    // Public static function
    public static String FFTDecomFunc(Object b)
    {
        //int[] a = Array.ConvertAll<object, int>(b.ToArray(), (o) => (int)o);
        //List<int> a = b.Split(',').Select(s => int.Parse(s)).ToList();
        List<int> FFTList = new List<int>();
        String DecompressedValues = null;

        for (int i = 0; i < a.Count; i++)
        {

            if (i == 0)
            {
                FFTList.Add(BitConverter.ToUInt16(new[] { Convert.ToByte(a[i]), Convert.ToByte(a[i +1]), }, 0));
                FFTList.ForEach(x => Console.WriteLine(x));
                i++;
            }
            else
            {
                if (a[i] == -128)
                {
                    FFTList.Add(BitConverter.ToUInt16(new[] { Convert.ToByte(a[i + 1]), 
                    Convert.ToByte(a[i + 2]), }, 0));
                    i += 2;
                }
                else
                {

                    if (FFTList.Count == 1)
                    {
                        FFTList.Add(FFTList[0] + a[i]);
                    }
                    else
                    {
                        FFTList.Add(FFTList[(FFTList.Count - 1)] + a[i]);
                    }
                }

            }

        }

        /*
        for (int i = 0; i < FFTList.Count; ++i)
        {
            FFTList[i] /= 100;
        }
        */
        DecompressedValues = string.Join(",", FFTList);  


        return DecompressedValues;
    }
   }
  }

Given you know that your input is an array, you don't need to cast it to type Array.鉴于您知道您的输入是一个数组,您不需要将其转换为 Array 类型。 Here is what you can do instead.这是您可以做的。 You query could be:您的查询可能是:

SELECT udf.sampleASAproject_Class1_sampleudf([Values]) from Input

And my example C# UDF (Codebehind in my case) returns converts the array to Int64 type and returns the first value:我的示例 C# UDF(在我的例子中为代码隐藏)返回将数组转换为 Int64 类型并返回第一个值:

using System;
using System.Linq;

namespace sampleASAproject
{
    public class Class1
    {
        // Public static function
        public static Int64 sampleudf(Object[] data)
        {
            Int64[] arrayInteger = data.Cast<Int64>().ToArray();
            return arrayInteger[0];
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM