解析作为对象传递给C#UDF的数组以进行Azure流分析

问题描述 投票:0回答:1

这里是相关的JSON文件的一部分。我正在尝试将值传递给用户定义的脚本进行处理

{
"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
],

流分析查询看起来像这样

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 )

这是用户定义的功能。 我真的不知道如何将对象b中的值解析为整数列表或数组以进行处理。

 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;
    }
   }
  }
c# json azure-stream-analytics
1个回答
0
投票

鉴于您知道您的输入是一个数组,因此无需将其强制转换为Array类型。您可以改用以下方法。您查询的可能是:

SELECT udf.sampleASAproject_Class1_sampleudf([Values]) from Input

还有我的示例C#UDF(在我的例子中为Codebehind)返回将数组转换为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];
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.