Is it possible to use cockroach gen_random_uuid() function inside JSON data while inserting into JSON datatype in cockroachDB - cockroachdb

I am new to cockroach DB and was wondering if the below ask is possible
One of the columns in my table is of JSON type and the sample data in it is as follows
{
"first_name": "Lola",
"friends": 547,
"last_name": "Dog",
"location": "NYC",
"online": true,
"Education": [
{
"id": "4ebb11a5-8e9a-49dc-905d-fade67027990",
"UG": "UT Austin",
"Major": "Electrical",
"Minor": "Electronics"
},
{
"id": "6724adfa-610a-4efe-b53d-fd67bd3bd9ba",
"PG": "North Eastern",
"Major": "Computers",
"Minor": "Electrical"
}
]
}
Is there a way to replace the "id" field in JSON as below to get the id generated dynamically?
"id": gen_random_uuid(),

Yes, this should be possible. To generate JSON data that includes a randomly-generated UUID, you can use a query like:
root#:26257/defaultdb> select jsonb_build_object('id', gen_random_uuid());
jsonb_build_object
--------------------------------------------------
{"id": "d50ad318-62ba-45c0-99a4-cb7aa32ad1c3"}
If you want to update in place JSON data that already exists, you can use the jsonb_set function (see JSONB Functions).

Related

How to compare JSON data against DB data in JMeter

I want to compare JSON response with DB data in JMeter. Below is Db table structure (Oracle database):
Table Structure
Below is the JSON
{
"page": 2,
"per_page": 6,
"total": 12,
"total_pages": 2,
"data": [
{
"id": 7,
"email": "michael.lawson#reqres.in",
"first_name": "Michael",
"last_name": "Lawson",
"avatar": "https://reqres.in/img/faces/7-image.jpg"
},
{
"id": 8,
"email": "lindsay.ferguson#reqres.in",
"first_name": "Lindsay",
"last_name": "Ferguson",
"avatar": "https://reqres.in/img/faces/8-image.jpg"
},
{
"id": 9,
"email": "tobias.funke#reqres.in",
"first_name": "Tobias",
"last_name": "Funke",
"avatar": "https://reqres.in/img/faces/9-image.jpg"
},
{
"id": 10,
"email": "byron.fields#reqres.in",
"first_name": "Byron",
"last_name": "Fields",
"avatar": "https://reqres.in/img/faces/10-image.jpg"
},
{
"id": 11,
"email": "george.edwards#reqres.in",
"first_name": "George",
"last_name": "Edwards",
"avatar": "https://reqres.in/img/faces/11-image.jpg"
},
{
"id": 12,
"email": "rachel.howell#reqres.in",
"first_name": "Rachel",
"last_name": "Howell",
"avatar": "https://reqres.in/img/faces/12-image.jpg"
}
],
"support": {
"url": "https://reqres.in/#support-heading",
"text": "To keep ReqRes free, contributions towards server costs are appreciated!"
}
}
Below is the configuration I have made in JMeter
JDBC Request
JSON Extractor
Response Assertion
But I am getting null response:
Assertion Result
what mistake am I making?
I want to compare each cell data with JSON data. For example ID=7 with JSON and ID=9 with JSON and so on.
Is this possible to do this with any scripting language in JMeter, if yes please suggest the code for the same.
There are multiple problems with your setup:
You need to change your SQL query to SELECT ID from HHDIXI.Data, otherwise you will get LAST_NAME entries in the ID variable
You need to change your JSONPath query to $..data[*].id and set "Match No" to -1 otherwise you will get only first ID into idfromAPI variable
Use Debug Sampler and View Results Tree listener combination to see what variables are being generated by the HTTP and JDBC request samplers.
If you want to compare each ID from API to each ID from the DB in one shot the only option is JSR223 Assertion
Example code:
1.upto(vars.get('ID_#') as int, { index ->
def expected = vars.get('ID_' + index)
def actual = vars.get('idfromAPI_' + index)
if (expected != actual) {
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage('Data mismatch, in the database: ' + expected + ', in the API: ' + actual)
}
})
and demo:

Laravel data recording with API

I save the received data from the API to the database. In my API result, there is the following answer:
"opponents": [
{
"opponent": {
"acronym": "RH",
"id": 127276,
"image_url": "/image/127276/reverse_heaven_logo_std.png",
"location": null,
"modified_at": "2020-04-14T19:03:48Z",
"name": "Reverse Heaven",
"slug": "reverse-heaven"
},
"type": "Team"
},
{
"opponent": {
"acronym": " neon",
"id": 2061,
"image_url": "/image/2061/neon_esport.png",
"location": "PH",
"modified_at": "2020-04-14T19:04:02Z",
"name": "Neon Esports",
"slug": "neon-esports"
},
"type": "Team"
}
],
If i use var_dump($opponents), i receive only last opponent from API. How can I save exactly 2 values that are in the array under the same name opponent->name?
Since you are using laravel, you may use collections.
$decoded = json_decode($opponents);
return collect($decoded->opponents)->pluck('opponent.name')->toArray();
you may do it in a single line if you prefer;
return collect(json_decode($opponents)->opponents)->pluck('opponent.name')->toArray();
first decode the json like this
$decoded_opponents = json_decode($opponents);
then create a empty array
$array = [];
run a foreach loop on you decoded data
foreach($decoded_opponents->opponents as $key=>$value){
$array[]=$value->opponent->name;
}
the names now stored in $array

