首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >要求卡夫卡休息

要求卡夫卡休息
EN

Stack Overflow用户
提问于 2020-05-10 16:30:57
回答 1查看 8.7K关注 0票数 2

我已经使用curl调用在kafka-rest上执行了HTTP。一个请求是成功的,而另一个请求(具有不同的json)则返回422错误({"error_code":422,"message":"Unrecognized : receiver"})。

工作请求

代码语言:javascript
复制
    curl -X POST -H "Content-Type: application/vnd.kafka.json.v2+json" -H "Accept: application/vnd.kafka.v2+json" --data '{"records":[{"value":{"foo":"bar"}}]}' "http://localhost:8082/topics/jsontest"

出站

代码语言:javascript
复制
{"offsets":[{"partition":0,"offset":0,"error_code":null,"error":null}],"key_schema_id":null,"value_schema_id":null}

不工作请求

代码语言:javascript
复制
 curl -X POST -H "Content-Type: application/vnd.kafka.json.v2+json" -H "Accept: application/vnd.kafka.v2+json" --data @alert-request.json "http://localhost:8082/topics/jsontest"

输出

代码语言:javascript
复制
{"error_code":422,"message":"Unrecognized field: receiver"}

alert-request.json

代码语言:javascript
复制
{
  "receiver": "email-logs",
  "status": "firing",
  "alerts": [
    {
      "status": "firing",
      "labels": {
        "alertname": "CPUUsageAbove20%",
        "instance": "node-exporter:9100",
        "job": "node-exporter",
        "monitor": "my-project",
        "severity": "warn",
        "team": "raptors"
      },
      "annotations": {
        "dashboard": "www.prometheus.io",
        "description": "CPU usage on node-exporter:9100 has reached 60"
      },
      "startsAt": "2020-04-30T08:03:17.309164516Z",
      "endsAt": "0001-01-01T00:00:00Z",
      "generatorURL": "http://0ab6d9955c65:9090/graph?g0.expr=60+%3E+job%3Anode_cpu_seconds%3Ausage+%3E+20\u0026g0.tab=1",
      "fingerprint": "9c558a8c20c2ba08"
    }
  ],
  "groupLabels": {
    "alertname": "CPUUsageAbove20%",
    "team": "raptors"
  },
  "commonLabels": {
    "alertname": "CPUUsageAbove20%",
    "instance": "node-exporter:9100",
    "job": "node-exporter",
    "monitor": "my-project",
    "severity": "warn",
    "team": "raptors"
  },
  "commonAnnotations": {
    "dashboard": "www.prometheus.io",
    "description": "CPU usage on node-exporter:9100 has reached 60"
  },
  "externalURL": "http://5493399b56dc:9093",
  "version": "4",
  "groupKey": "{}/{team=~\"^(?:(raptors|leafs))$\"}:{alertname=\"CPUUsageAbove20%\", team=\"raptors\"}"
}

我需要在json上面使用kafka-rest写到kafka,我使用curl调用在kafka-rest上执行HTTP,但这是返回错误。如何用卡夫卡-rest(卷曲)成功地书写卡夫卡?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-10 16:53:58

作为"error_code":422的响应中的错误代码意味着请求有效负载不正确。

根据kafka-rest的API规范,有效的请求有效负载结构之一是

代码语言:javascript
复制
{
  "records": [
    {
      "key": "recordKey",
      "value": "recordValue"
    }
  ]
}

因此,您的alert-request.json文件应该使用value字段中records中的内容进行修改。

代码语言:javascript
复制
{
  "records": [
    {
      "key": "recordKey",
      "value": {
        "receiver": "email-logs",
        "status": "firing",
        "alerts": [
          {
            "status": "firing",
            "labels": {
              "alertname": "CPUUsageAbove20%",
              "instance": "node-exporter:9100",
              "job": "node-exporter",
              "monitor": "my-project",
              "severity": "warn",
              "team": "raptors"
            },
            "annotations": {
              "dashboard": "www.prometheus.io",
              "description": "CPU usage on node-exporter:9100 has reached 60"
            },
            "startsAt": "2020-04-30T08:03:17.309164516Z",
            "endsAt": "0001-01-01T00:00:00Z",
            "generatorURL": "http://0ab6d9955c65:9090/graph?g0.expr=60+%3E+job%3Anode_cpu_seconds%3Ausage+%3E+20\u0026g0.tab=1",
            "fingerprint": "9c558a8c20c2ba08"
          }
        ],
        "groupLabels": {
          "alertname": "CPUUsageAbove20%",
          "team": "raptors"
        },
        "commonLabels": {
          "alertname": "CPUUsageAbove20%",
          "instance": "node-exporter:9100",
          "job": "node-exporter",
          "monitor": "my-project",
          "severity": "warn",
          "team": "raptors"
        },
        "commonAnnotations": {
          "dashboard": "www.prometheus.io",
          "description": "CPU usage on node-exporter:9100 has reached 60"
        },
        "externalURL": "http://5493399b56dc:9093",
        "version": "4",
        "groupKey": "{}/{team=~\"^(?:(raptors|leafs))$\"}:{alertname=\"CPUUsageAbove20%\", team=\"raptors\"}"
      }
    }
  ]
}

请注意,key字段是可选的,您可以提供适合您的用例的任何唯一键。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61715273

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档