I want to write a specific query for my Hyperledger Composer app. Below, I have 2 assets and a transaction. Asset1 has a field called contents, which is an array of type Asset2. The associated code is below:
namespace org.acme.biznet
asset Asset1 identified by Asset1Id {
o String Asset1Id
--> Asset2[] contents
}
asset Asset2 identified by Asset2Id {
o String Asset2Id
}
transaction Transact {
--> Asset1 asset1
}
I want to select all instances of Transact where the associated Asset1 has a specified Asset2 inside. The closest solution I came to the query below, which did not work.
query GetTransactionsThatHaveAsset2 {
description: ""
statement:
SELECT org.acme.biznet.Transact
WHERE (asset1.contents CONTAINS (Asset2Id == _$propId))
}
The thing is, I have also written the query below.
query GetAsset1sThatHaveAsset2 {
description: ""
statement:
SELECT org.acme.biznet.Asset1
WHERE (contents CONTAINS (Asset2Id == _$propId))
}
This query behaves as intended, but it's selecting over Asset1. I want to select over Transact. How would I write this query?
no, you can't presently nest the query like you propose, nested named queries are not currently implemented in Composer (CouchDB is not a relational DB, and hence Composer query language can't translate the nested query presently to be translated to CouchDB) 2) Transact is a transaction - it contains a relationship identifier(s) you defined in your model, not the nested data stored in the related asset. You would have to define a query that searches for all transactions that matches the asset1 identifier field you passed to the trxn - then in your code, you can check transact.asset1.contents contains the 'something' (passed in to the trxn too?) using a javascript match - quite straightforward). Alternatively, you could use the REST API filters (loopback filters as opposed to queries) where (form your app code) you can resolve the relationships of transaction (Transact) back to asset1 (and its contents) using the REST call with filter eg
{"where":{"asset1":"resource:org.acme.net.Asset1#1"}, "include":"resolve"} . Hope this helps, maybe its nesting you're looking for exclusively..
In my case, I have these assets
// Define assets
asset Product identified by productId {
o String productId
o String description
o String serialNumber
o String modelNumber
o String status // TRANSFERED, RECEIVED, RECLAMED
o DateTime joinTime
--> Trader previousOwner
--> Trader currentOwner
--> Trader newOwner
}
// Trade, moves product from to a new owner.
transaction Trade {
--> Product product
--> Trader newOwner
o String trade_type
}
Executing a trade transaction results in the record:
{
"$class": "org.sp.network.Trade",
"product": "resource:org.sp.network.Product#123",
"newOwner": "resource:org.sp.network.Trader#6694",
"trade_type": "Trade",
"transactionId": "e39a86ed4748a3ab73b5e9c023f6bb0ca025098af09b8b5b2dca8f5f7ef0db67",
"timestamp": "2019-06-13T12:04:20.180Z"
}
And to query all Trade transactions that contain a product is
query ProductPath{
description: "Selete all Trade transactions for a specific ProductId"
statement:
SELECT org.sp.network.Trade
WHERE (_$productId==product)
}
Using the rest server: the value of _$productId is resource:org.sp.network.Product#123
Related
Jmeter ForEach Controller failing to write variables in original order correctly
I am executing a http request retrieving a json payload with an array of employees. For each record (employee) I need to parse the record for specific fields e.g. firstName, lastName, PersonId and write to a single csv file, incrementing a new row per record.
Unfortunately, the file created has two issues. The PersonId never gets written and secondly the sequence of the values is not consistent with the returned original values. Sometimes I get the same record for lastName with the wrong firstName and vice versa. Not sure if the two issues are related, I suspect my regular expression extract is wrong for a number.
Jmeter setup. (5.2.1)
jmeter setUp
Thread group
+ HTTP Request
++ JSON JMESPath Extractor
+ ForEach Controller
++ Regular Expression Extractor: PersonId
++ Regular Expression Extractor: firstName
++ Regular Expression Extractor: lastName
++ BeanShell PostProcessor
getWorker returns the following payload
jsonPayload
JSON JMESPath Extractor to handle the payload.
{
"items" : [
{
"PersonId" : 398378,
"firstName" : "Sam",
"lastName" : "Shed"
},
{
"PersonId" : 398379,
"firstName" : "Bob",
"lastName" : "House"
}
],
"count" : 2,
"hasMore" : true,
"limit" : 2,
"offset" : 0,
"links" : [
{
"rel" : "self",
"href" : "https://a.site.on.the.internet.com/employees",
"name" : "employees",
"kind" : "collection"
}
]
}
JSON JMESPath Extractor Configuration
Name of created variables: items
JMESPath expressions: items
Match No. -1
Default Values: Not Found
ForEach Controller
ForEach Controller Configuration
Input variable prefix: items
Start Index: Empty
End Index: Empty
Output variable name: items
Add "_"? Checked
Each of the Regular Expression Extracts follow the same pattern as below.
Extract PersonId with Regular Expression
Apply to: Main Sample Only
Field to check: Body
Name of created variable: PersonId
Regular Expression: "PersonId":"(.+?)"
Template: $1$
Match No. Empty
Default Value: PersonId
The final step in the thread is where I write out the parsed results.
BeanShell PostProcessor
PersonNumber = vars.get("PersonNumber");
DisplayName = vars.get("DisplayName");
f = new FileOutputStream("/Applications/apache-jmeter-5.2.1/bin/scripts/getWorker/responses/myText.csv", true);
p = new PrintStream(f);
this.interpreter.setOut(p);
print(PersonId+", "+ PersonNumber+ ", " + DisplayName);
f.close();
I am new to this and looking either for someone to tell me where I screwed up or direct me to a place I can read up on the appropriate topics. (Both are fine). Thank you.
For Each Controller doesn't know the structure of items variable since it is in JSON format. It is capable of just understanding an array and traverses through them. I would suggest to move away from For Each Controller in your case and use the JSON extractor itself for all the values like below
Person ID
First Name
Last Name
Beanshell Sampler Code
import java.io.FileWriter; // Import the FileWriter class
int matchNr = Integer.parseInt(vars.get("personId_C_matchNr"));
log.info("Match number is "+matchNr);
f = new FileOutputStream("myText.csv", true);
p = new PrintStream(f);
for (int i=1; i<=matchNr; i++){
PersonId = vars.get("personId_C_"+i);
FirstName = vars.get("firstName_C_"+i);
LastName = vars.get("lastName_C_"+i);
log.info("Iteration is "+i);
log.info("Person ID is "+PersonId);
log.info("First Name is "+FirstName);
log.info("Last Name is "+LastName);
p.println(PersonId+", "+FirstName+", "+LastName);
}
p.close();
f.close();
Output File
HOW THE ABOVE ACTUALLY WORKS
When you extract values using the matchNr, it goes in a sequential order in which the response has arrived. For example, in your case, Sam & Shed appear as first occurrences and Bob & House appear as subsequent occurrences. Hence JMeter captures them with the corresponding match and stores them as 1st First Name = Sam, 2nd First Name = Bob and so on.
GENERIC STUFF
The regex expression for capturing Person ID which you have used seems to be inaccurate. The appropriate one would be
"PersonId" :(.+?),
and not
"PersonId":"(.+?)"
Move to JSR223 processors instead of Beanshell as they are more performant. Source: Which one is efficient : Java Request, JSR223 or BeanShell Sampler for my script. The migration is pretty simple. Just copy the code that you have in Beanshell and paste it in JSR223.
Close any stream or writer that is open appropriately else it might cause issues when other users are trying to write to the file during load test
In case you are planning to use this file as a subsequent input within JMeter, please note that there is a space between comma and the next element. For example, it is "Sam, Shed" and not "Sam,Shed".JMeter by default does not trim any spaces and will use the value just like that. Hence you might want to take a judicious call regarding that space
Hope this helps!
Since JMeter 3.1 you shouldn't be using Beanshell, go for JSR223 Test Elements and Groovy language for scripting.
Given Groovy has built-in JSON support you shouldn't need any extractors, you can write the data into a file in a single shot like:
new groovy.json.JsonSlurper().parse(prev.getResponseData()).items.each { item ->
new File('myText.csv') << item.get('PersonId') << ',' << item.get('firstName') << ',' << item.get('lastName') << System.getProperty('line.separator')
}
More information: Apache Groovy - Why and How You Should Use It
When I try to make the query:
query PapersFromAPoll
{
description: "retrieve all the papers from a poll"
statement:
SELECT org.acme.democracity.Paper
WHERE(poll.pollId == _$id)
}
I am not able to get any rows, here is the fragment where I make this query:
return query('PapersFromAPoll',{id : count.poll.pollId})
It is strange because when I erase the WHERE statement and I make a simple query without any parameters it works perfectly.
Paper and Poll in model.cto file:
asset Paper identified by paperId {
o String paperId
o String[] fields
--> Poll poll //Paper related with a poll
}
asset Poll identified by pollId {
o String pollId
o Ask[] asks
}
query PapersFromAPoll
{
description: "retrieve all the papers from a poll"
statement:
SELECT org.acme.democracity.Paper
WHERE(poll == _$pollId)
}
So you can filter by the Poll asset by giving a reference to that Poll asset. When you try to query with the pollId it will have to be a reference to the resource resource:org.acme.democracity.Poll#{insert_id}
I got a domain like this:
ZZPartAndTeam
String parts
String team
Parts may have many team.
For ex: part:part1 team:10
part:part1 team:20
part:part2 team:30
How can I query in the domain that get all parts which have multi team?
result:part:part1 team:10
part:part1 team:20
Thanks.
The HAVING clause is not supported by Hibernate Criteria. A way around is to use DetachedCriteria.
import org.hibernate.criterion.DetachedCriteria as HDetachedCriteria
query: { builder, params ->
// This query counts the number of teams per part
HDetachedCriteria innerQry = HDetachedCriteria.forClass(ZZPartAndTeam.class)
innerQry.setProjection(Projections.projectionList()
.add(Projections.count('team').as('teamCount'))
)
innerQry.add(HRestrictions.eqProperty('part', 'outer.part')
// Using innerQuery, this criteria returns the parts having more than one team.
HDetachedCriteria outerQry = HDetachedCriteria.forClass(ZZPartAndTeam.class, 'outer')
outerQry.setProjection(Projections.projectionList()
.add(Projections.distinct(Projections.property('part').as('part')))
)
outerQry.add(Subqueries.gt(1, innerQry))
builder.addToCriteria(Property.forName('part').in(outerQry))
}
Im currently researching how to port the Data Access Layer of an existing .NET 4.0 MVC 3 web application over to an entity framework. There are many reasons, but the primary one being due to thousands of stored procedures, adding just 1 field to a table results in 30 - 50 sproc edits!!
We are using MS SQL Server 2008 R2 and, ideally, we would like to use NHibernate and Fluent for mapping.
I have simplified the problem Im having into a simple example:
Imagine the following 2 tables:
'Products' Table
ID (INT)
DefaultName (NVARCHAR(128))
'Product Names' Table
ProductID (INT)
Name (NVARCHAR(128))
Culture (VARCHAR(10))
The Products table will contain a list of products, each of them will have a default, English, name. The Product Names table will contain the ID of the Product and many translations.
Currently, using stored procedures, we have the following:
SELECT Products.ID,
ISNULL(ProductNames.Name, Products.DefaultName) AS Name
FROM Products
LEFT JOIN ProductNames ON ProductNames.ProductID = Products.ID AND ProductNames.Culture = #Culture;
Note: #Culture is passed into the procedure
This always ensures a single Product with either a localised name or default (English) name is returned.
My question is: Is this possible to do at the Mapping level of Fluent NHibernate? I have been searching for days on 'How to join on 2 columns', but cant find a solution which works. It would seem odd if this is not possible in such a mature framework?
As an example of what I have been experimenting with:
public class ProductMap : ClassMap<Product> {
public ProductMap() {
Id(p => p.Id);
Join("ProductNames", pn => {
pn.Optional()
.KeyColumn("ProductID")
.Map(p => p.Name);
});
}
}
However, this results in the following exception:
More than one row with the given identifier was found: 109, for class: Product
This is because product 109 has 5 translations and thus all 5 cannot be mapped to a single string.
I have managed to use the 'HasMany<>' method to map all translations into a List within a Product. However, this is not what I need.
if the name is readonly then
public class ProductMap : ClassMap<Product> {
public ProductMap() {
Id(p => p.Id);
Map(p => p.Name).Formula("Select ISNULL(pn.Name, DefaultName) FROM ProductNames pn WHERE pn.ProductID = ID AND pn.Culture = '" + GetCUltureFromSomewhere() + "'");
}
}
edit #2: Question solved halfways. Look below
As a follow-up question, does anyone know of a non-intrusive way to solve what i'm trying to do below (namely, linking objects to each other without triggering infinite loops)?
I try to create a asp.net-mvc web application, and get a StackOverFlowException. A controller triggers the following command:
public ActionResult ShowCountry(int id)
{
Country country = _gameService.GetCountry(id);
return View(country);
}
The GameService handles it like this (WithCountryId is an extension):
public Country GetCountry(int id)
{
return _gameRepository.GetCountries().WithCountryId(id).SingleOrDefault();
}
The GameRepository handles it like this:
public IQueryable<Country> GetCountries()
{
var countries = from c in _db.Countries
select new Country
{
Id = c.Id,
Name = c.Name,
ShortDescription = c.ShortDescription,
FlagImage = c.FlagImage,
Game = GetGames().Where(g => g.Id == c.GameId).SingleOrDefault(),
SubRegion = GetSubRegions().Where(sr => sr.Id == c.SubRegionId).SingleOrDefault(),
};
return countries;
}
The GetGames() method causes the StackOverflowException:
public IQueryable<Game> GetGames()
{
var games = from g in _db.Games
select new Game
{
Id = g.Id,
Name = g.Name
};
return games;
}
My Business objects are different from the linq2sql classes, that's why I fill them with a select new.
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
edit #1: I have found the culprit, it's the following method, it triggers the GetCountries() method which in return triggers the GetSubRegions() again, ad nauseam:
public IQueryable<SubRegion> GetSubRegions()
{
return from sr in _db.SubRegions
select new SubRegion
{
Id = sr.Id,
Name = sr.Name,
ShortDescription = sr.ShortDescription,
Game = GetGames().Where(g => g.Id == sr.GameId).SingleOrDefault(),
Region = GetRegions().Where(r => r.Id == sr.RegionId).SingleOrDefault(),
Countries = new LazyList<Country>(GetCountries().Where(c => c.SubRegion.Id == sr.Id))
};
}
Might have to think of something else here :) That's what happens when you think in an OO mindset because of too much coffee
Hai! I think your models are recursively calling a method unintentionally, which results in the stack overflow. Like, for instance, your Subregion object is trying to get Country objects, which in turn have to get Subregions.
Anyhow, it always helps to check the stack in a StackOverflow exception. If you see a property being accessed over and over, its most likely because you're doing something like this:
public object MyProperty { set { MyProperty = value; }}
Its easier to spot situations like yours, where method A calls method B which calls method A, because you can see the same methods showing up two or more times in the call stack.
The problem might be this: countries have subregions and subregions have countries. I don't know how you implement the lazy list, but that might keep calling GetCountries and then GetSubRegions and so on. To find that out, I would launch the debugger en set breakpoints on the GetCountries and GetSubRegions method headers.
I tried similar patterns with LinqToSql, but it's hard to make bidirectional navigation work without affecting the performance to much. That's one of the reasons I'm using NHibernate right now.
To answer your edited question, namely: "linking objects to each other without triggering infinite loops":
Assuming you've got some sort of relation where both sides need to know about the other... get hold of all the relevant entities in both sides, then link them together, rather than trying to make the fetch of one side automatically fetch the other. Or just make one side fetch the other, and then fix up the remaining one. So in your case, the options would be:
Option 1:
Fetch all countries (leaving Subregions blank)
Fetch all Subregions (leaving Countries blank)
For each Subregion, look through the list of Countries and add the Subregion to the Country and the Country to the Subregion
Option 2:
Fetch all countries (leaving Subregions blank)
Fetch all Subregions, setting Subregion.Countries via the countries list fetched above
For each subregion, go through all its countries and add it to that country
(Or reverse country and subregion)
They're basically equialent answers, it just changes when you do some of the linking.