Writing multimeasure records with AWS JavaV2 SDK

0

Is it possible to write multimeasure records to Timestream AWS SDK JavaV2? See below code for reference on how I am trying to do this. I get an error while writing to Timestream - software.amazon.awssdk.services.timestreamwrite.model.ValidationException: The request contains one or more records that are missing the mandatory attribute measureName. Attributes such as the time, measure name, and/or dimension name must be specified through common attributes or the array of records. (Service: TimestreamWrite, Status Code: 400, Request ID: XXXXXXXXXXXXXXX)

Looks like the JavaV2 sdk is not supplying the measureValues property to Timestream. I have also tried the vararg builder method with same result - Record.Builder measureValues(MeasureValue... var1)

I am on version 2.20.26 of software.amazon.awssdk.timestreamwrite

val mv: MeasureValue = MeasureValue.builder()
            .name(measureName)
            .type("DOUBLE").value(measureValue).build()

return Record.builder()
            .time(event.timestamp)
            .dimensions(dimensions)
            .measureValues(mutableListOf(mv))
            .version(version)
            .build()
1개 답변
0

Common attributes are attributes that are shared across multiple records. You can specify these attributes once, and then they are applied to all the records that you are writing. In your case, the time and dimensions are likely common attributes.

public void writeRecordsWithCommonAttributes() {
    System.out.println("Writing records with extracting common attributes");
    // Specify repeated values for all records
    List<Record> records = new ArrayList<>();
    final long time = System.currentTimeMillis();

    List<Dimension> dimensions = new ArrayList<>();
    final Dimension region = Dimension.builder().name("region").value("us-east-1").build();
    final Dimension az = Dimension.builder().name("az").value("az1").build();
    final Dimension hostname = Dimension.builder().name("hostname").value("host1").build();

    dimensions.add(region);
    dimensions.add(az);
    dimensions.add(hostname);

    Record commonAttributes = Record.builder()
            .dimensions(dimensions)
            .measureValueType(MeasureValueType.DOUBLE)
            .time(String.valueOf(time)).build();

    Record cpuUtilization = Record.builder()
            .measureName("cpu_utilization")
            .measureValue("13.5").build();
    Record memoryUtilization = Record.builder()
            .measureName("memory_utilization")
            .measureValue("40").build();

    records.add(cpuUtilization);
    records.add(memoryUtilization);

    WriteRecordsRequest writeRecordsRequest = WriteRecordsRequest.builder()
            .databaseName(DATABASE_NAME)
            .tableName(TABLE_NAME)
            .commonAttributes(commonAttributes)
            .records(records).build();

    try {
        WriteRecordsResponse writeRecordsResponse = timestreamWriteClient.writeRecords(writeRecordsRequest);
        System.out.println("writeRecordsWithCommonAttributes Status: " + writeRecordsResponse.sdkHttpResponse().statusCode());
    } catch (RejectedRecordsException e) {
        System.out.println("RejectedRecords: " + e);
        for (RejectedRecord rejectedRecord : e.rejectedRecords()) {
            System.out.println("Rejected Index " + rejectedRecord.recordIndex() + ": "
                    + rejectedRecord.reason());
        }
        System.out.println("Other records were written successfully. ");
    } catch (Exception e) {
        System.out.println("Error: " + e);
    }
}
profile picture
전문가
답변함 일 년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