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 Answer
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
EXPERT
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions