KSQL Array Of Object - ksqldb

I'm trying to fetch data from Object Array with ksql Any one Can Helps Me :)
i have JSON Message
"Total": [
{
"TotalType": "Standard",
"TotalAmount": 15.99
}
i write this but it dosn't work
create stream test2 (Total Array<STRUCT>< <TotalAmount Double>) with(KAFKA_TOPIC='hermes__output',VALUE_FORMAT='JSON');
create stream test2 (Total[0] STRUCT<TotalAmount Double>) with(KAFKA_TOPIC='hermes__output',VALUE_FORMAT='JSON');
thx

What you want is:
create stream test2
(Total Array<STRUCT< TotalType STRING, TotalAmount Double >>)
with(KAFKA_TOPIC='hermes__output',VALUE_FORMAT='JSON');

Related

Foreach controller not working for Json data

I have below json
[
{
"id": 13059,
"xid": "81f4278a53fe4c1a8d03346a34d76a47",
"yid": "8cbefbadec52ced5:e4f6801:17c73aeaf21:-5a75"
},
{
"id": 13059,
"xid": "061ef5792e8f4603bb1f86c71e6fb16c",
"yid": "8cbefbadec52ced5:e4f6801:17c73aeaf21:-5a56"
},
{
"id": 13061,
"xid": "4290987b25b34ffbb5b0329f1ab1b673",
"yid": "8cbefbadec52ced5:e4f6801:17c73aeaf21:-5dd4"
},
{
"id": 13063,
"xid": "57c4a2790aa44376be1e5215c5cb7ad0",
"yid": "8cbefbadec52ced5:e4f6801:17c73aeaf21:-585e"
}
]
Now I put the above json in global variable in JSR223 sampler
vars.put("jsonVariable",JSON.stringify(aboveJson))
Now next I add "ForEach Controller" and add
InputVariable Prefix = jsonVariable
Output variable name = outVariable
Then I add another JSR223 sampler inside ForEach Controller and log below data
log.info(${outVariable})
But ForEach controller is not even executing for once. Please let me know where I did wrong
ForEach Controller isn't smart enough to parse your JSON structure, it iterates an individual set of JMeter Variables which looks like:
var_1=foo
var_2=bar
etc.
so depending on what you're trying to achieve you need to:
In case if you want individual values of id, xid and/or yid - add 1 or more JSON Extractors and configure them to fetch the values from the response. Then use ForEach Controller and feed the variable from JSON Extractor to it
If you want to iterate JSON Objects instead you can use JSR223 PostProcessor and the following Groovy code:
new groovy.json.JsonSlurper().parse(prev.getResponseData()).eachWithIndex { entry, int index ->
vars.put('json_' + (index + 1), new groovy.json.JsonBuilder(entry).toString())
}

How to declare array inside array in yaml file

for example : rate:
{floor: '1', functionId: BDEB1,
baseRates: { baseRateAgreementLevel: baseRateAgreementLevel,name: LIBOR GBP 1 Month, value: 0.1}
}
Above rate is a array and baserate is another array which is inside rate array
Getting error "Cannot create property" while declaring array inside array in snake yaml file.
I'm guessing you meant to write:
rate:
floor: '1'
functionId: BDEB1
baseRates:
baseRateAgreementLevel: baseRateAgreementLevel
name: LIBOR GBP 1 Month
value: 0.1
Is this what you wanted? Take a look here for a json equivalent representation.

How store data to array from json

I have a json file in the format as follows:
{
"recipe_type": [
"vegetarian",
"non-vegetarian"
],
"recipe_times": [
"<30min",
"30-60min",
"60-90min"
],
"recipe_cuisines": [
"arabian",
"european",
"Indian",
"chinese"
]
}
I need to store each list to separate array:
#recipe = new JS.M.Recipe()
#recipe.fetch(#recipe.getRecipeType())
#recipeTypes = #recipe.getRecipeType()//Here data is getting as objetcs..if i use alert to print this. It showing output like "[object Object]"..
I want to store data as follows:
#recipe_type = #recipeTypes.get("recipe_type")
#recipe_times = #recipeTypes.get("recipe_times")//This is wrong
How can I achieve what I want?
If you are using backbone then you must be having underscorejs loaded on the page. If yes, this should work for you
lists = _.values(data)
where data is the JSON object mentioned above.

Aerospike: how to bulk load a list of integers into a bin?

I'm trying to use the Aerospike bulk loader to seed a cluster with data from a tab-separated file.
The source data looks like this:
set key segments
segment 123 10,20,30,40,50
segment 234 40,50,60,70
The third column, 'segments', contains a comma separated list of integers.
I created a JSON template:
{
"version" : "1.0",
"input_type" : "csv",
"csv_style": { "delimiter": " " , "n_columns_datafile": 3, "ignore_first_line": true}
"key": {"column_name":"key", "type": "integer"},
"set": { "column_name":"set" , "type": "string"},
"binlist": [
{"name": "segments",
"value": {"column_name": "segments", "type": "list"}
}
]
}
... and ran the loader:
java -cp aerospike-load-1.1-jar-with-dependencies.jar com.aerospike.load.AerospikeLoad -c template.json data.tsv
When I query the records in aql, they seem to be a list of strings:
aql> select * from test
+--------------------------------+
| segments |
+--------------------------------+
| ["10", "20", "30", "40", "50"] |
| ["40", "50", "60", "70"] |
+--------------------------------+
The data I'm trying to store is a list of integers. Is there an easy way to convert the objects stored in this bin to a list of integers (possibly a Lua UDF) or perhaps there's a tweak that can be made to the bulk loader template?
Update:
I attempted to solve this by creating a Lua UDF to convert the list from strings to integers:
function convert_segment_list_to_integers(rec)
for i=1, table.maxn(rec['segments']) do
rec['segments'][i] = math.floor(tonumber(rec['segments'][i]))
end
aerospike:update(rec)
end
... registered it:
aql> register module 'convert_segment_list_to_integers.lua'
... and then tried executing against my set:
aql> execute convert_segment_list_to_integers.convert_segment_list_to_integers() on test.segment
I enabled some more verbose logging and notice that the UDF is throwing an error. Apparently, it's expecting a table and it was passed userdata:
Dec 04 2015 23:23:34 GMT: DEBUG (udf): (udf_rw.c:send_result:527) FAILURE when calling convert_segment_list_to_integers convert_segment_list_to_integers ...rospike/usr/udf/lua/convert_segment_list_to_integers.lua:2: bad argument #1 to 'maxn' (table expected, got userdata)
Dec 04 2015 23:23:34 GMT: DEBUG (udf): (udf_rw.c:send_udf_failure:407) Non-special LDT or General UDF Error(...rospike/usr/udf/lua/convert_segment_list_to_integers.lua:2: bad argument #1 to 'maxn' (table expected, got userdata))
It seems that maxn isn't an applicable method to a userdata object.
Can you see what needs to be done to fix this?
To convert your lists with string values to lists of integer values you can run the following record udf:
function convert_segment_list_to_integers(rec)
local list_with_ints = list()
for value in list.iterator(rec['segments']) do
local int_value = math.floor(tonumber(value))
list.append(list_with_ints, int_value)
end
rec['segments'] = list_with_ints
aerospike:update(rec)
end
When you edit your existing lua module, make sure to re-run register module 'convert_segment_list_to_integers.lua'.
The cause of this issue is within the aerospike-loader tool: it will always assume/enforce strings as you can see in the following java code:
case LIST:
/*
* Assumptions
* 1. Items are separated by a colon ','
* 2. Item value will be a string
* 3. List will be in double quotes
*
* No support for nested maps or nested lists
*
*/
List<String> list = new ArrayList<String>();
String[] listValues = binRawText.split(Constants.LIST_DELEMITER, -1);
if (listValues.length > 0) {
for (String value : listValues) {
list.add(value.trim());
}
bin = Bin.asList(binColumn.getBinNameHeader(), list);
} else {
bin = null;
log.error("Error: Cannot parse to a list: " + binRawText);
}
break;
Source on Github: http://git.io/vRAQW
If you prefer, you can modify this code and re-compile to always assume integer list values. Change line 266 and 270 to something like this (untested):
List<Integer> list = new ArrayList<Integer>();
list.add(Integer.parseInt(value.trim());

Accessing specific values in array of JSON-Objects

I am using rails 3 with backbone.js 0.53 and currently receive a GET with the following array:
[{"credit_card":
{"id":2,"cc_number":"12345678912345","cc_type":"stack","owner":"overflow"}},
....next objects....]
I have read a lot of the other threads but can't figure out how to access the values.
Is there any way to do this with the backbone-given methods like .get()?
I tried
myArray = eval(arrayJSON)
alert myArray.length #works
but any other way of accessing the single values in an array or iterating over it fails.
Probably I am just missing something here.
Quick example of how to iterate through all your results via underscore:
/* received results mocking */
model.attributes = [
{ "credit_card" : { "id":2, "cc_number":"12345678912345" },
{ "credit_card" : { "id":3, "cc_number":"44444444455555" },
{ "credit_card" : { "id":4, "cc_number":"66666655554332" }
]
/* lets get all results */
results = model.toJSON()
/* loop through all results */
_(results).each(item) {
console.log(item.credit_card.id);
}
/* get result by array pos */
console.log(results[1]); // get 2nd item
Cheers
It might be useful to see more details, but in general you should be able to access elements just fine with the following syntax: alert(myArray[0].credit_card.cc_number);
I even pasted your sample array into jsFiddle and had no issues: http://jsfiddle.net/P4w7T/1/

Resources