Script writing in java for elasticsearch metric aggregation - elasticsearch-aggregation

I want to write following elasticsearch aggregation query in java. I am not able to write script for this in java , please help
"aggs":{
"AVG_MEMORY_USAGE": {
"avg": {
"script": {"lang": "expression", "source": "doc['ProcessingTimeInMilliSecs']/doc['timezone']" }
}
}
}

Found a way to do this:
Script script = new
Script("doc['ProcessingTimeInMilliSecs'].value/doc['timezone'].value");
AvgAggregationBuilder agg =
AggregationBuilders.avg("AVG_MEMORY_USAGE").script(script);
Avg avg = agg.getAggregations().get("AVG_MEMORY_USAGE");
String avgValue = avg.getValue();
script uses painless language as a default so I did not explicitly mention the lang parameter in "script"

Related

Karate - String concatenation of JSON value with a variable [duplicate]

The embedded expressions are not replaced when appended, prepended or surrounded by characters in the following simplified and very basic scenario:
* def jobId = '0001'
* def out =
"""
{
"jobId": "#(jobId)",
"outputMetadata": {
"fileName_OK": "#(jobId)",
"fileName_Fail_1": "some_text_#(jobId)",
"fileName_Fail_2": "#(jobId)-and-some-more-text",
"fileName_Fail_3": "prepend #(jobId) and append"
}
}
"""
* print out
Executing the scenario returns:
{
"jobId": "0001",
"outputMetadata": {
"fileName_OK": "0001",
"fileName_Fail_1": "some_text_#(jobId)",
"fileName_Fail_2": "#(jobId)-and-some-more-text",
"fileName_Fail_3": "prepend #(jobId) and append"
}
}
Is it a feature, a limitation, or a bug? Or, did I miss something?
This is as designed ! You can do this:
"fileName_Fail_2": "#(jobId + '-and-some-more-text')"
Any valid JS expression can be stuffed into an embedded expression, so this is not a limitation. And this works only within JSON string values or when the entire RHS is a string within quotes and keeps the parsing simple. Hope that helps !

DBArrayList to List<Map> Conversion after Query

Currently, I have a SQL query that returns information to me in a DBArrayList.
It returns data in this format : [{id=2kjhjlkerjlkdsf324523}]
For the next step, I need it to be in a List<Map> format without the id: [2kjhjlkerjlkdsf324523]
The Datatypes being used are DBArrayList, and List.
If it helps any, the next step is a function to collect the list and then to replace all single quotes if any [SQL-Injection prevention]. Using:
listMap = listMap.collect() { "'" + Util.removeSingleQuotes(it) + "'" }
public static String removeSingleQuotes(s) {
return s ? s.replaceAll(/'"/, '') : s
}
I spent this morning working on it, and I found out that I needed to actually collect the DBArrayList like this:
listMap = dbArrayList.collect { it.getAt('id')}
If you're in a bind like I was and restrained to a specific schema this might help, but #ou_ryperd has the correct answer!
While using a DBArrayList is not wrong, Groovy's idiom is to use the db result as a collection. I would suggest you use it that way directly from the db:
Map myMap = [:]
dbhandle.eachRow("select fieldSomeID, fieldSomeVal from yourTable;") { row ->
map[row.fieldSomeID] = row.fieldSomeVal.replaceAll(/'"/, '')
}

Run SQL Query in Phonegap

Hello everyone i am trying to run a query as follows, but i always get "SQL ERROR: undefined"
What am i doing wrong.
db = window.openDatabase("Database", "1.0", "SQLDB", 200000);
RunQuery ("DROP TABLE IF EXISTS ARTIGOS");
function RunQuery(QueryExecute) {
db.transaction(function(transaction){
transaction.executeSql(QueryExecute,successCB,errorCB);
})
}
function errorCB(err) {
alert("SQL Error: "+err.message);
}
function successCB() {
alert("SQL OK");
}
One possibility is that the transaction.executeSql method takes a parameters array as its second argument. So to use the callbacks like you have, you may have to pass in an empty array for the parameters. e.g.:
transaction.executeSql(QueryExecute, [], successCB, errorCB);
Referenced from the Cordova docs here:
https://cordova.apache.org/docs/en/latest/cordova/storage/storage.html

How to configure a timeout for an SQL query in Groovy?

How do I create a timeout for this operation: ?
def db = Sql.newInstance("jdbc:mysql://${mysql_host}:3306/${dbName}",
user, pass, 'com.mysql.jdbc.Driver')
db.eachRow(query) { row ->
// do something with the row
}
I believe the correct way would be something like this:
sql = Sql.newInstance("jdbc:oracle:thin:#localhost:1521:XE", "user",
"pwd", "oracle.jdbc.driver.OracleDriver")
sql.withStatement {
stmt -> stmt.queryTimeout = 10
}
sql.eachRow("select * from someTable", {
println it
} )
of course, this is where I'd used Oracle, but I hope this can give you an idea.
I believe there might not be a general answer, but rather a database/driver-specific answer via parameters to the connection URL.
E.g. for mysql, I think that adding connectTimeout=something&socketTimeout=something might do the trick.

If condition not working in Bean shell sampler

I am writing following script in Bean Shell sampler but it is not executed properly,
JMeter never enters in 'if' condition, what I m doing wrong?
*WRIDTEMP is a variable, WRId is a variable having value retrieved from a csv file.
if((vars.get("WRIDTEMP")==vars.get("WRId")) || vars.get("WRIDTEMP")==0)
{
String i = vars.get("C");
int counter = Integer.parseInt(i);
counter++;
vars.put("C", "" + counter);
if(counter<10 )
{
vars.put("Message",temp+authString);
}
}
You are comparing String using == , you must use .equals() method to compare them.
to compare to 0, you should do .equals("0")
I think I can confirm this issue.
I'm not sure if it's a bug or not, but what I have found is that when running this code:
String jira_version = vars.get("jiraVersion");
if (jira_version =="7") ) {
vars.put("closeIssueTitle_local","Done");
vars.put("isJIRA6_local","false");
vars.put("isJIRA7_local","true");
} else if ( jira_version.equals("6") ) {
vars.put("closeIssueTitle_local","Close Issue");
vars.put("isJIRA6_local","true");
vars.put("isJIRA7_local","false");
} else {
vars.put("closeIssueTitle_local","CLOSEISSUETITLE_BEANSHELL_FAILURE");
vars.put("isJIRA6_local","ERROR");
vars.put("isJIRA7_local","ERROR");
}
Where the value of jira_version is either literally 6 or 7, then the if condition always evaluates to it's last, no-match case.
when I change my evaluation condition to
if ( jira_version.equals("6") ) {
then it evaluates as expected.
Here's the rub for me. When I run this in the standalone beanshell environment, bsh-2.0b5.jar for example, it my code example works as expected. It's only within JMeter that I have to rely on .equals("X").
This does feel a bit like a bug.
This seems to be a bug with beanshell interpreter in jmeter. The regular beanshell shell does support string comparison using == . In fact, it is one of the features of beanshell.