Code: Select all
BookTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: "books"
BillingMode: PAY_PER_REQUEST
PointInTimeRecoverySpecification:
PointInTimeRecoveryEnabled: True
StreamSpecification:
StreamViewType: NEW_AND_OLD_IMAGES
AttributeDefinitions:
- AttributeName: "bookId"
AttributeType: "S"
- AttributeName: "createdAt"
AttributeType: "N"
KeySchema:
- AttributeName: "bookId"
KeyType: HASH
- AttributeName: "createdAt"
KeyType: RANGE
Code: Select all
package com.myservice.model;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBDocument;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIgnore;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConvertedEnum;
import java.io.Serializable;
import java.util.Date;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@DynamoDBTable(tableName = "books")
public class Book {
@Data
@Builder
@DynamoDBDocument
@NoArgsConstructor
@AllArgsConstructor
public static class PrimaryKey implements Serializable {
@DynamoDBHashKey
private String bookId;
@DynamoDBRangeKey
private Date createdAt;
@DynamoDBIgnore
public static Book.PrimaryKey of(String bookId, Date createdAt) {
return Book.PrimaryKey.builder()
.bookId(bookId)
.createdAt(createdAt)
.build();
}
}
@Id
@DynamoDBIgnore
private PrimaryKey pk;
private String description;
@DynamoDBHashKey
@DynamoDBAttribute(attributeName = "bookId")
public String getbookId() {
return pk != null ? pk.getbookId() : null;
}
public Book setbookId(final String bookId) {
if (pk == null) {
pk = new Book.PrimaryKey();
}
pk.setbookId(bookId);
return this;
}
@DynamoDBRangeKey
@DynamoDBAttribute(attributeName = "createdAt")
public Date getCreatedAt() {
return pk != null ? pk.getCreatedAt() : null;
}
public Book setCreatedAt(final Date createdAt) {
if (pk == null) {
pk = new Book.PrimaryKey();
}
pk.setCreatedAt(createdAt);
return this;
}
}
< /code>
Datenzugriffsobjekt: < /p>
@Repository
@EnableScan
@EnableScanCount
public interface BookDao extends DynamoDBPagingAndSortingRepository {
Page findByBookId(String bookId, Pageable page);
}
< /code>
Serviceschnittstelle: < /p>
public interface BookService {
Page getBooksUsingBookId(String bookId, Pageable page);
}
< /code>
Service-Implementierung: < /p>
@Service
@RequiredArgsConstructor
public class BookServiceImpl implements BookService {
private final BookDao bookDao;
@Override
public Page getBooksUsingBookId(String bookId, Pageable page) {
return bookDao.findByBookId(bookId, page);
}
}
Code: Select all
protected Long createdTimestamp;
public Long getCreatedTimestamp() {
if (isNull(createdTimestamp) && nonNull(getCreatedAt())) {
this.createdTimestamp = getCreatedAt().getTime();
}
return createdTimestamp;
}