Write Message

通过指定通道,向数据源写入消息数据。

前提条件

已创建数据写入通道,且通道已启动。

请求格式

POST https://{apigw-address}/data-federation/v2.0/channels/write/{channelId}/msg

请求参数(URI)

名称

位置(Path/Query)

必需/可选

数据类型

描述

orgId

Query

必需

String

用户所属的组织ID。如何获取orgId信息>>

channelId

Path

必需

String

通道ID。

请求参数(Body)

名称

必需/可选

数据类型

描述

dataSourceName

必需

String

数据源别名。

data

必需

String

待写入的数据。

sync

必需

Boolean

指定数据写入方式。true:同步写入,false:异步写入,默认使用同步写入。

响应参数

名称

数据类型

描述

failures

List<JSONObject>

写入失败的数据列表,详见 失败记录结构体

失败记录结构体

名称

数据类型

描述

info

List<JSONObject>

失败记录详情,详见 失败记录详情

line

List<JSONObject>

失败数据。

失败记录详情

名称

数据类型

描述

field

String

失败字段。

reason

String

失败原因。

示例

请求示例

url: https://{apigw-address}/data-federation/v2.0/channels/write/{channelId}/msg?orgId={}

method: POST

requestBody:
{
    "dataSourceName": "mysql_remote",
    "data": "{\"table\":\"data\",\"lines\":[{\"WGEN.GenReactivePW\":\"2.5283\",\"ou_id\":\"o15622268182161\",\"WTUR.TurbineListSts\":\"5\",\"WTUR.TurbineUnionSts\":\"71\",\"WTUR.ConnectionSts\":\"0\",\"WGEN.GenActivePW\":\"45.700001\",\"WROT.TemB2Mot\":\"29.504801\",\"WTUR.TurbineTopSts\":\"2\",\"WGEN.TorqueSetpoint\":\"867.359375\",\"WWPP.PPCurrentDay\":\"0\",\"WCNV.GridFreq\":\"49.971561\",\"WWPP.PPCurrentYear\":\"17896\",\"dev_id\":\"04mmQAEM\",\"WNAC.TemOut\":\"25.691801\",\"WWPP.PPCurrentMonth\":\"33763\",\"WTUR.TurbineHealthSts\":\"0\",\"timeOfDay\":\"2019-09-01 00:00:00\",\"WTUR.TurbineGroupSts\":\"70\"},{\"WGEN.GenReactivePW\":\"2.7037\",\"ou_id\":\"o15622268182161\",\"WTUR.TurbineListSts\":\"5\",\"WTUR.TurbineUnionSts\":\"71\",\"WTUR.ConnectionSts\":\"0\",\"WGEN.GenActivePW\":\"45.439999\",\"WTUR.TurbineTopSts\":\"2\",\"WGEN.TorqueSetpoint\":\"865.127869\",\"WCNV.GridFreq\":\"49.998112\",\"dev_id\":\"04mmQAEM\",\"WTUR.TurbineHealthSts\":\"0\",\"timeOfDay\":\"2019-09-01 00:00:01\",\"WTUR.TurbineGroupSts\":\"70\"}]}"
    "sync":true
}

返回示例

{
   "msg": "OK",
   "code": 0,
   "failures": [
     {
       "line": [],
       "info": [
         {
           "reason": "",
           "field": ""
         }
       ]
     }
   ],
   "submsg": ""
 }

Java SDK调用示例

import com.alibaba.fastjson.JSONObject;
import com.envision.apim.poseidon.config.PConfig;
import com.envision.apim.poseidon.core.Poseidon;
import com.envision.apim.poseidon.request.PoseidonRequest;
import com.google.common.net.HttpHeaders;
import org.apache.commons.codec.binary.Hex;
import org.junit.Test;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Sample {

    private static String accessKey = "AccessKey of your APP";
    private static String secretKey = "SecretKey of your APP";
    private static String orgId = "yourOrgId";
    private static String chId = "yourChannelId";
    private static String url = "https://{domain_url}";

    private static class Request extends PoseidonRequest {

        public void setQueryParam(String key, Object value) {
            queryEncodeParams().put(key, value);
        }

        public void setMethod(String method) {
            this.method = method;
        }

        public void setBodyParams(String key, Object value) {
            bodyParams().put(key, value);
        }

        private String method;

        @Override
        public String baseUri() {
            return "";
        }

        @Override
        public String method() {
            return method;
        }
    }

    private String createData() {
        Map<String, Object> data = new HashMap<>();
        data.put("table", "test_ppe_xia");
        List<Map<String, Object>> line = new ArrayList<>();
        Map<String, Object> temp = new HashMap<>();
//        temp.put("dev_id", UUID.randomUUID().toString().substring(0, 6));
        temp.put("sex", "女");
        temp.put("name", "明明");
        temp.put("age", 133);
        line.add(temp);
        data.put("lines", line);
        return JSONObject.toJSONString(data);
    }

    @Test
    public void writeMsg() {
        for (int i = 0; i < 1; i++) {
            Request request = new Request();
            request.setQueryParam("orgId", orgId);
            request.setMethod("POST");
            request.headerParams().put(HttpHeaders.CONTENT_TYPE, "application/json");
            request.setBodyParams("data", createData());
            request.setBodyParams("dataSourceName", "mysql_source_test");
            request.setBodyParams("sync", true);
            try {
                JSONObject response = Poseidon.config(PConfig.init().appKey(accessKey).appSecret(secretKey).debug())
                        .url(url + "/data-federation/v2.0/channels/write/" + chId + "/msg")
                        .getResponse(request, JSONObject.class);
                System.out.println(response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}