如何“合并"或“转换" Azure Cosmos DB中的JSON文档

编程入门 行业动态 更新时间:2024-10-19 02:18:50
本文介绍了如何“合并"或“转换" Azure Cosmos DB中的JSON文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用Microsoft Bot Framework和Azure设置聊天机器人.我想将"UserState"保存在数据库中,以便轻松分析用户数据.我设法将我的userState以JSON文档的形式保存在Azure Cosmos DB中.

问题是与机器人的每次交互都在Cosmos DB的集合"中创建了一个新的文档".

我如何轻松地合并数据(数据结构是一致的),并且在最佳情况下将数据存储在某种表中?我要用于分析的工具需要.txt或.csv文件.

这是存储用户数据的JSON文件的代码段.

{ "id": "emulator*2fusers*2f9321b527-4699-4b4a-8d9d-9cd9fa8f1967*2f", "realId": "emulator/users/9321b527-4699-4b4a-8d9d-9cd9fa8f1967/", "document": { "userData": { "name": "value", "age": 18, "gender": "value", "education": "value", "major": "value" }, "userDataExtended": { "roundCounter": 3, "choices": [ "A", "A", "B" ], }, "_rid": "0k5YAPBrVaknAAAAAAAAAA==", "_self": "dbs/0k5YAA==/colls/0k5YAPBrVak=/docs/0k5YAPBrVaknAAAAAAAAAA==/", "_etag": "\"ac009377-0000-0000-0000-5c59c5610000\"", "_attachments": "attachments/", "_ts": 1549387105 }

在最佳情况下,我想将数据包含在具有名称",年龄"等列以及每个用户(文档)为一行的表结构中.

谢谢!

解决方案

您的问题中有几件事,我将分别解决.

扩展Drew的评论:

您正在创建多个文档,因为您正在通过模拟器运行机器人.模拟器每次重新启动时,都会创建一个新的用户ID,从而为该用户创建一个新文档,并为该用户的对话创建一个新文档.如果您使用仿真器以外的其他渠道,那么只要用户ID保持一致,就不会出现此问题.

关于合并文档:

我不确定您要查找的是什么,但是您可能可以使用 SQL查询以完成所需的操作.只需单击新建SQL查询".例如,运行SELECT * FROM c会将所有文档合并到一个输出中.

关于文本/csv文件:

我不确定您的工具是什么,但是如果它可以处理JSON,则上面的方法可能对您有用.如果没有,则可以实现自定义中间件来获取所需的txt/csv输出. 这里是一个示例,显示了相对相似的内容. C#中没有等效的示例,但是您仍然可以实现您自己的中间件以完成相同的操作.

关于表格:

如果您真的正在寻找表存储,请在V3机器人中受支持,但在V4中被blob存储所取代.您可以在V3中编写您的机器人.与Jay所说的类似,您也许仍然可以使用触发函数将其发送到表存储,但是随后您将数据存储了两次.

关于分析

如果您真正想要的只是分析,请应用洞察/Bot分析可能是您所需要的,尽管我不相信它将提供您想要的详细信息.

I'm setting up a Chatbot with the Microsoft Bot Framework and Azure. I want to save my "UserState" in a database in order to easily analyze the user data. I managed to save my userState in form of JSON documents in Azure Cosmos DB.

The problem is that each interaction with the bot creates a new "document" in a "collection" in Cosmos DB.

How can I easily merge the data (data structure is consistent) and in the best case have the data in some kind of table? The tool I want to use for analyzing requires .txt or .csv files.

This is a snippet of the JSON file which stores the user data.

{ "id": "emulator*2fusers*2f9321b527-4699-4b4a-8d9d-9cd9fa8f1967*2f", "realId": "emulator/users/9321b527-4699-4b4a-8d9d-9cd9fa8f1967/", "document": { "userData": { "name": "value", "age": 18, "gender": "value", "education": "value", "major": "value" }, "userDataExtended": { "roundCounter": 3, "choices": [ "A", "A", "B" ], }, "_rid": "0k5YAPBrVaknAAAAAAAAAA==", "_self": "dbs/0k5YAA==/colls/0k5YAPBrVak=/docs/0k5YAPBrVaknAAAAAAAAAA==/", "_etag": "\"ac009377-0000-0000-0000-5c59c5610000\"", "_attachments": "attachments/", "_ts": 1549387105 }

In the best case I want to have the data in a table structure with columns "name", "age", etc. and each user (document) as a row.

Thank you!

解决方案

There's a few things in your questions and I'll address them all separately.

Expanding on Drew's comment:

You have multiple documents being created because you're running the bot through emulator. Each time emulator restarts, it creates a new User ID and therefore a new document for the user and also one for that user's conversation. You will not have this issue if you use a channel other than emulator, provided that the User ID remains consistent.

Regarding merging documents:

I'm not sure exactly what you're looking for, but you might be able to use SQL Queries to accomplish what you need. Just click "New SQL Query". For example, running SELECT * FROM c merges all of the documents into a single output.

Regarding text/csv files:

I'm not sure what your tool is, but if it can handle JSON, then the above might work for you. If not, you can implement custom middleware to get the txt/csv output you're looking for. Here's a sample that shows something relatively similar. There isn't an equivalent example in C#, but you can still implement your own middleware to do the same thing.

Regarding Tables:

If you're really looking for Table Storage, it was supported in V3 bots, but replaced by blob storage in V4. You could write your bot in V3. Similar to what Jay said, you might still be able to use a trigger function to send it to table storage, but then you're storing the data twice.

Regarding Analysis

If all you're really looking for is analysis, Application Insights/Bot Analytics may be what you need, although I don't believe it will provide the detail you're looking for.

更多推荐

如何“合并"或“转换" Azure Cosmos DB中的JSON文档

本文发布于:2023-11-17 02:27:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608476.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文档   Azure   quot   Cosmos   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!