Save Nested JSON using Spring data jpa - java

I want to save data in MYSQL DB by creating Entity class and repository from scratch. I am able to save the normal String Data, Integer Data but struggling to save complex JSON data's
for instance:
[
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0002",
"type": "donut",
"name": "Raised",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0003",
"type": "donut",
"name": "Old Fashioned",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
}
]
How can I store such JSON's in MYSQL Db?
Should I Create Class for every nested element ?

(I would consider to switch to a NoSQL DB instead of MySQL, but okay...)
//1.
create table users_json(
id int auto_increment primary key,
details json);
2.
public interface SomeRepository extends JpaRepository<AnyEntity, Long> {
#Modifying(clearAutomatically = true)
#Query(value = "insert into users_json (details) values (:param) ", nativeQuery = true)
#Transactional
int insertValue(#Param("param") String param);}
3.
anyRepository.insertValue("{ \"page\": \"1\" , \"name\": \"Zafari\", \"os\": \"Mac\", \"spend\": 100, \"resolution\": { \"x\": 1920, \"y\": 1080 } }");
4.
SELECT id, details->'$.name' FROM users_json;

Storing JSON in MySQL is possible. You can use these 3 column types depending upon the column size.
For your Entity class :
#Entity
#Getter
#Setter
public class Test {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#Column(columnDefinition = "LONGTEXT") // can store upto 4GB
private String longText;
#Column(columnDefinition = "MEDIUMTEXT") // can store upto 64MB
private String mediumText;
#Column(columnDefinition = "TEXT") // can store upto 64KB
private String text;
}
For your Controller method :
#PostMapping(value = "/addData")
public void addData(#RequestBody String payload) {
testRepository.addData(payload);
}
For your Repository Class:
#Repository
public interface TestRepository extends JpaRepository<Test,Integer> {
#Modifying
#Transactional
#Query(value = "INSERT INTO test(text,medium_text,long_text) VALUE(?1,?1,?1)" ,nativeQuery = true)
void addData(String payload);
}
In MYSQL it will look like this,

It depends if you want to store your Json as String or do you want to convert it into DTO instances that are mapped to your entities and use repository to save them to DB? If you want to store JSON as a String than It shouldn't be any different from any other String. If you want to store it as Entities than you need to convert your JSON (de-serialize) into your DTOs and then work with them as regular DTOs. It doesn't matter how they where created. I just answered very similar question. Please see here

Related

Build JSON from a very complex JSON Schema in Java

I have a complex issue here and some advice or suggestions would be greatly appreciated. Essentially I have a complex JSON schema that looks something like this:
{
"$schema": "http://example.org",
"$id": "http://example.org",
"title": "schema title",
"description": "description",
"properties": {
"name": {
"description": "description",
"type": "string",
"enum": [
"name1",
"name2"
]
},
"storage": {
"description": "description",
"type": "integer",
"minimum": "200",
"maximum": "500",
"default": "200",
},
"domain": {
"description": "description",
"type": "string"
},
},
"if": {
"properties": {
"name": {
"const": "name1"
}
}
},
"then": {
"if": {
"properties": {
"version": {
"const": "version1"
}
}
},
"then": {
"properties": {
"cpus": {
"description": "description",
"type": "integer",
"minimum": 1,
"maximum": 8
},
"memory": {
"description": "description",
"type": "integer",
"minimum": 1,
"maximum": 32
},
},
"required": [
"cpus",
"memory"
]
},
"else": {
"if": {
"properties": {
"version": {
"const": "version2"
}
}
},
"then": {
"properties": {
"cpus": {
"description": "description",
"type": "integer",
"minimum": 1,
"maximum": 8
},
"diskSize": {
"description": "description",
"type": "integer",
"minimum": 250,
"maximum": 1000
},
},
"required": [
"cpus",
"diskSize"
]
}
}
},
"else": {
"if": {
"properties": {
"name": {
"const": "name2"
}
}
},
"then": {
"if": {
"properties": {
"version": {
"const": "version3"
}
}
},
"then": {
"properties": {
"diskSize": {
"description": "description",
"type": "integer",
"minimum": 100,
"maximum": 500
}
"memory": {
"description": "description",
"type": "integer",
"minimum": 1,
"maximum": 28
}
},
"required": [
"diskSize",
"memory"
]
},
"else": {
"if": {
"properties": {
"version": {
"const": "version4"
}
}
},
"then": {
"properties": {
"cpus": {
"description": "description",
"type": "integer",
"minimum": 1,
"maximum": 28
},
"memory": {
"description": "description",
"type": "integer",
"minimum": 1,
"maximum": 64
}
},
"required": [
"cpus",
"memory"
]
}
}
}
}
}
I need to build a JSON object using this schema in java. Every property in the schema is inside of a map that I have access to, so I can quite simply just get the property from the map and add it to a JsonNode object that I am building. Every property under the initial "properties" object is easy to retrieve, I can just get a list of them and then get each one from the map.
The complexity lies in the if/then/else part of the json schema. The only way I can see to find which property I need is to first build the initial part of the json from the first "properties" object and then have some sort of quite complex recursive algorithm that goes into every if/then/else statement and compares the value of the property being evaluated and then returns a list of the properties I need to get from the map. I have looked around online for a library that can build Json from a Json schema in java but haven't found anything that can deal with the complex if/then/else statements.
Any suggestions or ideas would be greatly appreciated.

How to create RealmList<RealmList<Object>> in android

I am trying to get a RealmList of RealmList from server (JSON) into realm object. I am getting
error: Element type of RealmList must be a class implementing 'RealmModel' or one of the 'java.lang.String', 'byte[]', 'java.lang.Boolean', 'java.lang.Long', 'java.lang.Integer', 'java.lang.Short', 'java.lang.Byte', 'java.lang.Double', 'java.lang.Float', 'java.util.Date'.
{
"facilities": [
{
"facility_id": "1",
"name": "Property Type",
"options": [
{
"name": "Apartment",
"icon": "apartment",
"id": "1"
},
{
"name": "Condo",
"icon": "condo",
"id": "2"
},
{
"name": "Boat House",
"icon": "boat",
"id": "3"
},
{
"name": "Land",
"icon": "land",
"id": "4"
}
]
},
{
"facility_id": "2",
"name": "Number of Rooms",
"options": [
{
"name": "1 to 3 Rooms",
"icon": "rooms",
"id": "6"
},
{
"name": "No Rooms",
"icon": "no-room",
"id": "7"
}
]
},
{
"facility_id": "3",
"name": "Other facilities",
"options": [
{
"name": "Swimming Pool",
"icon": "swimming",
"id": "10"
},
{
"name": "Garden Area",
"icon": "garden",
"id": "11"
},
{
"name": "Garage",
"icon": "garage",
"id": "12"
}
]
}
],
"exclusions": [
[
{
"facility_id": "1",
"options_id": "4"
},
{
"facility_id": "2",
"options_id": "6"
}
],
[
{
"facility_id": "1",
"options_id": "3"
},
{
"facility_id": "3",
"options_id": "12"
}
],
[
{
"facility_id": "2",
"options_id": "7"
},
{
"facility_id": "3",
"options_id": "12"
}
]
]
}
try this solution
public class Exclusion extends RealmObject {
private int facilityId;
private int optionsId;
}
public class Exclusions extends RealmObject {
private RealmList<Exclusion> exclusions;
}
so now you can using RealmList<Exclusions> exclusionsRealmList as Realm list, legal
hope this helps
Gianthran's answer is close, but you should also set up relations.
public class Exclusion extends RealmObject {
#Index
private int facilityId;
#Index
private int optionsId;
private Facility facility;
private Option option;
}
And
public class Facility extends RealmObject {
#LinkingObjects("facility")
private final RealmResults<Exclusion> exclusions = null;
}
public class Option extends RealmObject {
#LinkingObjects("option")
private final RealmResults<Exclusion> exclusions = null;
}
I don't think the Exclusions object is necessary, I'd expect that the table is already a collection in a way.

Consuming a nested JSON array using Spring Boot and RestTemplate

I am attempting to consume an API in my Spring Boot application using an HTTP GET request which returns the below JSON. The issues I'm running into are that there is a JSON array contained inside the "playerentry" level with un-named/unheaded pairs of player and team info. For Spring, one would usually create a java class for each layer of the JSON and use the #JsonProperty() annotation to specify which part of the JSON to generate the Java Objects from. Without names for pairs contained inside the JSON array, and being unsure how to properly setup the java classes for the playerentry array and contained array pairs, I have been unable to use the RestTemplate and RestTemplateBuilder to consume this JSON. Any Help would be greatly appreciated.
{
"rosterplayers": {
"lastUpdatedOn": "2018-02-25 4:24:30 PM",
"playerentry": [
{
"player": {
"ID": "10138",
"LastName": "Abrines",
"FirstName": "Alex"
},
"team": {
"ID": "96",
"City": "Oklahoma City",
"Name": "Thunder",
"Abbreviation": "OKL"
}
},
{
"player": {
"ID": "9466",
"LastName": "Acy",
"FirstName": "Quincy"
},
"team": {
"ID": "84",
"City": "Brooklyn",
"Name": "Nets",
"Abbreviation": "BRO"
}
},
{
"player": {
"ID": "9390",
"LastName": "Adams",
"FirstName": "Steven"
},
"team": {
"ID": "96",
"City": "Oklahoma City",
"Name": "Thunder",
"Abbreviation": "OKL"
}
},
{
"player": {
"ID": "9375",
"LastName": "Afflalo",
"FirstName": "Arron"
},
"team": {
"ID": "103",
"City": "Sacramento",
"Name": "Kings",
"Abbreviation": "SAC"
}
},
{
"player": {
"ID": "9357",
"LastName": "Ajinca",
"FirstName": "Alexis"
},
"team": {
"ID": "110",
"City": "New Orleans",
"Name": "Pelicans",
"Abbreviation": "NOP"
}
},
{
"player": {
"ID": "9272",
"LastName": "Aldrich",
"FirstName": "Cole"
},
"team": {
"ID": "100",
"City": "Minnesota",
"Name": "Timberwolves",
"Abbreviation": "MIN"
}
},
{
"player": {
"ID": "9480",
"LastName": "Aldridge",
"FirstName": "LaMarcus"
},
"team": {
"ID": "106",
"City": "San Antonio",
"Name": "Spurs",
"Abbreviation": "SAS"
}
},
{
"player": {
"ID": "9454",
"LastName": "Alexander",
"FirstName": "Cliff"
},
"team": {
"ID": "95",
"City": "Orlando",
"Name": "Magic",
"Abbreviation": "ORL"
}
},
{
"player": {
"ID": "9299",
"LastName": "Allen",
"FirstName": "Tony"
},
"team": {
"ID": "107",
"City": "Memphis",
"Name": "Grizzlies",
"Abbreviation": "MEM"
}
}
]
}
}
This should work
class Roasterplayers {
String lastUpdatedOn;
List<PlayerEntry> playerentry;
}
class PlayerEntry {
Player player;
Team team;
}
class Player {
#JsonProperty("ID")
String id;
#JsonProperty("LastName")
String lastName;
#JsonProperty("FirstName")
String firstName;
}
class Team {
#JsonProperty("ID")
String id;
#JsonProperty("City")
String city;
#JsonProperty("Name")
String name;
#JsonProperty("Abbreviation")
String abbreviation;
}
Make sure you have Setters and Getters for each field

Setting a dynamic date format in Elastic Search

I am new to Elastic Search.
I have a User mapping and associated with the User is a Nested Object extraDataValues. In this object is the id, a string value and another nested object. For example:
"extraDataValues": [
{
"id": 1,
"value": "01/01/2016 00:00:00",
"id": 10,
"label": "Metadata Date",
"displayable": true
},
},
{
"id": 2,
"value": "aaaa",
"id": 11,
"label": "Metadata TextBox",
"displayable": true
},
}
],
As you can see, value field can be a date or a normal string. The problem arises here, I want to be able to sort this value given that it could be either a date or a normal string. Moreover, the date can be in two formats: "dd/MM/yyyy HH:mm:ss", "dd/MM/yyyy". How can I achieve this firstly with Elastic Search (so I can understand the theory) and then Java?
I have tried adding "dynamic_date_formats" : ["dd/MM/yyyy HH:mm:ss", "dd/MM/yyyy"]
to no avail.
The mapping for the Users is:
User Mapping Document
{
"User": {
"properties": {
"fullName": {
"type": "string",
"index": "not_analyzed",
"fields": {
"raw_lower_case": {
"type": "string",
"analyzer": "case_insensitive"
}
}
},
"username": {
"type": "string",
"index": "not_analyzed",
"fields": {
"raw_lower_case": {
"type": "string",
"analyzer": "case_insensitive"
}
}
},
"email": {
"type": "string",
"index": "not_analyzed",
"fields": {
"raw_lower_case": {
"type": "string",
"analyzer": "case_insensitive"
}
}
},
"firstName": {
"type": "string",
"index": "not_analyzed",
"fields": {
"raw_lower_case": {
"type": "string",
"analyzer": "case_insensitive"
}
}
},
"surname": {
"type": "string",
"index": "not_analyzed",
"fields": {
"raw_lower_case": {
"type": "string",
"analyzer": "case_insensitive"
}
}
},
"id": {
"type": "long"
},
"extraDataValues": {
"type": "nested",
"dynamic_date_formats" : ["dd/MM/yyyy HH:mm:ss", "dd/MM/yyyy"],
"properties": {
"extraDataValueObject": {
"properties": {
"id": {
"type": "long"
},
"label": {
"type": "string"
},
"displayable": {
"type": "boolean"
}
}
},
"value": {
"type": "string",
"index": "not_analyzed",
"fields": {
"raw_lower_case": {
"type": "string",
"analyzer": "case_insensitive"
}
}
}
}
}
}
}
}
You can't do that the way you are trying to do it. dynamic_date_formats are used only for dynamically added date fields, not for date fields that you specify in your mapping (from the documentation).
What I would suggest trying out is this mapping:
"value": {
"type": "string",
"fields": {
"date1": {
"type": "date",
"format": "dd/MM/yyyy HH:mm:ss",
"ignore_malformed": "true"
},
"date2": {
"type": "date",
"format": "dd/MM/yyyy",
"ignore_malformed": "true"
}
}
}
Where you have a field which is string (for the string type part of the value) and for it you define two subfields each with a different date format. It's imperative to have for them "ignore_malformed": "true" in case you really have a string instead of a date coming in.
In this way you can index this:
POST /my_index/user/1
{
"value": "aaa"
}
POST /my_index/user/2
{
"value": "01/01/2016 00:00:00"
}
POST /my_index/user/3
{
"value": "02/02/2016"
}
And you could differentiate between which type of date or string was indexed like this in a query:
"query": {
"filtered": {
"filter": {
"exists": {
"field": "value.date2"
}
}
}
}
If ES was able to index something under value.date2 then you get that document back. The same goes for value.date1, of course.

