我们如何在Dgraph上使用lagom的读取处理器?

时间:2018-05-28 18:03:08

标签: java microservices lagom dgraph

我是拉格姆和dgraph的新手。我一直坚持如何使用 Dgraph 来使用lagom的读取端处理器。只是给你一个想法,下面是使用Cassandra和lagom的代码。

import akka.NotUsed;
import com.lightbend.lagom.javadsl.api.ServiceCall;
import com.lightbend.lagom.javadsl.persistence.cassandra.CassandraSession;
import java.util.concurrent.CompletableFuture;
import javax.inject.Inject;
import akka.stream.javadsl.Source;
public class FriendServiceImpl implements FriendService {

private final CassandraSession cassandraSession;

@Inject
public FriendServiceImpl(CassandraSession cassandraSession) {
    this.cassandraSession = cassandraSession;
}

//Implement your service method here

}

1 个答案:

答案 0 :(得分:1)

Lagom不为Dgraph提供开箱即用的支持。如果你必须使用带有Dgraph的Lagom的Read-Side处理器,那么你必须使用Lagom的Generic Read Side support。像这样:

/**
 * Read side processor for Dgraph.
 */
public class FriendEventProcessor extends ReadSideProcessor<FriendEvent> {
    private static void createModel() {
        //TODO: Initialize schema in Dgraph
    }

    @Override
    public ReadSideProcessor.ReadSideHandler<FriendEvent> buildHandler() {
        return new ReadSideHandler<FriendEvent>() {
            private final Done doneInstance = Done.getInstance();

            @Override
            public CompletionStage<Done> globalPrepare() {
                createModel();
                return CompletableFuture.completedFuture(doneInstance);
            }

            @Override
            public CompletionStage<Offset> prepare(final AggregateEventTag<FriendEvent> tag) {
                return CompletableFuture.completedFuture(Offset.NONE);
            }

            @Override
            public Flow<Pair<FriendEvent, Offset>, Done, ?> handle() {
                return Flow.<Pair<FriendEvent, Offset>>create()
                        .mapAsync(1, eventAndOffset -> {
                                    if (eventAndOffset.first() instanceof FriendCreated) {
                                        //TODO: Add Friend in Dgraph;
                                    }

                                    return CompletableFuture.completedFuture(doneInstance);
                                }
                        );
            }
        };
    }

    @Override
    public PSequence<AggregateEventTag<FriendEvent>> aggregateTags() {
        return FriendEvent.TAG.allTags();
    }
}

对于FriendEvent.TAG.allTags(),您必须在FriendEvent界面中添加以下代码:

int NUM_SHARDS = 20;

  AggregateEventShards<FriendEvent> TAG =
          AggregateEventTag.sharded(FriendEvent.class, NUM_SHARDS);

  @Override
  default AggregateEventShards<FriendEvent> aggregateTag() {
    return TAG;
  }

我希望这有帮助!