I want to store the following JSON Array into MySql Database
My Requirement is to Synchronize the below data with MySql DB from an android Application.
[
{
"DATE":"Oct 15, 2015 7:09:23 AM",
"LNG":"75.124",
"LAT":"15.3647066",
"IMEI":"2",
"LOC":"Prabhat Colony"
},
{
"DATE":"Oct 15, 2015 7:09:41 AM",
"LNG":"75.0078",
"LAT":"15.4589233",
"IMEI":"2",
"LOC":"Hosayellapur"
},
{
"DATE":"Oct 15, 2015 7:15:56 AM",
"LNG":"74.4977",
"LAT":"15.849695",
"IMEI":"2",
"LOC":"Camp"
},
{
"DATE":"Oct 15, 2015 7:50:19 AM",
"LNG":"77.209",
"LAT":"28.6139383",
"IMEI":"2",
"LOC":"Rajpath Area"
},
{
"DATE":"Oct 15, 2015 7:50:41 AM",
"LNG":"77.391",
"LAT":"28.535515",
"IMEI":"2",
"LOC":"Sector - 106"
},
{
"DATE":"Oct 15, 2015 8:07:25 AM",
"LNG":"77.391",
"LAT":"28.535515",
"IMEI":"2",
"LOC":"Sector - 106"
},
{
"DATE":"Oct 15, 2015 8:20:26 AM",
"LNG":"77.391",
"LAT":"28.535515",
"IMEI":"2",
"LOC":"Sector - 106"
}
]
Can anyone please guide me on which is the best way to store JSON values to mysql Database?
You can create an object containing your attributes, and then deserialize your json to get an array of your object.
Then, you can then use JPA or another method to store your object in the database.
Unmarshal the steam into Predefined DTO classes. You can use GSon. Gson is an open source Java library to serialize and deserialize Java objects to (and from) JSON.
Gson gson = new Gson();
ClassName className = gson.fromJson(data, ClassName.class);
2.Use ORM such as hibernate to store Object in relational database.
Related
I have a JSON String:
{
06-2020={
ma01={
0={duur=Dag, aantalVoertuigen=2, tijd=14: 06, naamHuurder=K, prijs=70 },
1={duur=Dag, aantalVoertuigen=2, tijd=14: 08, naamHuurder=H, prijs=70 },
2={duur=Dag, aantalVoertuigen=2, tijd=14: 08, naamHuurder=H, prijs=70 },
3={duur=Dag, aantalVoertuigen=2, tijd=14: 08, naamHuurder=H, prijs=70 },
Aantal=4
}
},
05-2020={
hallo={auto=ja, naam=ja },
za30={hallo=ja, naam=ja }
}
}
I need to get the 06-2020 and the 05-2020 out of it.
I tried using JSONobject. But that only works with one layer.
Thanks in advance
I have a customer info with orders in Elastic Search in the following format
{
"_index": “customer_domain_r4",
"_type": “CustomerData”,
"_id": "6fff6ce8-e792-4047-b636-a1dda0aa2b72",
"_version": 173,
"_score": 3.551258,
"_source": {
“first”: "John",
“last”: "Doe",
“dateOfBirth”: null,
"primaryPhone": "6154525131",
“customerOrders”: [
{
“orders”: [
{
“orderNumber": 1,
“orderName": “Grocery”,
"recordVersion": 1,
"createdDateTimeGmt": "May 09, 2018 03:19:11 PM",
"deletedDateTimeGmt": "May 12, 2018 08:28:42 PM",
"createdSessionTokenId": "PUWzJmipCIyvciMDEr3IfhXuV3Gx",
"updatedSessionTokenId": "1234567890",
"lastUpdatedDateTimeGmt": "May 12, 2018 08:28:48 PM",
“orderType”: “Online”
"id": "8e2973e3-b43b-4430-b6d7-7c24846e9bba"
},
{
“orderNumber": 2,
“orderName": “Medications”,
"recordVersion": 1,
"createdDateTimeGmt": "May 09, 2018 03:19:11 PM",
"deletedDateTimeGmt": null,
"createdSessionTokenId": "PUWzJmipCIyvciMDEr3IfhXuV3Gx",
"updatedSessionTokenId": "1234567890",
"lastUpdatedDateTimeGmt": "May 12, 2018 08:28:48 PM",
“orderType”: “Offline”
"id": "8e2973e3-b43b-4430-b6d7-7c2665e9bba"
}
]
}
Now my requirement is to fetch only orders of a certain type for a given customer id. The query i ran
{"index":"customer_domain_r4","type":"CustomerData"}
{"query":{"bool":{"must":[{"match":{"id":"6fff6ce8-e792-4047-b636-a1dda0aa2b72"}},{"nested":{"path":"customerOrders","query":{"bool":{"must":[{"match":{"customerOrders. orderType":"Online"}}]}}}}]}},"_source":{"includes":["customerOrders"],"excludes":[]}}
The above query returns all the orders of the customer because the criteria looks for any customer which has an order of type online. I am looking for the query to return only orders of customer 6fff6ce8-e792-4047-b636-a1dda0aa2b72 of type online.
Is there a way i can directly add criteria for the includes section.
If not i have to filter this in the java middle tier section
I am trying to make a program that reads through this JSON text:
{
"Suburban Station Departures: September 1, 2017, 10:18 pm": [
{
"Northbound": [
{
"direction": "N",
"path": "R7N",
"train_id": "776",
"origin": "Trenton",
"destination": "Chestnut H East",
"status": "4 min",
"service_type": "LOCAL",
"next_station": "30th Street Station",
"sched_time": "Sep 1 2017 10:26:00:000PM",
"depart_time": "Sep 1 2017 10:27:00:000PM",
"track": "2",
"track_change": null,
"platform": "B",
"platform_change": null
},
The program will retrieve the output from the site, and grabs the last updated portion of text. I've managed to retrieve the site's input, but I'm not sure how I would get the info within the two arrays. I have tried looking at other questions but they are dealing with no arrays/lists at all and just parsing data without lists/arrays.
In your data "Suburban Station Departures: September 1, 2017, 10:18 pm" is a String . It should be key pair value according to structure.
According to your JSON Data try this code:
JSONObject obj = new JSONObject("JSON_String");
JSONArray array = obj .getJSONArray("Suburban Station Departures: September 1, 2017, 10:18 pm");
for(int i=0;i<array .length;i++)
{
JSONObject objDepartures = array .getJSONObject(i);
JSONArray arrayNorthbound=objDepartures .getJSONArray("Northbound");
for(int i=0;i<arrayNorthbound.length;i++)
{
JSONObject objDepartures = arrayNorthbound.getJSONObject(i);
String direction=objDepartures .getString("direction");
// rest same
}
}
How can I use Solrj to query Solr using the following api:
http://localhost:8983/solr/admin/zookeeper?detail=true&path=%2Fconfigs%2Fmy-search%2Fdataimport.properties
the above api gives the content of the dataimport.properties file.
{
"znode": {
"path": "/configs/my-search/dataimport.properties",
"prop": {
"version": 186,
"aversion": 0,
"children_count": 0,
"ctime": "Sun Oct 16 10:24:04 UTC 2016 (1476613444895)",
"cversion": 0,
"czxid": 479,
"ephemeralOwner": 0,
"mtime": "Fri Mar 24 09:48:50 UTC 2017 (1490348930211)",
"mzxid": 31451,
"pzxid": 479,
"dataLength": 111
},
"data": "#Fri Mar 24 09:48:50 UTC 2017\nname.last_index_time=2017-03-24 09\\:48\\:49\nlast_index_time=2017-03-24 09\\:48\\:49\n"
},
"tree": [
{
"data": {
"title": "dataimport.properties",
"attr": {
"href": "admin/zookeeper?detail=true&path=%2Fconfigs%2Fmy-search%2Fdataimport.properties"
}
}
}
]
}
btw, my Solr is configured in cloud mode.
Given that your Solr is running in Cloud mode the file /configs/my-search/dataimport.properties is into Zookeeper.
SolrJ does not have native API to easily read from Zookeeper.
To read into the Zookeeper I suggest to use use Apache Curator Framework
String dataPath = "/configs/my-search/dataimport.properties";
String connectionString = "zookeeper-ensemble:2181";
CuratorFramework client = CuratorFrameworkFactory.newClient(connectionString, new ExponentialBackoffRetry(1000, 3));
client.start();
byte[] barray = client.getData().forPath(dataPath);
if (barray != null) {
String data = new String(barray);
System.out.println(data);
}
I need some help on using elemMatch in spring data to query data in mongodb and retrieve results from mongodb.
I want to retrieve results for a particular testRun say testRun=1
I have the following data in mongodb:
[ {
"testMethod": "initialization",
"testCase": "com.harish.test.TestNGRest.OrganisationGetTest",
"build": 1,
"ranNumberofTimes": 2,
"failed": 0,
"success": 2,
"testRuns": [
{
"testMethod": "initialization",
"testCase": "com.harish.test.TestNGRest.OrganisationGetTest",
"branchName": "branch_1",
"testRun": 1,
"success": 1,
"fail": 0,
"timetorun": 0,
"cases": [
{
"caseId": 1,
"status": "success",
"failreason": null,
"startDate": "Fri May 27 14:41:22 EDT 2016",
"endDate": "Fri May 27 14:41:22 EDT 2016"
}
],
"startDate": "Fri May 27 14:41:22 EDT 2016",
"endDate": "Fri May 27 14:41:22 EDT 2016"
},
{
"testMethod": "initialization",
"testCase": "com.harish.test.TestNGRest.OrganisationGetTest",
"branchName": "branch_1",
"testRun": 2,
"success": 1,
"fail": 0,
"timetorun": 1,
"cases": [
{
"caseId": 1,
"status": "success",
"failreason": null,
"startDate": "Fri May 27 14:41:49 EDT 2016",
"endDate": "Fri May 27 14:41:49 EDT 2016"
}
],
"startDate": "Fri May 27 14:41:49 EDT 2016",
"endDate": "Fri May 27 14:41:49 EDT 2016"
}
]
In mongo console i used the following command and got the results:
db.test.find({"testRuns.testRun":1},{"testRuns":{"$elemMatch":{"testRun":1}}}).pretty()
I got the results for my requirement in mongo console.
The real question comes here, I used spring data in to retrieve the results in my java code.
return mongoTemplate.find(BasicQuery.query(Criteria.where("testRuns.testRun").is(testRun).andOperator(Criteria.where("testRuns").elemMatch(Criteria.where("testRun").is(testRun)))),Test.class, COLLECTION_NAME);
However, i'm not able to retrieve the results for a particular test run. Can someone please help me resolve this issue.
Thank you.
You don't need $elemMatch if you would like to get "testRuns.testRun" = 1. There is no second condition. Please refer the below statement in the mongo reference document link.
"Since the $elemMatch only specifies a single condition, the $elemMatch expression is not necessary, and instead you can use the following query:"
db.survey.find(
{ "results.product": "xyz" }
)
https://docs.mongodb.com/manual/reference/operator/query/elemMatch/
I have tested with the below code with the data given in the post. It works fine.
Please make sure that the testRun variable is defined as Integer in Java code. If it is of other data type, the query wouldn't return result.
query.addCriteria(Criteria.where("testRuns.testRun").is(testRun));
mongoOperations.find(query, Stack.class);