spark dataframes java explode function - java

I have the below json file
{
"data": [
{
"id": "254094394708288_946242468826807",
"from": {
"name": "Emirates NBD \u0628\u0646\u0643 \u0627\u0644\u0625\u0645\u0627\u0631\u0627\u062a \u062f\u0628\u064a \u0627\u0644\u0648\u0637\u0646\u064a",
"category": "Bank/Financial Institution",
"id": "254094394708288"
}
},
{
"id": "254094394708288_945515772232810",
"from": {
"name": "Emirates NBD \u0628\u0646\u0643 \u0627\u0644\u0625\u0645\u0627\u0631\u0627\u062a \u062f\u0628\u064a \u0627\u0644\u0648\u0637\u0646\u064a",
"category": "Bank/Financial Institution",
"id": "254094394708288"
}
}
]
}
I want to create a hive table based on JSON objects inside "data" JSON array. I want to do this in Java. I dont want to do "select data.id,data.from from temptable". I want to do something similar to to a select * query. It should automatically infer schema from json objects. I came to find explode function in python and scala.
http://stackoverflow.com/questions/31859271/sparksql-and-explode-on-dataframe-in-java
Is there something similar in Java??

In Java there is exactly the same thing:
org.apache.spark.sql.functions.explode(Column e)
which returns Column
You could check my answer here for the details

Related

How can I store and update the nested json object into couchbase using java sdk

I am using couchbase Community Edition 5.0.1 and java-client 2.7.4. I want to store the following nested json object into couchbase. If I want to update the same object without affecting the other fields.
Eg:
If I want to add one more player object under players object
array
If I want to add One more group say 'Z Group' under group object array
How can I Achieve this without affecting other fields.
{
"doctype": "config:sample",
"group": [{
"name": "X Group",
"id": 1,
"players": [{
"name": "Roger Federer",
"number": 3286,
"keyword": "tennies"
},
{
"name": "P. V. Sindhu",
"number": 4723,
"keyword": "badminton"
}
]
},
{
"name": "Y Group",
"id": "2",
"players": [{
"name": "Jimmy Connors",
"number": 5623,
"keyword": "tennies"
},
{
"name": "Sachin",
"number": 8756,
"keyword": "Cricket"
}
]
}
]
}
N1QL has a huge variety of functions to operate on arrays:
https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/arrayfun.html
In your case, you could simply use ARRAY_INSERT or ARRAY_PREPEND
Check out update/update-for syntax (last example) https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/update.html
UPDATE default AS d
SET d.group = ARRAY_APPEND(d.group, {......})
WHERE .....;
UPDATE default AS d
SET g.players = ARRAY_APPEND(g.players, {......}) FOR g IN d.group WHEN g.id = 2 END
WHERE .....;
If you know which document IDs you want to update you can use the key-value subdocument API, which will generally be faster than going via N1QL for a single document update.
This will add a new player to the end of X Group's "players" array:
bucket.mutateIn(docId)
.arrayAppend("group[0].players",
JsonObject.create()
.put("name", "John Smith"))
// ... other player JSON
.execute();
And this will add a new Group Z to the "group" array:
bucket.mutateIn(docId)
.arrayAppend("group",
JsonObject.create()
.put("name", "Z Group"))
// ... other group JSON
.execute();

Is there a way to dynamically set variables values for the post request with jmeter beanshell preprocessor

I need to dynamically build the following post request JSON body with jmeter beanshell preprocessor. I am referring to the following question which has a solution for my problem with looped strings. I would need to do this with json-property(variables) an array of JSON objects with different name and values. Thanks a lot.
{
"processDefinitionId":"optaplannerkey:1:dbc4af8f-7e04-11e9-afa3-1ecac26bb5e0",
"businessKey":"optaplannerkey",
"returnVariables":true,
"variables": [
{
"name": "TaskDescription",
"value": "Fixing the issue with sink"
},
{
"name": "TaskCategory",
"value": "plumbing"
},
{
"name": "Priority",
"value": "Medium"
},
{
"name": "Status",
"value": "New"
},
{
"name": "SkillsRequired",
"value": "Plumbing Skills"
},
{
"name": "DueDate",
"value": "2019-05-24T11:23:08.030+05:30"
}
]
}
Use dummy sampler with the parameterized json request and CSV Data Set Config for the dynamic input. Below, I have paremeterized only two for demo.
Then, Use JSR223 Post processor with the following code:-
vars.put("responseVar",prev.getResponseDataAsString());
This will put response body in "responseVar" variable. Fetch it using ${responseVar}
Hope this helps.

