简体   繁体   English

如何借助 Cube.js 处理带有动态键(PorstgreSQL JSONB)的嵌套 hash?

[英]How to deal with nested hash with dynamic keys(PorstgreSQL JSONB) with the help of Cube.js?

I am quite a newbie to Cube.js.我是 Cube.js 的新手。 I have been trying to integrate Cube.js analytics functionality with my Ruby on Rails app.我一直在尝试将 Cube.js 分析功能与我的 Ruby on Rails 应用程序集成。 The database is PostgreSQL.数据库为 PostgreSQL。 In a database, there is a certain column called answers_json with jsonb data type which contains a nested hash.在数据库中,有一个名为answers_json的列,其数据类型为 jsonb,其中包含嵌套的 hash。 An example of data of that column is:该列的数据示例如下:

**answers_json:**
"question_weights_calc"=>
    {"314"=>{"329"=>1.5, "331"=>4.5, "332"=>1.5, "333"=>3.0},
     "315"=>{"334"=>1.5, "335"=>4.5, "336"=>1.5, "337"=>3.0},
     "316"=>{"338"=>1.5, "339"=>3.0}}

There are many more keys in the same column with the same hash structure as shown above.如上所示,同一列中还有更多具有相同 hash 结构的键。 I posted the specific part because I would be dealing with this part only.我发布了特定部分,因为我将只处理这部分。 I need assistance with accessing the values in the hash.我需要帮助来访问 hash 中的值。 The column has a nested hash.该列有一个嵌套的 hash。 In the example above, the keys "314", "315" and "316" are Category IDs.在上面的示例中,键“314”、“315”和“316”是类别 ID。 The keys associated with Category ID "314" are "329","331","332", "333";与类别 ID“314”关联的键是“329”、“331”、“332”、“333”; which are Question IDs.它们是问题 ID。 Each category will have multiple questions.每个类别将有多个问题。 For different records, the category and question IDs will be dynamic.对于不同的记录,类别和问题 ID 将是动态的。 For example, for another record, Category ID and Question IDs associated with that category id will be different.例如,对于另一条记录,与该类别 ID 关联的类别 ID 和问题 ID 将不同。 I need to access the values associated with the key question id.我需要访问与关键问题 ID 关联的值。 For example, to access the value "1.5" I need to do this in my schema file:例如,要访问值“1.5”,我需要在我的模式文件中执行此操作:

**sql: `(answers_json -> 'question_weights_calc' -> '314' ->> '329')`**

But the issue here is, those ids will be dynamic for different records in the database.但这里的问题是,对于数据库中的不同记录,这些 id 将是动态的。 Instead of "314" and "329", they can be some other numbers.代替“314”和“329”,它们可以是一些其他数字。 Adding different record's json here for clarification:在此处添加不同记录的 json 以进行澄清:

**answers_json:**
"question_weights_calc"=>{"129"=>{"273"=>6.0, "275"=>15.0, "277"=>8.0}, "252"=>{"279"=>3.0, "281"=>8.0, "283"=>3.0}}}

How can I know and access those dynamic IDs and their values since I also need to perform mathematical operations on values.我如何知道和访问这些动态 ID 及其值,因为我还需要对值执行数学运算。 Thanks!谢谢!

As a general rule, it's difficult to run SQL-based reporting on highly dynamic JSON data.作为一般规则,很难对高度动态的 JSON 数据运行基于 SQL 的报告。 Postgres does have some useful functions for dealing with JSON , and you might be able to use json_each or json_object_keys plus a few joins to get there, but its quite likely that the performance and maintainability of such a query would be difficult to say the least Cube.js ultimately executes SQL queries, so if you do go the above route, the query should be easily transferrable to a Cube.js schema. Postgres 确实有一些有用的函数来处理 JSON ,你也许可以使用json_eachjson_object_keys加上一些连接来到达那里,但这种查询的性能和可维护性很可能至少很难说 Cube .js 最终会执行 SQL 查询,因此如果您执行 go 上述路线,查询应该很容易转移到 Cube.js 模式。

Another approach would be to create a separate data processing pipeline that collects all the JSON data and flattens it into a single table.另一种方法是创建一个单独的数据处理管道,该管道收集所有 JSON 数据并将其扁平化为一个表。 The pipeline should then store this data back in your database of choice, from where you could then use Cube.js to query it.然后,管道应将此数据存储回您选择的数据库中,然后您可以在其中使用 Cube.js 对其进行查询。

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

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