小编Tob*_*son的帖子

使用 ruamel.yaml,如何使带有 NEWLINE 的变量成为不带引号的多行

我正在生成用作协议的 YAML,其中包含一些生成的 JSON。

import json
from ruamel import yaml
jsonsample = { "id": "123", "type": "customer-account", "other": "..." }
myyamel = {}
myyamel['sample'] = {}
myyamel['sample']['description'] = "This example shows the structure of the message"
myyamel['sample']['content'] = json.dumps( jsonsample, indent=4, separators=(',', ': '))
print yaml.round_trip_dump(myyamel, default_style = None, default_flow_style=False, indent=2, block_seq_indent=2, line_break=0, explicit_start=True, version=(1,1))
Run Code Online (Sandbox Code Playgroud)

然后我得到这个输出

%YAML 1.1
---
sample:
  content: "{\n    \"other\": \"...\",\n    \"type\": \"customer-account\",\n    \"\
  id\": \"123\"\n}"
description: This example shows the structure of the message
Run Code Online (Sandbox Code Playgroud)

现在对我来说,如果我能够使多行行从管道开始格式化,看起来会好得多|

我想看到的输出是这样的 …

python json ruamel.yaml

5
推荐指数
1
解决办法
2860
查看次数

创建KSQL流:如何从复杂的json中提取值

我正在尝试在Apache/KAFKA KSQL中创建一个流主题包含(有点复杂的JSON)

{
  "agreement_id": "dd8afdbe-59cf-4272-b640-b14a24d8234c",
  "created_at": "2018-02-17 16:00:00.000Z",
  "id": "6db276a8-2efe-4495-9908-4d3fc4cc16fa",
  "event_type": "data",
  "total_charged_amount": {
    "tax_free_amount": null,
    "tax_amounts": [],
    "tax_included_amount": {
      "amount": 0.0241,
      "currency": "EUR"
    }
  }
  "used_service_units": [
    {
      "amount": 2412739,
      "currency": null,
      "unit_of_measure": "bytes"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

现在创建一个流很容易就像event_type和created_at这样简单的东西.那会是这样的

CREATE STREAM tstream (event_type varchar, created_at varchar) WITH (kafka_topic='usage_events', value_format='json');

但现在我需要访问used_service_units ....我想在上面的JSON中提取"金额"

我该怎么做?

CREATE STREAM usage (event_type varchar,create_at varchar, used_service_units[0].amount int) WITH (kafka_topic='usage_events', value_format='json');
Run Code Online (Sandbox Code Playgroud)

结果是

line 1:78: mismatched input '[' expecting {'ADD', 'APPROXIMATE', ...
Run Code Online (Sandbox Code Playgroud)

如果我改为创建一个像这样的流

CREATE STREAM usage (event_type varchar,create_at varchar, …
Run Code Online (Sandbox Code Playgroud)

json apache-kafka ksql

5
推荐指数
1
解决办法
1604
查看次数

标签 统计

json ×2

apache-kafka ×1

ksql ×1

python ×1

ruamel.yaml ×1