How to solve the following Oracle NoSQL error (The query doesn't contain the variable)? - oracle-nosql

I have the following error when trying to do a query using the API calls
{
"code": "InvalidParameter",
"message": "The query doesn't contain the variable: id"
}
Please, ci-below the payload and the call executed
oci-curl nosql.eu-frankfurt-1.oci.oraclecloud.com post ./request.json "/20190828/query" | jq
[opc#instance-dev ~]$ cat request.json
{
"compartmentId": "ocid1.xxxxxx"
, "statement": "DECLARE $id LONG; SELECT * FROM test_table where id > $id"
, "variables" : {"id":"10"}
}

Variables must be the same as in DECLARE statement including the $ and same types.
In you case, the good name for the variable is $id and not id
"variables" : {"$id":10}

Related

Validate the JDBC request with a user defined variable

I am new to JMeter,
In my test I am creating a JDBC Connection to oracle DB and running a query which fetching me the count of records, which I want to validate must be equal to the SAMPLE-NUMBER (which is a defined variable in the user defined variable).
SELECT COUNT(*) FROM event_log WHERE audit_context_key LIKE '288017ec-0dcf-4fd5-9565-e8ad15e65cd2' AND event_desc = 'Success'
Response Body:
COUNT(*)
2
UserDefined Variable
You can do this, defined the variable name in JDBC request,
for example, TOTALCOUNT and add a JSR223 Assertion with the following code,
1.upto(vars.get('TOTALCOUNT_#') as int, {
if (vars.get('TOTALCOUNT_' + it) == '${__groovy(vars.get('SAMPLE-NUMBER'),)}') {
AssertionResult.setFailure(false);
}
})
Response Assertion can do the trick for you:
In the JDBC Request define "Variable Names", i.e. ACTUAL_COUNT
Once done you can compare the ACTUAL_COUNT variable value with the SAMPLE-NUMBER variable like:

Oracle NoSQL database : How to import json documents in a specific column?

I have a question about the import command in the Oracle NoSQL Database - SQL shell tool. I am wondering if there is an option to read a file and put the data in a JSON column instead of put the data in a table. Currently, it is matching the fields from the JSON file to the columns in the table. Something like
import -table <table_name> - column <myjsoncolumn> -file <myfile>
Here an example - simplified version
sql-> create table stat (reportTime long, reportTimeHuman string , primary key (reportTime));
Statement completed successfully
sql-> import -table stat -file file.json
Loaded 736 rows to stat.
sql-> select * from stat limit 5;
{"reportTime":1624370080000,"reportTimeHuman":"2021-06-22 13:54:40.000 UTC"}
{"reportTime":1624366760000,"reportTimeHuman":"2021-06-22 12:59:20.000 UTC"}
{"reportTime":1624368660000,"reportTimeHuman":"2021-06-22 13:31:00.000 UTC"}
{"reportTime":1624370980002,"reportTimeHuman":"2021-06-22 14:09:40.002 UTC"}
I want to do
CREATE TABLE IF NOT EXISTS stat
( id INTEGER GENERATED ALWAYS AS IDENTITY, myJson JSON, PRIMARY KEY (id))
import -table stat -column myJSON -file file.json
sql-> select myJson from stat limit 5;
{"reportTime":1624370080000,"reportTimeHuman":"2021-06-22 13:54:40.000 UTC"}
{"reportTime":1624366760000,"reportTimeHuman":"2021-06-22 12:59:20.000 UTC"}
{"reportTime":1624368660000,"reportTimeHuman":"2021-06-22 13:31:00.000 UTC"}
{"reportTime":1624370980002,"reportTimeHuman":"2021-06-22 14:09:40.002 UTC"}
I am expecting to have the json documents in the myJson column. id is a generated number in this case.
No, there is no such option using the shell but the migrator tool can do this
Step 1: create your table
Step 2: create the following file ./migrator-export.json
{
"source" : {
"type" : "file",
"format" : "json",
"dataPath" : "/data/test/kvstore_export/"
},
"sink" : {
"type" : "nosqldb",
"storeName" : "OUG",
"helperHosts" : ["localhost:5000"],
"table" : "stat",
"requestTimeoutMs" : 5000
},
"transforms": {
"aggregateFields" : {
"fieldName" : "myJson",
"skipFields" : []
}
},
"abortOnError" : true,
"migratorVersion" : "1.0.0"
}
Step 3; execute the command Migrator using the previous configuration file.
You can also follow the wizard
Would you like to create table as part of migration process?
Use this option if you want to create table through the migration tool.
If you select yes, you will be asked to provide a file that contians table DDL or to use default schema.
(y/n) (n): y
We identified source as file.
Would you like to use below default schema?
CREATE TABLE IF NOT EXISTS stat(id LONG GENERATED ALWAYS AS IDENTITY(CACHE 5000), document JSON, PRIMARY KEY(SHARD(id)))
Where 'id' will be auto generated and 'document' is all the
fields aggregated into one JSON column.
Please note that tool will internally create table with above mentioned schema.
For aggregation below transforms are applied
internally by the tool."transforms" : {
"aggregateFields" : {
"fieldName" : "document"
}
}
(y/n) (n): y

Trouble passing array variable to BigQuery query from shell

I'm trying to run parametrized queries from a shell script and I need to be able to provide parameters.
This is the command I'm trying to run :
bq --location=US query --use_legacy_sql=False \
--parameter='gender::M' \
--parameter='states:ARRAY<STRING>:["WA", "WI", "WV", "WY"]' \
'SELECT name, sum(number) as count
FROM `bigquery-public-data.usa_names.usa_1910_2013`
WHERE gender = #gender
AND state IN UNNEST(#states)
GROUP BY name
ORDER BY count DESC
LIMIT 10;'
If you copy/paste as it is in a shell, it works.
Now, this is what I'm trying to run :
In a file script.sh
runQuery(){
states="'$1'"
bq --location=US query --use_legacy_sql=False --parameter='gender::M' --parameter=$states \
'SELECT name, sum(number) as count
FROM `bigquery-public-data.usa_names.usa_1910_2013`
WHERE gender = #gender
AND state IN UNNEST(#states)
GROUP BY name
ORDER BY count DESC
LIMIT 10;'
}
runQuery 'states:ARRAY<STRING>:["WA", "WI", "WV", "WY"]'
But this doesn't work.
Here's the error log :
I am pretty sure it has to do with the fact that the parameter has both single quotes and double quotes and at some points something goes wrong when processing 'states:ARRAY:["WA", "WI", "WV", "WY"]' but I just can't find how to solve this.
Thanks in advance !
Found the error :
Changed runQuery 'states:ARRAY<STRING>:["WA", "WI", "WV", "WY"]'
To runQuery 'states:ARRAY<STRING>:["WA","WI","WV","WY"]'
Note that blank spaces were removed from the array.

Composer query to match a participant reference

So I have a query like this:
query selectOrder{
description: "Select an Order that matches a Client reference and an Order Number"
statement:
SELECT com.x.Order
WHERE (client == _$client AND orderNumber == _$orderNumber)
}
The order is something like this:
asset Order identified by uuid {
o String uuid
--> Client client
o String orderNumber
--> Item[] items
}
How do I pass the reference to the client to the query?
I tried the reference and was told to toJSON it.
I tried that and it won't parse the thing - there's a clear issue with the parsing of the query.
I can't find the answer in the docs, so I'm wondering if anyone has done this or if I have to save the client id instead of the reference to client and lose the integrity.
EDIT: For completeness for the first answer below.
I'm trying to add an Item to the array of Items.
My Item object is defined like this:
asset Item identified by uuid {
o String uuid
o DateTime timestamp
o String orderNumber
--> Client client
o String[] message
}
When the transaction is invoked the single object passed in is the Item.
I'm setting Item.client as the _$client value in the query.
Should I be pre-pending it with "resource:"?
I'm asking because I thought that was in the reference string already - at least it is in the view in the playground.
EDIT2:
So I manually construct the following variable:
var RSRC = 'resource:com.x.Client#XYZ123'
Set that as the client in this query
return query('selectOrder', {agency : RSRC, orderNumber : orderNumber});
But I'm still getting this:
Error: unknown operator "0" - should be one of $eq, $lte, $lt, $gt,
$gte, $exists, $ne, $in, $nin, $size, $mod, $regex, $elemMatch, $type
or $all
What next?
Embedding the "resource..." string in quotes didn't work either.
Your query looks ok, but you need to pass a string with the format:
resource:type.Name#instance for the relationship.
E.g. resource:org.acme.Car#123ABC

How to write a query with two ? placeholders in sequence?

I am using a NamedParameterJdbcTemplate, but found that this problem is in the underlying JdbcTemplate class, so I will show the problem as it occurs with the JdbcTemplate (so let's not worry about the safety of the SQL query here).
Here's what I am trying to achieve:
String sql = "SELECT * FROM clients ORDER BY ? ?";
return jdbcTemplate.query(sql,
new Object[] { "name", "ASC" },
new ClientResultSetExtractor());
I expected the first place-holder to be replaced with "name" and the second with "ASC", which would create the valid SQL query:
SELECT * FROM clients ORDER BY name ASC
But unfortunately, running that jdbc query does not work:
ERROR: syntax error at or near "$2" at character 35
STATEMENT: SELECT * FROM clients ORDER BY $1 $2
What am I doing wrong?
EDIT
I had assumed the problem was the two placeholders in sequence, but even when I remove the first one, it still won't accept just the last one, which should tell the query whether to sort in ASC or DESC order. Is this a bug, and if not, why the heck is this not acceptable????
You're trying to use parameters incorrectly.
Parameters are not column names or SQL statement keywords. They're data content (eg., WHERE LastName = ? is a valid parameterized statement, WHERE ? = 'Smith' is not).

Resources