How to create schema containing list of objects using Avro?

Does anyone knows how to create Avro schema which contains list of objects of some class?
I want my generated classes to look like below :
class Child {
String name;
}
class Parent {
list<Child> children;
}
For this, I have written part of schema file but do not know how to tell Avro to create list of objects of type Children?
My schema file looks like below :
{
"name": "Parent",
"type":"record",
"fields":[
{
"name":"children",
"type":{
"name":"Child",
"type":"record",
"fields":[
{"name":"name", "type":"string"}
]
}
}
]
}
Now problem is that I can mark field children as either Child type or array but do not know how to mark it as a array of objects of type Child class?
Can anyone please help?
You need to use array type for creating the list.
Following is the updated schema that handles your usecase.
{
"name": "Parent",
"type":"record",
"fields":[
{
"name":"children",
"type":{
"type": "array",
"items":{
"name":"Child",
"type":"record",
"fields":[
{"name":"name", "type":"string"}
]
}
}
}
]
}
I had following class and avro maven plugin generated two classes accordingly :
public class Employees{
String accountNumber;
String address;
List<Account> accountList;
}
public class Account {
String accountNumber;
String id;
}
Avro file format :
{
"type": "record",
"namespace": "com.mypackage",
"name": "AccountEvent",
"fields": [
{
"name": "accountNumber",
"type": "string"
},
{
"name": "address",
"type": "string"
},
{
"name": "accountList",
"type": {
"type": "array",
"items":{
"name": "Account",
"type": "record",
"fields":[
{ "name": "accountNumber",
"type": "string"
},
{ "name": "id",
"type": "string"
}
]
}
}
}
]
}
Array as type
{
"type": "record",
"name": "jamesMedice",
"fields": [{
"name": "columns",
"type": {
"type": "array",
"items": {
"type": "record",
"name": "columnValues",
"fields": [{
"name": "personId",
"type": "string",
"default": "null"
},
{
"name": "email",
"type": "string",
"default": "null"
}
]
}
}
}]
}

Categories