In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
It is difficult to implement cross-summarization in MongoDB, and it is also quite complicated to take out the data and summarize them in high-level languages such as Java. Therefore, you can consider using the aggregator esProc to assist MongoDB to complete cross-summarization. Let's take a look at the specific approach through an example.
The collection of Student is as follows:
Db.student.insert ({school:'school1', sname: 'Sean', sub1:4, sub2: 5})
Db.student.insert ({school:'school1', sname: 'chris', sub1:4, sub2: 3})
Db.student.insert ({school:'school1', sname: 'becky', sub1:5, sub2: 4})
Db.student.insert ({school:'school1', sname: 'sam', sub1: 5meme sub2: 4})
Db.student.insert ({school:'school2', sname: 'dustin', sub1:2, sub2: 2})
Db.student.insert ({school:'school2', sname: 'greg', sub1:3, sub2: 4})
Db.student.insert ({school:'school2', sname: 'peter', sub1:5, sub2: 1})
Db.student.insert ({school:'school2', sname: 'brad', sub1:2, sub2: 2})
Db.student.insert ({school:'school2', sname: 'liz', sub1: 3Jing sub2: null})
Hope to be able to count the following cross-table: one school per row, the first column is the number of people with a sub1 score of 5, the second example is the number of people with a sub1 score of 4, and so on.
The script for the aggregator is as follows:
A1: connect to the MongoDB database, ip and port number is localhost:27017, database is test, user name and password are test.
A2: use the find function to fetch the collection student from MongoDB to form a cursor. EsProc uses the same parameter format as mongdb's find statement in the find function. EsProc cursors read and process data in batches to avoid excessive amounts of data and memory overflows. The amount of data here is not large, so use the fetch function to take it out at once.
A3: first of all, grouped according to the school.
A4: after the school is divided into groups, each group is aligned according to the sequence of [1Jing 2Jing 3jin4 Jing 5], and then the length of each aligned group is calculated.
A5: the length of A4 is placed in the corresponding position according to the demand, and the results are arranged.
The results are as follows:
It is important to note that the aggregator esProc does not contain MongoDB's java driver package. To access MongoDB with esProc, you must put the java driver package of MongoDB (version 2.12.2 or above, such as mongo-java-driver-2.12.2.jar) in [esProc installation directory]\ common\ jdbc in advance.
The script that aggregator esProc assists MongoDB calculation can be easily integrated into java. As long as you add a line and write it as resultA5, you can output the results in resultset form to java. For specific code, please refer to the esProc tutorial. Similarly, using java to call esProc to access MongoDB must also put the java driver package of mongdb into the classpath of the java program.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
SYS@orcl1 > set lines 200SYS@orcl1 > col name for a50SYS@orcl1 > select * from vs. recovery
© 2024 shulou.com SLNews company. All rights reserved.