Hi I have a collection of the following format
{
"_id" : ObjectId("572eb5df1d739cc73c21f953"),
"address" : {
"building" : "469",
"coord" : [
-73.961704,
40.662942
],
"street" : "Flatbush Avenue",
"zipcode" : "11225"
},
"borough" : "Brooklyn",
"cuisine" : "Hamburgers",
"grades" : [
{
"date" : ISODate("2014-12-30T00:00:00Z"),
"grade" : "A",
"score" : 8
},
{
"date" : ISODate("2014-07-01T00:00:00Z"),
"grade" : "B",
"score" : 23
},
{
"date" : ISODate("2013-04-30T00:00:00Z"),
"grade" : "A",
"score" : 12
},
{
"date" : ISODate("2012-05-08T00:00:00Z"),
"grade" : "A",
"score" : 12
}
],
"name" : "Wendy'S",
"restaurant_id" : "30112340"
}
i have made 3 very simple classes to map objects. Restaurants, Address and Grade.
they have the following format
Restaurant
public String _id;
public Address address;
public String town;
public String cuisine;
public String name;
public String r_id;
public Grade [] grades;
Address
public int building;
public String street;
public long zip;
public float [] coord;
Grade
public String date;
public char grade;
public int score;
I have written the simplest of codes to just toString() a mapped object and see what i get
DB db = new MongoClient().getDB("test");
Jongo j = new Jongo(db);
MongoCollection collection = j.getCollection("restaurants");
Restaurant r1 = collection.findOne().as(Restaurant.class);
System.out.println(r1);
This code outputs
Restaurant: 572eb5df1d739cc73c21f953 / Wendy'S
Cuisine: Hamburgers
Restaurant ID: null
Address: 469 Flatbush Avenue 0, null
Grades: Tue Dec 30 05:00:00 PKT 2014 A / 8
Grades: Tue Jul 01 05:00:00 PKT 2014 B / 23
Grades: Tue Apr 30 05:00:00 PKT 2013 A / 12
Grades: Tue May 08 05:00:00 PKT 2012 A / 12
I cannot seem to fix the null I am getting for the zipcode and the restaurant_id.
Someone please help. Thanks.
Your field names don't match the documents names so they're not getting mapped over. If I recall correctly, you can use #JsonProperty to map those java field names to the document names you're getting back out of mongodb.
Related
How can I sort the map using the data from a value Example: Map<String, List> is my input. I need to sort and get the order based on timestamp
Example:
class SomeObject{
String var1;
Timestamp var2;
String var3; // getters and setters }
INPUT:
Entry 1:
{ Key1 = 123 [ someObject [ var1=4 var2=08.02.2020 13:00:00, var3=6 ] ] }
Entry 2:
{ Key1 = 456 [ someObject [ var1=2 var2=08.02.2020 15:00:00 var3=1 ] ] }
Entry 3:
{ Key1 = 789 [ someObject [ var1=1 var2=08.02.2020 03:00:00 var3=3 ] ] }
OUTPUT after sorting according to var2:
Entry 1:
{ Key1 = 789 [ someObject [ var1=1 var2=08.02.2020 03:00:00 var3=3 ] ] }
Entry 2:
{ Key1 = 123 [ someObject [ var1=4 var2=08.02.2020 13:00:00 var3=6 ] ] }
Entry 3:
{ Key1 = 456 [ someObject [ var1=2 var2=08.02.2020 15:00:00 var3=1 ] ] }
There are 3 entries in Map with format Map<String, List> I need to sort this Map collection with var2 values
Before keys are in: 123,456,789
After sorting keys are in: 789,123,456
This question already has answers here:
MongoDB extracting values from BasicDBObject (Java)
(2 answers)
Closed 5 years ago.
I want to get an JSON array into a var in java. My JSON seems ok but when i try to put a JSOn array into a java array var it does not work.
here the error i get : errororg.json.JSONException: JSONObject["tweets"] is not a JSONArray.
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
DB db = mongoClient.getDB( "test1" );
DBCollection coll = db.getCollection("tweetsCol");
DBCursor cursor = coll.find();
while (cursor.hasNext()) {
BasicDBObject obj = (BasicDBObject) cursor.next();
JSONObject objjj = new JSONObject(obj);
try{
System.out.println("okok "+objjj); // THE JSON I WILL SHOW YOU
JSONArray jsonMainArr = objjj.getJSONArray("tweets");
}catch(JSONException e){
System.out.println("error"+e);
}}
Here's my data in MongoDB:
{
"_id":"5939bc6676abbe186feb73a5",
"user_request_id":"5941903f37aaa6ec55689e85",
"tweets":[
{
"date":"Wed Jun 07 18:32:57 CDT 2017",
"text":"[Earthview Wonders][Video] No.265: Astronaut Thomas Pesquet completed 6-month #MissionProxima. #Neweyes\u2026 ",
"_id":872597276891398144,
"user":"livearthjp"
},
{
"date":"Wed Jun 07 18:16:56 CDT 2017",
"text":"Astronaut Thomas Pesquet #Thom_astro Shares His #Songs4Space ",
"_id":872593245716467712,
"user":"anasia5mice"
},
{
"date":"Wed Jun 07 15:46:03 CDT 2017",
"text":"Thomas Pesquet: Undocking and landing ",
"_id":872555275387117570,
"user":"GRASSIFREE"
},
{
"date":"Wed Jun 21 17:02:37 CDT 2017",
"text":"#Thom_astro #Space_Station And his colleagues said, 'Pesquet, if you play Baker Street one more time...'",
"_id":877647972430823429,
"user":"kimkemmis"
},
{
"date":"Wed Jun 21 17:01:16 CDT 2017",
"text":"[News] ",
"_id":877647632524394497,
"user":"ArthurC2Pouce"
},
{
"date":"Wed Jun 21 11:28:48 CDT 2017",
"text":"Thomas Pesquet's music is OUT THERE! Cool dude. ",
"_id":877563967178104836,
"user":"tiarudd34"
},
{
"date":"Wed Jun 21 11:10:15 CDT 2017",
"text":"jaime thomas pesquet",
"_id":877559296741048320,
"user":"sosthene_maus"
},
{
"date":"Wed Jun 21 10:23:03 CDT 2017",
"text":"French astronaut Thomas Pesquet took some of the most amazing pictures ever while in spce ",
"_id":877547418606329861,
"user":"raygibbs1"
},
{
"date":"Wed Jun 21 10:23:00 CDT 2017",
"text":"French astronaut Thomas Pesquet shares stunning pictures of Earth: via #AOL",
"_id":877547405180157952,
"user":"raygibbs1"
},
{
"date":"Wed Jun 21 08:46:13 CDT 2017",
"text":"Coll Cambuston like Thomas Pesquet! #thomastro #cardierun",
"_id":877523048546676736,
"user":"CambF974"
},
{
"date":"Wed Jun 21 08:00:06 CDT 2017",
"text":"Thomas Pesquet returned to Earth on 2 June 2017 after completion of his six-months long Proxima mission to the... ",
"_id":877511443775619072,
"user":"rospaceagency"
},
{
"date":"Tue Jun 20 23:50:34 CDT 2017",
"text":"Thomas Pesquet #Thom_astro and Messier 83 #Astronauts #ESA #CNES #NASA ✨👨\u200d🚀✨ ",
"_id":877388248368033794,
"user":"AmirAliBehrooz"
}
],
"query_name":"Pesquet",
"active":"true",
"started_at":"2017_06_08"
}
Now I understand better your question thanks to #Neil Lunn, and following his best advice, you have to understand that you are not dealing here with Json, you can directly do:
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
DB db = mongoClient.getDB( "test1" );
DBCollection coll = db.getCollection("tweetsCol");
DBCursor cursor = coll.find();
while (cursor.hasNext()) {
BasicDBObject obj = (BasicDBObject) cursor.next();
try{
System.out.println("okok "+obj); // THE JSON I WILL SHOW YOU
BasicDBList jsonMainArr = obj.get("tweets");
}catch(JSONException e){
System.out.println("error"+e);
}}
This is my json data based on the date i want to sort and group by and find total SMSSentId.
The problem is same month have more than sms sent Please help me
data.json
{
"_id" : ObjectId("57bed54862c2630dd29ecce0"),
"SMSSentId" : [
"51b6f5fde4b0dd92df2c3270",
"51b866e9e4b021170dd1ae1c"
],
"sDate" : 02-Oct 2015,
"status" : "S"
}
{
"_id" : ObjectId("57bed54862c2630dd29ecce0"),
"SMSSentId" : [
"51b6f5fde4b0dd92df2c3270"
],
"sDate" : 02-Oct 2015,
"status" : "S"
}
{
"_id" : ObjectId("57bed54862c2630dd29ecce0"),
"SMSSentId" : [
"5416e31ae4b0fb949a58942f"
"51b866e9e4b021170dd1ae1c"
],
"sDate" : 09-Oct 2015,
"status" : "S"
}
{
"_id" : ObjectId("57bed54862c2630dd29ecce0"),
"SMSSentId" : [
"51b876d5e4b021170dd1b09c",
"51b87736e4b021170dd1b09f",
],
"sDate" : 01-Jan 2016,
"status" : "S"
}
{
"_id" : ObjectId("57bed54862c2630dd29ecce0"),
"SMSSentId" : [
"51b87736e4b021170dd1b09f",
],
"sDate" : 01-Apr 2016,
"status" : "S"
}
{
"_id" : ObjectId("57bed54862c2630dd29ecce0"),
"SMSSentId" : [
"51b87736e4b021170dd1b09f",
"51b6f5fde4b0dd92df2c3270"
],
"sDate" : 01-Apr 2016,
"status" : "S"
}
these values are already have SMSLog Pojo class
class SMSLog{
private String id;
private List<String>SMSSentId;
private String sDate;
private String status;
}
how to process these data
public static void main(String args[]){
List<SmsLog> report;
for (SmsLog smsLog : report) {
try {
} catch (Exception ae) {
System.out.println(ae.getMessage());
}
}
}
I want to expecting this output using core java logic...
# | Month | Year | TotalSMSSent
1 Oct 2015 5
2 Jan 2016 2
3 Apr 2016 1
TotalSMSSent is total count of SMSSentId in each month suppose same month have multiple documents.Thankyou..
My json file is like the below , i am trying to read all Name under majorsector_percent using the below code .
Code :
JavaSQLContext sQLContext = new JavaSQLContext(sc);
sQLContext.jsonFile("C:/Users/HimanshuK/Downloads/world_bank/world_bank.json").registerTempTable("logs");
sQLContext.sqlContext().cacheTable("logs");
List s = sQLContext.sql("select majorsector_percent from logs limit 1 ").map(row -> new Tuple2<>(row.getString(0), row.getString(1))).collect();
JSON FIle
{ "_id" : { "$oid" : "52b213b38594d8a2be17c780" }, "approvalfy" : 1999, "board_approval_month" : "November", "boardapprovaldate" : "2013-11-12T00:00:00Z", "borrower" : "FEDERAL DEMOCRATIC REPUBLIC OF ETHIOPIA", "closingdate" : "2018-07-07T00:00:00Z", "country_namecode" : "Federal Democratic Republic of Ethiopia!$!ET", "countrycode" : "ET", "countryname" : "Federal Democratic Republic of Ethiopia", "countryshortname" : "Ethiopia", "docty" : "Project Information Document,Indigenous Peoples Plan,Project Information Document", "envassesmentcategorycode" : "C", "grantamt" : 0, "ibrdcommamt" : 0, "id" : "P129828", "idacommamt" : 130000000, "impagency" : "MINISTRY OF EDUCATION", "lendinginstr" : "Investment Project Financing", "lendinginstrtype" : "IN", "lendprojectcost" : 550000000, "majorsector_percent" : [ { "Name" : "Education", "Percent" : 46 }, { "Name" : "Education", "Percent" : 26 }, { "Name" : "Public Administration, Law, and Justice", "Percent" : 16 }, { "Name" : "Education", "Percent" : 12 } ], "mjtheme" : [ "Human development" ], "mjtheme_namecode" : [ { "name" : "Human development", "code" : "8" }, { "name" : "", "code" : "11" } ], "mjthemecode" : "8,11", "prodline" : "PE", "prodlinetext" : "IBRD/IDA", "productlinetype" : "L", "project_abstract" : { "cdata" : "The development }, "project_name" : "Ethiopia General Education Quality Improvement Project II", "projectfinancialtype" : "IDA", "projectstatusdisplay" : "Active", "regionname" : "Africa", "sector1" : { "Name" : "Primary education", "Percent" : 46 }, "sector2" : { "Name" : "Secondary education", "Percent" : 26 }, "sector3" : { "Name" : "Public administration- Other social services", "Percent" : 16 }, "sector4" : { "Name" : "Tertiary education", "Percent" : 12 }, "sectorcode" : "ET,BS,ES,EP", "source" : "IBRD", "status" : "Active", "supplementprojectflg" : "N", "theme1" : { "Name" : "Education for all", "Percent" : 100 }, "themecode" : "65", "totalamt" : 130000000, "totalcommamt" : 130000000, "url" : "http://www.worldbank.org/projects/P129828/ethiopia-general-education-quality-improvement-project-ii?lang=en" }
but i am getting this error because of typecasting , how to handle such cases and how to know the schema :
java.lang.ClassCastException: scala.collection.mutable.ArrayBuffer cannot be cast to java.lang.String
The issue is that the result of that query is a structure containing arrays. When you try to map the result using row.getString(1) on an array, it fails with a CastException as the corresponding object is not a String.
The result of a SQL Query is a DataFrame, you can ask for the schema like this (you can use the same commands in the Java API):
scala> val data = sqlContext.sql("select majorsector_percent from logs limit 1 ")
data: org.apache.spark.sql.DataFrame = [majorsector_percent: array<struct<Name:string,Percent:bigint>>]
scala> data.printSchema
root
|-- majorsector_percent: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- Name: string (nullable = true)
| | |-- Percent: long (nullable = true)
You could extract the information you need from the resulting dataframe like so:
data.select("majorsector_percent.Name","majorsector_percent.Percent")
scala> data.select("majorsector_percent.Name","majorsector_percent.Percent").collect
res4: Array[org.apache.spark.sql.Row] = Array([WrappedArray(Education, Education, Public Administration, Law, and Justice, Education),WrappedArray(46, 26, 16, 12)])
Or you could simplify the process by using a more specific querying to start with:
val directQuery = sqlContext.sql("select majorsector_percent.Name, majorsector_percent.Percent from logs limit 1 ")
directQuery: org.apache.spark.sql.DataFrame = [Name: array<string>, Percent: array<bigint>]
scala> directQuery.collect
res5: Array[org.apache.spark.sql.Row] = Array([WrappedArray(Education, Education, Public Administration, Law, and Justice, Education),WrappedArray(46, 26, 16, 12)])
I am an iPhone programmer, trying to create an Android version of an iPhone app.
I have a NSArray in Objective C like below.
NSArray *myarray =
#[
#{#"name" : #"2014", #"items" :
#[
#{#"date" : #“01 January 2014", #"full_name" : #“Day 1”},
#{#"date" : #“15 February 2014", #"full_name" : #"Day 2”},
]
},
#{#"name" : #"2015", #"items" :
#[
#{#"date" : #“05 January 2015", #"full_name" : #"Day 1"},
#{#"date" : #“18 February 2015", #"full_name" : #"Day 2”},
#{#"date" : #“26 March 2015", #"full_name" : #"Day 3”},
]
}
];
What is Java equivalent of the array?
How can initialize the above array in Java?
I need all your help finding the answer, thanks.
i think you have two class like:
items class:
class item
{
String date;
String fullName;
// getter and setter
}
and Data class:
class Data
{
List<item> items;
String name;
// getter and setter
}
in your code you have one List of Data like:
List<Data> mArray = new ArrayList<Data>();
for adding as static see following link:
1 - Initialization of an ArrayList in one line
2- how to initialize static ArrayList in one line