Elastic Search: Query the given index to find out values fields

Can anyone help me in making a query for the following index..
"_index": "demodata",
"_type": "sarthak",
"_id": "AVyLnJgOVAC1tB7BveyG",
"_score": 1,
"_source": {
"values": """[{"label":"Male","value":"m","selected":true},{"label":"Female","value":"F"},{"label":"Other","value":"O"}]""",
"name": "select-1496990862221",
"className": "form-control",
"label": "Select",
"type": "select",
"required": true
I want to get inside values and get the label ,value and selected values.Also the values are not fixed they will change. Si I want a query which works on other types. Thanks for the help.
it's a nested field, something like this should work :
“must”: [
{
“nested”: {
“path”: “values”,
“filter”: {
“terms”: {
“values.label”: [
“male”
]
}
}
}
}
]
This query works for ES 1.7.*, you might need a little modification if you work with other versions.

Show JSON data in grid in vaadin

Am a new bee to vaadin. I have to show the data from a JSON file (which is fetching from MySQL db) in Grid/Table(vaadin). I am able show the data in table if JSON in the below format.
[
{
"id": "ex-wardrobe",
"productId": "ex-wardrobe",
"name": "exWardrobe",
"desc": "Some description",
"dimension": "WxDxH 148\" X 24\" X 112\" ",
"category": "Bedroom",
"subcategory": "Wardrobe",
"categoryId": "bedroom",
"subcategoryId": "wardrobe",
"tags": "all, Space Design Bedroom, Space Details Wardrobe",
"designer": "hb",
"curr": "INR",
"popularity": "1",
"relevance": "1",
"shortlisted": "1",
"likes": "1",
"createDt": "",
"pageId": "ex-wardrobe",
"styleName": "Fresh",
"styleId": "cfresh",
"priceRange": "Premium",
"priceId": "premium",
"defaultPrice": "123",
"defaultMaterial": "MDF ",
"defaultFinish": "LAMINATE"
}
]
But, if i get JSON(data is related to same product) in the below format am unable to add data in table.
[
{
"id": "ex-wardrobe",
"productId": "ex-wardrobe",
"name": "exWardrobe",
"desc": "Some description",
"dimension": "WxDxH 148\" X 24\" X 112\" ",
"category": "Bedroom",
"subcategory": "Wardrobe",
"categoryId": "bedroom",
"subcategoryId": "wardrobe",
"tags": "all, Space Design Bedroom, Space Details Wardrobe",
"designer": "hb",
"curr": "INR",
"popularity": "1",
"relevance": "1",
"shortlisted": "1",
"likes": "1",
"createDt": "",
"pageId": "ex-wardrobe",
"styleName": "Fresh",
"styleId": "cfresh",
"priceRange": "Premium",
"priceId": "premium",
"defaultPrice": "123",
"defaultMaterial": "MDF ",
"defaultFinish": "LAMINATE",
"mf": [
{
"basePrice": "123",
"material": "MDF ",
"finish": "LAMINATE"
}
],
"images": [
"066___ex_WARDROBE_Dim.jpg",
"067___ex_WARDROBE_close_door.jpg",
"068___ex_DOVE_dim.jpg"
],
"components": [],
"accessories": []
}
]
This is the code which am using to show JSON data in table,
Table grid = new Table();
root.addComponent(grid);
grid.setStyleName("iso3166");
grid.setPageLength(6);
grid.setSizeFull();
grid.setSelectable(true);
grid.setMultiSelect(false);
grid.setImmediate(true);
grid.setColumnReorderingAllowed(true);
grid.setColumnCollapsingAllowed(true);
try {
JSONArray products = productsDataProvider.getCatalogs();
JsonContainer dataSource =
JsonContainer.Factory.newInstance(products.toString());
grid.setContainerDataSource(dataSource);
grid.setColumnReorderingAllowed(true);
grid.setWidth("98%");
grid.addStyleName(ChameleonTheme.TABLE_STRIPED);
} catch (IllegalArgumentException ignored) {
}
grid.setWidth("100%");
grid.setHeight("100%");
root.addComponent(grid);
Am stuck on this and i have sleepless night on this. Million tons of thanks in advance. I hope you GURU's can help me in this :)
Sorry not vaadin expert. See it first time and like it. I guess your problem are the arrays inside your object. I mean this:
"mf": [
{
"basePrice": "173881",
"material": "MDF ",
"finish": "LAMINATE"
}
],
"images": [
"066___ex_WARDROBE_Dim.jpg",
"067___ex_WARDROBE_close_door.jpg",
"068___ex_DOVE_dim.jpg"
],
"components": [],
"accessories": []
No idea how the component should display this. Have you tried it without mf, images, components and accessories?
You are using a very simple JSONContainer. As can be seen in the source code, this implementation does not support nested / compound elements and arrays.
First you have to ask yourself how these complex objects need to be displayed (UX), especially the "mf" field.
UPDATE
Simple compound objects (like "simpleCompound": {"name": "foo", number: 123}) can be shown in a table column (not supported by the JSONContainer you use, but similar functionality is available by the BeanItemContainer, so look there for how to implement this functionality).
The array fields are more problematic from a UX standpoint. Mostly this information is only shown on demand or in separate panels. The Vaadin Grid component offers the possibility to show a details view, see the wiki. Maybe that will fit your requirements.
Currently nested json data is not supported unless you create your own advance template for that. There is currently an online json container application demo that is used to test if the data is really json or json array but not nested. Then it displays that data in grid table. So you can use this to verify your data.
You can also get the application template with source code on github

Navigating through JSONObject Android

I am trying to implement a method which is able to navigate through a JSONObject until it finds a given parameter. My JSON is structured like a foldersystem. There are folders and every folder can have files.A folder can have another folder and so on.JSON is looking like this:
{
"Elements": [
{
"file": {
"files": [
{
"id": "562dd7a2-9268-46b2-8963-b7a3b43e906e",
"modified": 1457608018166,
"name": "Bild1"
},
{
"id": "0efd76e7-730e-428a-96a4-95e04844070a",
"modified": 1457608018166,
"name": "Audio"
},
{
"files": [
{
"id": "562dd7a2-9268-46b2-8963-b7a3b43e906e",
"modified": 1457608018166,
"name": "Bild2"
},
{
"id": "562dd7a2-9268-46b2-8963-b7a3b43e906e",
"modified": 1457608018166,
"name": "Bild3"
},
{
"id": "562dd7a2-9268-46b2-8963-b7a3b43e906e",
"modified": 1457608018166,
"name": "Bild4"
}
],
"id": "562dd7a2-9268-46b2-8963-b7a3b43e906e",
"name": "FolderInRoot"
}
],
"id": "562dd7a2-9268-46b2-8963-b7a3b43e906e",
"name": "RootFolder"
}
},
{
"file": {
"files": [],
"id": "562dd7a2-9268-46b2-8963-b7a3b43e906e",
"name": "AnotherRootFolder"
}
}
]
}
So for example I want to edit "Bild2" which has the path "/RootFolder/FolderInRoot/Bild2". Has someone a method which navigates to that given position. The path should be the parameter.
You need to loop through the your JSON response until you get to the correct depth, and then you can use getString("name") to get the content at that key (i.e. Bild2). Once you get the content from the object/key you can add it to a List or Map to iterate over or do anything else you want with the data. See this StackOverflow question for more info: Java loop over Json array?
You also might want to consider using the Gson library to make your parsing a lot easier. If you are able to add Gson to the project (and I suggest that you do), there are a ton of great tutorials for parsing data and deserializing JSON into plain Java objects (which will save you the headache of nested loops). Using GSON in Android to parse a complex JSON object and http://blog.nkdroidsolutions.com/how-to-parsing-json-array-using-gson-in-android-tutorial/ are two good resources to begin with.
(P.S. you might want to consider simplifying your JSON structure if possible because it will be a lot easier to work with).

Categories

Resources