Updated on 2024-10-21 GMT+08:00

Uploading Data

Function

This API is used to upload data to DIS streams.

Calling Method

For details, see Calling APIs.

URI

POST /v2/{project_id}/records

Table 1 Path Parameters

Parameter

Mandatory

Type

Description

project_id

Yes

String

Project ID

Request Parameters

Table 2 Request header parameters

Parameter

Mandatory

Type

Description

X-Auth-Token

Yes

String

User token.

It can be obtained by calling the IAM API used to obtain a user token. The value of X-Subject-Token in the response header is the user token.

Table 3 Request body parameters

Parameter

Mandatory

Type

Description

stream_name

Yes

String

Name of the stream

Maximum: 60

stream_id

No

String

Unique ID of the stream.

If no stream is found based on stream_name and stream_id is not empty, stream_id is used to search for the stream.

Note:

This parameter is mandatory when you upload data to a stream that has been authorized.

records

Yes

Array of PutRecordsRequestEntry objects

List of records to be uploaded

Table 4 PutRecordsRequestEntry

Parameter

Mandatory

Type

Description

data

Yes

String

Data to be uploaded.

The uploaded data is the serialized binary data (character string encoded using Base64).

For example, if the character string data needs to be uploaded, the character string after Base64 encoding is ZGF0YQ==.

explicit_hash_key

No

String

Hash value of the data to be written to the partition. The hash value overwrites the hash value of partition_key.

Value range: 0 to long.max

partition_id

No

String

Partition identifier of the stream.Two partition ID formats are available:- shardId-0000000000- 0For example, if a stream has three partitions, the partition IDs are 0, 1, and 2, or shardId-0000000000, shardId-0000000001, and shardId-0000000002.

partition_key

No

String

Partition to which data is written to.Note:If partition_id is transferred, it is preferentially used. If it is not transferred, partition_key is used.

Response Parameters

Status code: 200

Table 5 Response body parameters

Parameter

Type

Description

failed_record_count

Integer

Number of data records that fail to be uploaded

records

Array of PutRecordsResultEntry objects

List of upload results

Table 6 PutRecordsResultEntry

Parameter

Type

Description

partition_id

String

ID of the partition to which data is uploaded

sequence_number

String

Sequence number of the data to be uploaded. A sequence number is the unique identifier of each record. DIS automatically allocates a sequence number when the data producer calls the PutRecords operation to add data to the DIS stream. The sequence number of the same partition key usually changes with time. A longer interval between PutRecords requests results in a larger sequence number.

error_code

String

Error code

error_message

String

Error message

Example Requests

Uploading Data

POST https://{Endpoint}/v2/{project_id}/records

{
  "stream_name" : "newstream",
  "records" : [ {
    "data" : "MTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTE="
  } ]
}

Example Responses

None

SDK Sample Code

The SDK sample code is as follows.

Java

Uploading Data

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.huaweicloud.sdk.test;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.dis.v2.region.DisRegion;
import com.huaweicloud.sdk.dis.v2.*;
import com.huaweicloud.sdk.dis.v2.model.*;

import java.util.List;
import java.util.ArrayList;

public class SendRecordsSolution {

    public static void main(String[] args) {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        String ak = System.getenv("CLOUD_SDK_AK");
        String sk = System.getenv("CLOUD_SDK_SK");

        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);

        DisClient client = DisClient.newBuilder()
                .withCredential(auth)
                .withRegion(DisRegion.valueOf("<YOUR REGION>"))
                .build();
        SendRecordsRequest request = new SendRecordsRequest();
        PutRecordsRequest body = new PutRecordsRequest();
        List<PutRecordsRequestEntry> listbodyRecords = new ArrayList<>();
        listbodyRecords.add(
            new PutRecordsRequestEntry()
                .withData("MTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTE=")
        );
        body.withRecords(listbodyRecords);
        body.withStreamName("newstream");
        request.withBody(body);
        try {
            SendRecordsResponse response = client.sendRecords(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}

Python

Uploading Data

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# coding: utf-8

import os
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkdis.v2.region.dis_region import DisRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkdis.v2 import *

if __name__ == "__main__":
    # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak = os.environ["CLOUD_SDK_AK"]
    sk = os.environ["CLOUD_SDK_SK"]

    credentials = BasicCredentials(ak, sk)

    client = DisClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(DisRegion.value_of("<YOUR REGION>")) \
        .build()

    try:
        request = SendRecordsRequest()
        listRecordsbody = [
            PutRecordsRequestEntry(
                data="MTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTE="
            )
        ]
        request.body = PutRecordsRequest(
            records=listRecordsbody,
            stream_name="newstream"
        )
        response = client.send_records(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

Go

Uploading Data

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
    dis "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dis/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dis/v2/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dis/v2/region"
)

func main() {
    // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak := os.Getenv("CLOUD_SDK_AK")
    sk := os.Getenv("CLOUD_SDK_SK")

    auth := basic.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        Build()

    client := dis.NewDisClient(
        dis.DisClientBuilder().
            WithRegion(region.ValueOf("<YOUR REGION>")).
            WithCredential(auth).
            Build())

    request := &model.SendRecordsRequest{}
	var listRecordsbody = []model.PutRecordsRequestEntry{
        {
            Data: "MTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTE=",
        },
    }
	request.Body = &model.PutRecordsRequest{
		Records: listRecordsbody,
		StreamName: "newstream",
	}
	response, err := client.SendRecords(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

More

For SDK sample code of more programming languages, see the Sample Code tab in API Explorer. SDK sample code can be automatically generated.

Status Codes

Status Code

Description

200

Normal response

Error Codes

See Error Codes.