在AWS DynamoDB JAVA SDK中,null值会被转换为true。如果您希望将null值转换为其他值或不进行转换,可以使用DynamoDBMapperConfig对象来配置转换行为。
下面是一个示例代码,演示如何使用DynamoDBMapperConfig来自定义null值的转换行为:
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.model.PutItemResult;
import com.amazonaws.services.dynamodbv2.document.spec.PutItemSpec;
import com.amazonaws.services.dynamodbv2.document.utils.ValueMap;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
public class DynamoDBExample {
public static void main(String[] args) {
// Create an instance of AmazonDynamoDB
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
// Create a DynamoDBMapperConfig object
DynamoDBMapperConfig mapperConfig = DynamoDBMapperConfig.builder()
.withSaveBehavior(DynamoDBMapperConfig.SaveBehavior.CLOBBER)
.build();
// Create a DynamoDBMapper object with the custom mapperConfig
DynamoDBMapper mapper = new DynamoDBMapper(client, mapperConfig);
// Create an instance of the Item class
Item item = new Item()
.withPrimaryKey("id", 1)
.withNull("nullValue", true);
// Save the item to DynamoDB
mapper.save(item);
// Retrieve the item from DynamoDB
Table table = new DynamoDB(client).getTable("YourTableName");
Item retrievedItem = table.getItem("id", 1);
// Print the retrieved item
System.out.println(retrievedItem.toJSONPretty());
}
}
在上面的示例中,我们创建了一个DynamoDBMapperConfig对象,并通过withSaveBehavior方法将其配置为DynamoDBMapperConfig.SaveBehavior.CLOBBER。这将使null值被转换为true。
请替换YourTableName为您的表名,并根据您的实际需求进行其他自定义配置。