我已经在我的user-impl服务中创建了一个用户存储库Lagom readside,但是由于某些原因,运行sbt lagom:runAll
时未创建Cassandra users表。我不确定我在这里缺少什么。
我的UserRepository类:
@Singleton
public class UserRepository {
private final CassandraSession session;
@Inject
public UserRepository(CassandraSession session, ReadSide readSide) {
this.session = session;
readSide.register(PUserEventProcessor.class);
}
private static class PUserEventProcessor extends ReadSideProcessor<PUserEvent> {
private final CassandraSession session;
private final CassandraReadSide readSide;
private PreparedStatement insertUserStatement;
private PreparedStatement updateUserStatement;
@Inject
public PUserEventProcessor(CassandraSession session, CassandraReadSide readSide) {
this.session = session;
this.readSide = readSide;
}
@Override
public ReadSideHandler<PUserEvent> buildHandler() {
return readSide.<PUserEvent>builder("pUserEventOffset")
.setGlobalPrepare(this::createTables)
.setPrepare(tag -> prepareStatements())
.setEventHandler(PUserEvent.PUserCreated.class,
e -> insertUser(e.getUser()))
.setEventHandler(PUserEvent.PUserUpdated.class,
e -> updateUser(e.getUser()))
.build();
}
private void registerCodec(Session session, InstantCodec codec) {
session.getCluster().getConfiguration().getCodecRegistry().register(codec);
}
@Override
public PSequence<AggregateEventTag<PUserEvent>> aggregateTags() {
return PUserEvent.TAG.allTags();
}
private CompletionStage<Done> createTables() {
return doAll(
session.executeCreateTable(
"CREATE TABLE IF NOT EXISTS users (" +
"userId UUID, " +
"email text, " +
"firstName text, " +
"lastName text, " +
"gender text, " +
"PRIMARY KEY (userId) " +
")"
)
);
}
private CompletionStage<Done> prepareStatements() {
return doAll(
session.underlying()
.thenAccept(s -> registerCodec(s, InstantCodec.instance))
.thenApply(x -> Done.getInstance()),
prepareInsertUserStatement(),
prepareUpdateUserStatement());
}
// Insert users
private CompletionStage<Done> prepareInsertUserStatement() {
return session.
prepare("INSERT INTO users(" +
"userId, " +
"email" +
"firstName, " +
"lastName, " +
"gender, " +
"refreshToken" +
") " +
"VALUES (?, ?, ?, ?, ?, ?)"
)
.thenApply(accept(s -> insertUserStatement = s));
}
private CompletionStage<List<BoundStatement>> insertUser(DbUser user) {
return completedStatements(
insertUserCreator(user)
);
}
private BoundStatement insertUserCreator(DbUser user) {
return insertUserStatement.bind(
user.getUserId(),
user.getEmail(),
user.getFirstName(),
user.getLastName(),
user.getGender(),
user.getRefreshToken()
);
}
// Update user by userId
private CompletionStage<Done> prepareUpdateUserStatement() {
return session.
prepare("UPDATE users " +
"SET email = ?, " +
" firstName = ?, " +
" lastName = ?, " +
" gender = ?, " +
" refreshToken = ?" +
"WHERE userId = ?;"
)
.thenApply(accept(s -> updateUserStatement = s));
}
private CompletionStage<List<BoundStatement>> updateUser(DbUser user) {
return completedStatements(
updateUserCreator(user)
);
}
private BoundStatement updateUserCreator(DbUser user) {
return updateUserStatement.bind(
user.getEmail(),
user.getFirstName(),
user.getLastName(),
user.getGender(),
user.getUserId(),
user.getRefreshToken()
);
}
}
}
已添加到UserModule.java:
public class UserModule extends AbstractModule implements ServiceGuiceSupport {
@Override
protected void configure() {
bindService(UserService.class, UserServiceImpl.class);
bind(UserRepository.class);
}
}
application.conf:
play.modules.enabled += UserModule
lagom.persistence.ask-timeout = 1000s
user.cassandra.keyspace = user
cassandra-journal.keyspace = ${user.cassandra.keyspace}
cassandra-snapshot-store.keyspace = ${user.cassandra.keyspace}
lagom.persistence.read-side.cassandra.keyspace = ${user.cassandra.keyspace}
我的设置是标准设置:
“用户”表已创建,消息表甚至存储了事件。
为什么不显示“用户”表或如何进行故障排除有什么帮助?
这让我发疯。
启动服务时没有错误抛出,检查了lagom-auction示例,并且那里的键空间可用。我看不出代码有什么区别...
编辑: 尝试获取我的表: -sbt clean /手动删除嵌入式cassandra -将lagom-sbt-plugin升级到1.4.8
非常感谢帮助甚至技巧。没有阅读面就不能做很多事,哈哈。
答案 0 :(得分:0)
您需要将 result_array = [{
"date": 1533791167870,
"count": 1,
"name": "James"
}, {
"date": 1533791167870,
"count": 3,
"name": "Kane"
},
{
"date": 1533791167870,
"count": 0,
"name": "Trent"
},
{
"date": 1533791167870,
"count": 0,
"name": "Sam"
}, {
"date": 1533791167918,
"count": 0,
"name": "James"
},
{
"date": 1533791167918,
"count": 1,
"name": "Kane"
},
{
"date": 1533791167918,
"count": 0,
"name": "Sam"
},
{
"date": 1533791167918,
"count": 0,
"name": "Trent"
}, {
"date": 1536203856526,
"count": 0,
"name": "James"
},
{
"date": 1536203856526,
"count": 0,
"name": "Kane"
},
{
"date": 1536203856526,
"count": 2,
"name": "Trent"
},
{
"date": 1536203856526,
"count": 0,
"name": "Sam"
}, {
"date": 1536217871371,
"count": 0,
"name": "James"
},
{
"date": 1536217871371,
"count": 5,
"name": "Kane"
},
{
"date": 1536217871371,
"count": 5,
"name": "Trent"
},
{
"date": 1536217871371,
"count": 5,
"name": "Sam"
}, {
"date": 1536217882525,
"count": 4,
"name": "James"
},
{
"date": 1536217882525,
"count": 0,
"name": "Trent"
}, {
"date": 1536217882525,
"count": 0,
"name": "Sam"
},
{
"date": 1536217882525,
"count": 0,
"name": "Kane"
}
]
绑定为eager singleton。这将强制初始化不延迟。您会注意到,它在产品中无关紧要,但对于开发而言却无关紧要(这可能就是为什么您在开发人员中看到此问题的原因。)
有关示例,请参见online auction。