In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Mongodb脚本解决复杂问题的计算能力有限,直接使用时较为吃力。很多情况下需要将数据读出后在主程序中进一步完成运算,而在Java等高级语言中编写这类集合式运算也比较麻烦。这时可以用集算器esProc来辅助,这里通过一个例子来说明。
Mongodb中有一个test集合如下:
>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) }
…
具体的:test集合包含多个value,每个value是一个数字串。每个数字串都要和另外所有的数字串比较,找出每个数字串的最大相同数和最大不同数。如果第1行和第n行都含有数字1,就算相同数有1个,如果第1行有1第n行没有,就算不同数有1个。
集算器代码如下:
A1: Connect mongodb, ip and port number are localhost:27017, database is test, username and password are both test.
A2: Use the find function to take numbers from mongodb to form cursors. Set is test, filter condition is null, key_id is not taken out. As you can see, esProc uses the same parameter format in the find function as montdb's find statement. esProc cursors support batch reading and processing of data, which can avoid excessive data volume and memory overflow. Because the amount of data is not large, fetch is used to output all the records of the cursor.
A3: Add two columns to A2 to store the maximum number of similarities and the maximum number of differences. At the same time convert value to string.
A4: For A3 set loop, loop body is B4 to D10.
B4: Take the value of the current loop.
C4: Use the function to split the value into a sequence of single characters, removing duplicate values.
B5: Perform another inner cycle on A3. The cycle body is C6 to D10.
C5: If the loop position of the inner loop is equal to the current position of the outer loop, that is to say, the same value, skip this inner loop and proceed to the next inner loop.
C6: Get the value of the inner loop.
C7: Define two variables same and diff, storing the same number and different number of this comparison respectively, and the initial value is 0.
C8: Use the loop function to find the sequence values of the value partition of the outer loop one by one in the values of the inner loop. If it can be found, add 1 to same, otherwise add 1 to diff.
C9, C10: Compare same and diff with same and diff in A4 and reassign the larger to same and diff in A4.
The results of the run are:
Note that esProc does not include the java driver package for mongodb. To access mongodb with esProc, the java driver package for mongodb (version 2.12.2 or above, such as mongo-java-driver-2.12.2.jar) must be placed in [esProc installation directory]\common\jdbc in advance.
The script that the integrator esProc assists mongodb to calculate is easy to integrate into java. Just add a line A11 and write result A3 to output the result set form to java. Refer to esProc tutorial for specific code. Similarly, calling esProc in java to access mongodb requires putting the java driver package for mongodb 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
© 2024 shulou.com SLNews company. All rights reserved.