The digital comparison of the Collector assist MongoDB calculation

Source: Internet
Author: User
Tags diff

MONGODB Scripting has limited computational power to solve complex problems, and is more difficult to use directly. In many cases, it is necessary to read the data out of the main program to complete the operation, and in high-level languages such as Java to write this kind of aggregate operation is also more troublesome. This can be aided by the Esproc of the Collector, which is illustrated here by an example.

There is a test set in Mongodb as follows:

>db.test.find ({},{"_id": 0})

{"Value": Numberlong (112937552)}

{"Value": Numberlong (715634640)}

{"Value": Numberlong (487229712)}

{"Value": Numberlong (79198330)}

{"Value": Numberlong (440998943)}

{"Value": Numberlong (93148782)}

{"Value": Numberlong (553008873)}

{"Value": Numberlong (336369168)}

{"Value": Numberlong (369669461)}

...

specific: Thetest collection contains more than one value, and each value is a string of numbers. Each number string is compared to all the other numbers, finding the maximum number of identical and maximum different numbers for each number string. If the line 1 and the nth row contain the number 1, even if there are 1 of the same number , if line 1 has 1 nth row No, even if a different number has 1 of them.

The Collector code is as follows:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5C/24/wKiom1UbWA-C9apKAAGy5Q3NM6U354.jpg "style=" float: none; "title=" esproc_mongodb_digital_1.jpg "alt=" Wkiom1ubwa-c9apkaagy5q3nm6u354.jpg "/>

A1: Connect mongodb,IP and port number is localhost:27017, database is test, username and password are test.

  a2: Using Find test _id find find fetch All records of the travel target.

A3: Add two columns on A2 basis to hold the maximum number of identical and maximum different numbers. At the same time, the value is converted to a string.

A4: For A3 collection loops, the loop body is B4 to D10.

B4: Takes the value of the current loop .

C4: Use a function to split value into a sequence of single characters, removing duplicate values.

B5: One more inner loop for the A3. The loop body is C6 to D10.

C5: If the loop position of the inner loop is equal to the current position of the outer layer, that is , the same value, the inner loop is skipped and the next inner loop is performed.

C6: Gets the value of the inner loop .

C7: Defines two variables same and diff, each storing the same number and different number of this comparison, the initial value is 0.

C8: Use the loop function to find the sequence value of the value split of the outer loop one by one in value in the inner loop. If it can be found, then same add 1, otherwise diff will add 1.

C9,C10: compare same and diff in same and diff with A4, and assign a larger value to A4 in the same and diff.

The operating result is:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5C/1E/wKioL1UbWU7DgSzYAADU6kVqbI8215.jpg "style=" float: none; "title=" esproc_mongodb_digital_2.jpg "alt=" Wkiol1ubwu7dgszyaadu6kvqbi8215.jpg "/>

It is necessary to notethat Esproc does not contain MongoDB 's java driver package. to access MongoDB with Esproc, you must advance the Java driver package for MongoDB (requires 2.12.2 or more, such as Mongo-java-driver-2.12.2.jar) is placed in the [esproc installation directory ]\common\jdbc .

Esproc CollectorAssistMongodbthe computed script is easily integrated intoJava, just add a rowA11, writtenResult A3you can sendJavaOutputResultSetThe result of the form, the specific code referenceEsprocTutorials. Similarly, withJavacalledEsprocAccessMongodbmust also beMongdbof theJavaThe driver package is placedJavaof the programClasspaththe.




This article is from the High performance report data calculation blog, so be sure to keep this source http://report5.blog.51cto.com/8028595/1631734

The digital comparison of the Collector assist MongoDB calculation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.