Solrj to query solr : dataimport.properties file - java

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);
}

Related

How to do a GET request with params with dates ranges

im using karate framework to test some site, but one call to the endpoints is like this: https://urlbase/index?id=60&filters[start_date]=Fri%20Nov%2018%202022%2010:14:59%20GMT-0300&filters[end_date]=Sat%20Nov%2019%202022%2023:59:59%20GMT-0300
And i have problems with the filters[...] i don't know how to do that :(
I have this query def:
* def query = {id: 60, filters[start_date]:'Fri%20Nov%2018%202022%2010:14:59%20GMT-0300', filters[end_date]:'Sat%20Nov%2019%202022%2023:59:59%20GMT-0300'}
But the request url shows: GET https://urlbase/index?id=60&filters=%7Bstart_date%3D%2C+end_date%3D%7D
`
with no brackets.
And i want my GET url looks like this: https://urlbase/index?id=60&filters[start_date]=Fri%20Nov%2018%202022%2010:14:59%20GMT-0300&filters[end_date]=Sat%20Nov%2019%202022%2023:59:59%20GMT-0300
I tried this and it seems to work fine:
* url 'https://httpbin.org/anything'
* param id = 60
* param filters[start_date] = 'Fri Nov 18 2022 10:14:59 GMT-0300'
* param filters[end_date] = 'Sat Nov 19 2022 23:59:59 GMT-0300'
* method get
You can look at the response and confirm the server handles the request correctly:
{
"args": {
"filters[end_date]": "Sat Nov 19 2022 23:59:59 GMT-0300",
"filters[start_date]": "Fri Nov 18 2022 10:14:59 GMT-0300",
"id": "60"
},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept-Encoding": "gzip,deflate",
"Host": "httpbin.org",
"User-Agent": "Apache-HttpClient/4.5.13 (Java/17.0.4.1)",
"X-Amzn-Trace-Id": "Root=1-6377b983-6bfc4696374f7d08239de823"
},
"json": null,
"method": "GET",
"origin": "X.X.X.X",
"url": "https://httpbin.org/anything?filters[start_date]=Fri+Nov+18+2022+10%3A14%3A59+GMT-0300&filters[end_date]=Sat+Nov+19+2022+23%3A59%3A59+GMT-0300&id=60"
}

I'm not sure how to open these two Json lists and print the output that is within them

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
}
}

Consuming Json from yahoo finance using RestTemplate

I'm trying to get some financial data from yahoo finance using java / spring RestTemplate.
Suppose I need the data for IBM, the link would be something like this:
https://query1.finance.yahoo.com/v10/finance/quoteSummary/IBM?formatted=true&modules=incomeStatementHistory%2CcashflowStatementHistory%2CbalanceSheetHistory%2CincomeStatementHistoryQuarterly%2CcashflowStatementHistoryQuarterly%2CbalanceSheetHistoryQuarterly%2Cearnings
which is a nice formatted JSON with all the data I need:
{
"quoteSummary": {
"result": [
{
"cashflowStatementHistoryQuarterly": {
"cashflowStatements": [
{
"maxAge": 1,
"endDate": {
"raw": 1459382400,
"fmt": "2016-03-31"
},
"netIncome": {
"raw": -5490000,
"fmt": "-5.49M",
"longFmt": "-5,490,000"
},
"depreciation": {
"raw": 16849000,
"fmt": "16.85M", ... etc
But when I try to open the same link using RestTemplate:
using a code like:
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String>response = restTemplate.getForEntity(url, String.class);
The result I'm getting back in response is very different, it looks like this:
<200 OK,{"quoteSummary":{"result":[{}],"error":null}},{X-YQL-Host=[pprd3-node7511-lh2.manhattan.ir2.yahoo.com], X-Content-Type-Options=[nosniff], X-Yahoo-Request-Id=[6s0s3nlbqhlls], Cache-Control=[max-age=1, stale-while-revalidate=15], Content-Type=[application/json; charset=utf-8], Vary=[Origin], Content-Length=[45], Date=[Mon, 08 Aug 2016 19:00:12 GMT], Age=[2], Server=[ATS], Via=[http/1.1 media-ncache-api14.prod.media.ir2.yahoo.com (ApacheTrafficServer [cMsSfW]), http/1.1 media-ncache-api15.prod.media.ir2.yahoo.com (ApacheTrafficServer [cMsSf ]), http/1.1 media-router-api3.prod.media.ir2.yahoo.com (ApacheTrafficServer [cMsSfW]), https/1.1 r07.ycpi.loa.yahoo.net (ApacheTrafficServer [cMsSfW])], Strict-Transport-Security=[max-age=0], Connection=[keep-alive], Y-Trace=[BAEAQAAAAACPmeCQTRIz3QAAAAAAAAAAOEHjlsK9d9kAAAAAAAAAAAAFOZQGmKa_AAU5lAap7rIUoHdnAAAAAA--]}>
Any idea how can I get the same result I'm getting in the browser?
Thank you

How to use elemMatch in spring data for nested objects to retrieve data from mongodb

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);

How to store json array data into mysql database in JAVA?

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.

Categories

Resources