How to get Flux<T> from Mono<K> in Spring Reactive API? - spring

I have two independent collections in NoSQL document db Photo and Property where Photo has propertyId parameter meaning that I can find all photos that belong to a given property like a house. Normally without reactive I would simply do:
Property property = ....
List<Photo> = photoService.findByPropertyId(property.getId());
Just two lines. How to do above in Reactive Programming when I have
`Mono<Property> and I want to find Flux<Photo>
without using block()?` Assume aphotoService.findByPropertyId return List and in reactive case it returns Flux.

You should use flatMapMany, which triggers an async processing from the Mono's value which can emit multiple elements:
Flux<Photo> photoFlux = propertyMono
.flatMapMany(prop -> photoService.findByPropertyId(prop.getId()));

Related

Kafka streams: Using the DSL api, within a transform, how can I send two messages to different topics/separate DSL downstream processors

I'm using the DSL api and I have a use case where I need to check a condition and then if true, send an additional message to a separate topic from the happy path. My question is, how can I attach child processors to parents in the DSL api? Is it as simple as caching a stream variable and using it in two subsequent places, and naming those stream processors? Here's some brief code that explains what I'm trying to do. I am using the DSL api because I need the use of the foreignKeyJoin.
var myStream = stream.process(myProcessorSupplier); //3.3 returns a stream
stream.to("happyThingTopic"); Q: will the forward ever land here?
stream.map( myKvMapper, new Named("what-is-this")).to("myOtherTopic"); //will the forward land here?
public KeyValue<String, Object> process(Object key, Object value){
if (value.hasFlag){
processorContext.forward(key, new OtherThing(), "what-is-this?");
}
return new KeyValue(key, HappyThing(value));
}

Spring reactive: Chaining repository results

Repository repo
Repository otherRepo
foreach entity : repo.FindAll() {
entityFind = otherRepo.FindById(entity.Prop)
if (entityFind != null) {
return entityFind
}
}
How could I do this using the spring reactive?
I could use blockFirst() to search in otherRepo but it would break the reaction chain
I also have tried use a handle() to control the flow but I don't get to break the flow when I find an item
Any idea?
Thanks
If you have repos like this, for each record of repo1, if you need to find a record from repo2, you could probably join the tables using spring data JPQL & use your custom method instead as your current approach could have performance impact.
As you seem to be interested only in the first record, Just to give you an idea, We can achieve something like this.
return Flux.fromIterable(repo.findAll()) //assuming it returns a list
.map(entity -> otherRepo.findById(entity.property)) // for each entity we query the other repo
.filter(Objects::nonNull) // replace it with Optional::isPresent if it is optional
.next(); //converts the flux to mono with the first record
The answer from vins is assuming non-reactive repository, so here it is in a fully reactive style:
return repo.findAll() //assuming reactive repository, which returns Flux<Entity>
.flatMap(entity -> otherRepo.findById(entity.property)) //findById returns an empty Mono if id not found, which basically gets ignored by flatMap
.next(); //first record is turned into a Mono, and the Flux is cancelled
Note that as you've stated, this can lead to unnecessary requests being made to Cassandra (and then cancelled by the next()). This is due to flatMap allowing several concurrent requests (256 by default). You can either reduce the parallelism of flatMap (by providing a second parameter, an int) or use concatMap to perform findById queries serially.

sdn4.0 set resultDataContents graph

I want to use SDN4.0 to visualize by D3 in web application.For example,I want to use the following cypher query to get data:
#Query("MATCH (n:app)-[r:r1]->(m:app) RETURN n.alias,r,m.alias")
Iterable<Map<String, Object>> getAllRelations();
But the httpServer not response the exact data I want.
[{n.alias=A, r=(227)-[r1]->(235), m.alias=B}, ....]
And I want to response r1's properties ,tried r1.* but failed.
From the http://neo4j.com/developer/guide-data-visualization/ there is possible to set resultDataContents to graph(origin as rest)
So Is it any possiblity to set this parameter in SDN4.0 or have other solutions?
Thanks if having any ideas.
SDN is for creating domain rich applications. As such it's not the best tool to use if all you need is a list of properties to represent a graph.
You have a couple of paths as I can see it:
Model your application properly with SDN. Use #NodeEntity on a class called App and create a #Relationship to another App. You can then leverage SDN's repositories to return you a rich domain model which you can then transform (e.g. using DTO's) to the front end if need be.
Use the java Neo4j client, OGM HTTP driver (Undocumented), or if you are happy to completely use Javascript (either from the browser or with meteor or using a NodeJS server) you can just use the Javascript driver and call the database directly.
Either way if you are using D3 I highly recommend you use JSOG to represent your model in the front end.
How do I query for relationship data in spring data neo4j 4?
Above Answer solved my question.By using Neo4jOperations.queryForObjects() under Spring data Neo4j and return path in cypher query.

Entity Framework--Filter Data At Load Time

I’m building an MVC3 application with form authentication and a single hierarchical entity. The entity has a Region object as the “root” with several other objects. It looks something like:
Region->Language->objectA->objectB
Region->Application->….
Each user (other than administrators) is associated with single region. I'd like to limit the data loaded to the entity based on the user’s region. I’m not too familiar with EF. Is this appropriate or is there a better approach? How would I implement the best approach
You can certainly filter the data returned via Entity Framework. The code would look something like this:
using (MyContext ctx = new MyContext())
{
var filtered = (from r in ctx.Regions where SOME_CONDITIONS select r);
// Do stuff with filtered (which is an IEnumerable<Region>)
}
Note that you may need to use Include to load related objects, e.g.
ctx.Regions.Include("Language").Include("Application")
see http://msdn.microsoft.com/en-us/library/bb896272.aspx

What is the difference between service.Create and orgContext.AddObject?

I find that there are two ways at least to create a record in an entity like the following.
Common Part
var record = new someEntity()
{
attribute1="test1",
attribute2="test2"
};
var service = new OrganizationService("CrmConnectionString");
Part A
service.Create(record);
Part B
var orgContext = new OrganizationServiceContext(service);
orgContext.AddObject(record);
orgContext.SaveChanges();
What is the difference?And which is better?
Part A uses the raw create method of the organization service proxy. This operation directly creates the record.
Part B makes use of the OrganizationServiceContext, which implements the Unit of Work pattern. Your operations are not transmitted to the server until you call SaveChanges()
Which is better? It depends on your requirements. If you only want to create a record on the go -> use the service. If you do multiple things which form a logical unit take version B.

Resources