DynamoDB -Tabelle: Wie setzen Sie den Epoch -Zeitstempel mithilfe von Spring JPA vom Datum in Java ein? [geschlossen]Java

Java-Forum
Guest
 DynamoDB -Tabelle: Wie setzen Sie den Epoch -Zeitstempel mithilfe von Spring JPA vom Datum in Java ein? [geschlossen]

Post by Guest »

Ich benutze Java 17, Spring Boot 2.6.2, Lombok & AWS-Java-SDK-1.12.136, um einen RESTful-API-CRUD-Service zu erstellen. /> CloudFormation YAML-Abschnitt für diese neue Tabelle: < /p>

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
DynamoDB-Entität (die die PrimaryKey statische innere Klasse als zusammengesetzte Schlüssel verwendet):

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);
}
}
Verwenden Sie diese Methode, um das Datum des Datums des Datums in einem Epoch-Zeitstempel zu konvertieren.

Code: Select all

protected Long createdTimestamp;

public Long getCreatedTimestamp() {
if (isNull(createdTimestamp) && nonNull(getCreatedAt())) {
this.createdTimestamp = getCreatedAt().getTime();
}
return createdTimestamp;
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post