How to extract multiple json values from a Json response

I am trying to extract multiple values from a JSON response on my jmeter script. Below is sample of my response:
{
"startDate": "2018-12-10T15:36:34.400+0000",
"userId": "7211111-2fa90",
"createdBy": "TEST",
"note": {
"content": "Application Submitted "
},
"Type": "SUBMITTED"
},
"currentEventState": "CLOSED",
{
"Xxxx": "test",
"Loc": null,
"Zipcode": [],
"Locality": 82,
"Address": {
"Add": 12302,
"Add2": "place",
"Zip": {
"Phone": "home",
"Email": "test#test.com"
}
},
"state": "MD",
"Cost": "E "
},
"AppID": "cd8d98e6-c2a79",
"Status": "CLOSED",
}
I am trying to extract userid and AppID for the case if the TYPE is Submitted and Status is Closed.I tried using the Json extractor with $.[?(#.Type=="SUBMITTED")].[*].?(#.Status=="CLOSED").userid,APPID, but couldn't get the expected result. Could anyone guide me on this.
You need to use an inline predicate to combine 2 clausees and a semicolon in order to store results into 2 separate JMeter Variables.
Add JSON Extractor as a child of the request which returns above JSON
Configure it as follows:
Names of created variables: userid;appid
JSON Path Expressions: $..[?(#.Type=='SUBMITTED' && #.Status == 'CLOSED')].userId; $..[?(#.Type=='SUBMITTED' && #.Status == 'CLOSED')].AppID
Default values: NA;NA
Here is the demo of single expression working fine:
And here are extracted values reported by the Debug Sampler:

ElasticSearch URI Search null field

I need to create a query via URI to filter all data between two dates and also if this date field is null.
For example:
I have the field "creation_date" in some objects, however I want that in the resulting also does not appear the objects that the field does not have.
I tried something similar below:
http://localhost//elasticsearch/channels/channel/_search?q=channel.schedule.creation_date:[2018-06-19 TO 2018-12-22] OR channel.schedule.creation_date: NULL
As far as comparing the dates is OK, it works. The problem is to get the NULL values.
Edited
Source sample:
"_source": {
"channel": {
"activated": false,
"approved": false,
"content": "Jvjv",
"creation_date": "2018-06-21T13:06:10.000Z",
"facebookLink": "J jv",
"id": "Kvjvjv",
"instagramId": "Jvjv",
"name": "Kbkbkvk",
"ownerId": "sZtxdhiNbNY9sr2DtiCzlgJfsqb2",
"plan": 0,
"purpose": "Jvjv",
"recurrence": 1,
"segment": "Jvjvjv",
"twitterId": "Jvjv",
"youtubeId": "Jvj"
}
}
}
You can do this using the NOT(_exists_:field_name) constraint:
Can you try this ?
http://localhost//elasticsearch/channels/channel/_search?q=channel.schedule.creation_date:[2018-06-19 TO 2018-12-22] OR NOT(_exists_:channel.schedule.creation_date)

How to optimize repetitive data in elasticsearch index

I am inserting very huge JSON in elastic search index. The problem is how can we optimize JSON in elasticsearch index.
e.g my json format looks like:
{
"#context": "Context",
"#type": "SessionEvent",
"actor": {
"#context": "Context",
"#id": "554433",
"#type": "Person",
"name": null,
"description": null,
"extensions": {},
"dateCreated": "2015-08-01T06:00:00.000Z",
"dateModified": "2015-09-02T11:30:00.000Z"
},
"action": "action#LoggedIn",
"object": {
"#context": "Context",
"#id": "viewer",
"#type": "SoftwareApplication",
"name": "ePub",
"description": null,
"extensions": {},
"dateCreated": "2015-08-01T06:00:00.000Z",
"dateModified": "2015-09-02T11:30:00.000Z"
}
}
These type of JSON insert into elasticsearch in every seconds/minutes with the same actor or different actor with different actions.
So how can i store these information in elastic search?
Currently storing as it is in elasticsearch?
Or can we store repetitive part of JSON in other index and put it's reference in the above JSON?
Please suggest which approach would be better.

Resources