【问题标题】:Parsing an Array passed as an Object to a C# UDF for Azure Stream Analytics解析作为对象传递给 C# UDF 的数组以用于 Azure 流分析
【发布时间】:2020-05-05 18:32:51
【问题描述】:

这是相关的 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;
    }
   }
  }

【问题讨论】:

  • 什么是对象 b?它是一个json对象吗?你知道物体的结构吗?如果是,您可以反序列化该对象。这是微软 dacs 的链接:docs.microsoft.com/en-us/dotnet/standard/serialization/…
  • 我正在从 json 文件中传递值。
  • 我不熟悉 azure 流分析。如果您可以使用 microsoft 引用,则可以使用 using System.Web.Script.Serialization;using System.Runtime.Serialization; 如果您不能使用这些引用之一,您必须自己通过将其从字符串转换来完成。如果将其转换为字符串,您会得到什么? var a = b as string;
  • 我们接近尾声了。 :-) 试试这个string myObjectString = b.ToString(); List&lt;int&gt; a = myObjectString.Replace("[","").Replace("]","").Split(',').Where(m =&gt; int.TryParse(m.Trim(), out int n)).Select(x =&gt; int.Parse(x.Trim())).ToList();
  • 如果它不是字符串而是对象数组,那么试试这个var myObjArray = b as object[]; List&lt;int&gt; a = myObjArray.Where(m =&gt; int.TryParse(m.ToString().Trim(), out int n)).Select(x =&gt; int.Parse(x.ToString().Trim())).ToList();

标签: c# json azure-stream-analytics


【解决方案1】:

鉴于您知道您的输入是一个数组,您不需要将其强制转换为 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];
        }
    }
}

【讨论】:

  • Exception 'Specified cast is not valid.'.","Source":{"Name":"","Type":null},"Type":115}
猜你喜欢
  • 1970-01-01
  • 2021-05-23
  • 1970-01-01
  • 1970-01-01
  • 2016-10-27
  • 2021-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多