In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the basic operation of MongoDB cluster". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "the basic operation of MongoDB cluster explanation" bar!
1. Overview
Here I will organize an article to repeat the MongoDB for your reference. The contents of the catalogue are as follows:
Basic operation
CRUD
MapReduce
This article is based on the demonstration on MongoDB Cluster (Sharding+Replica Sets), so the operation is all at the cluster level, so some commands are different from using the MongoDB library alone.
two。 Basic operation
Common Shell commands are as follows:
Db.help () # Database help db.collections.help () # Collection help rs.help () # help on replica set show dbs # Show the database name show collections # Show collections select the database in the current library use db_name #
View the basic information of the collection, as follows:
# View help db.yourColl.help (); # query the number of data items in the current collection db.yourColl.count (); # check the data space size db.userInfo.dataSize (); # get the db db.userInfo.getDB () where the current aggregation set resides; # get the current aggregation status db.userInfo.stats (); # get the total aggregate size db.userInfo.totalSize () # db.userInfo.storageSize (); # Shard version information db.userInfo.getShardVersion () # rename the collection and rename userInfo to users db.userInfo.renameCollection ("users"); # Delete the current collection db.userInfo.drop ()
3.CRUD
3.1 create
In the cluster, we add a friends library with the command as follows:
Db.runCommand ({enablesharding: "friends"})
After the library is created, we create a user shard under the library with the following command:
Db.runCommand ({shardcollection: "friends. User"})
3.2 add
In MongoDB, both save and insert can achieve the new effect. But there is a difference between the two. In the save function, if the original object does not exist, they can insert data into collection; if it already exists, save will call update to update the record, while insert will ignore the operation.
In addition, in insert, you can insert a list at one time without traversing, which is efficient, while save needs to traverse the list and insert it one by one. Here we can look at the prototypes of the two functions. Through the function prototypes, we can see that for remote calls, it is more efficient to post the entire list to MongoDB at one time.
The prototype of the Save function is as follows:
The prototype of the Insert function (part of the code) is as follows:
3.3 query
3.3.1 query all records
Db. User.find ()
By default, 20 records are displayed per page, and when the display is not low, you can use the it iterative command to query the next page of data. Note: you cannot type the it command with ";" but you can set the size of the data displayed per page, using DBQuery.shellBatchSize= 50; that will show 50 records per page.
3.3.2 query the duplicate data of a column in the current aggregate set after the query is removed
Db. User.distinct ("name"); # will filter out the same data in name equivalent to: select distict name from user
3.3.3 query equals conditional data
Db.user.find ({"age": 24}); # is equivalent to: select * from user where age = 24
3.3.4 query is larger than conditional data
Db.user.find ({age: {$gt: 24}}); # is equivalent to: select * from user where age > 24
3.3.5 query less than conditional data
Db.user.find ({age: {$lt: 24}}); # equivalent to: select * from user where age
< 24; 3.3.6查询大于等于条件数据 db.user.find({age: {$gte: 24}}); #相当于: select * from user where age >= 24
3.3.7 query conditional data less than or equal to
Db.user.find ({age: {$lte: 24}}); # equivalent to: select * from user where age = 23 and age 25; db.user.find ({name: 'zhangsan', age: 22}); # equivalent to: select * from user where name =' zhangsan' and age = 22
3.3.13 sort
# ascending order: db.user.find (). Sort ({age: 1}); # descending order: db.user.find (). Sort ({age:-1})
3.3.14 query 5 pieces of data
Db.user.find (). Limit (5); # equivalent to: select * from user limit 5
Data after 3.3.15N
Db.user.find (). Skip (10); # equivalent to: select * from user where id not in (select * from user limit 5)
3.3.16 query records in a certain area
# query data between 5 and 10 db.user.find () .limit (10) .skip (5)
Can be used for paging, limit is pageSize,skip is the number of pages * pageSize.
3.3.17COUNT
Db.user.find ({age: {$gte: 25}}) .count (); # is equivalent to: select count (*) from user where age > = 20
3.3.18 installation result set sorting
Db.userInfo.find ({sex: {$exists: true}}) .sort ()
3.3.19 is not equal to NULL
Db.user.find ({sex: {$ne: null}}) # is equivalent to: select * from user where sex not null
3.4 Index
Create an index and specify the primary key field, as shown in the command:
Db.epd_favorites_folder.ensureIndex ({"id": 1}, {"unique": true, "dropDups": true}) db.epd_focus.ensureIndex ({"id": 1}, {"unique": true, "dropDups": true})
3.5 Update
The format of the update command is as follows:
Db.collection.update (criteria,objNew,upsert,multi)
Parameter description: criteria:
Query conditional objNew:update objects and some update operators
Upsert: if there is no record of update, whether to insert objNew this new document, true is insert, default is false, do not insert.
Multi: default is false, and only the * records found are updated. If true, update all records queried by condition.
Here is an example of updating the value of price in id 1, as shown below:
Db. User.update ({id: 1}, {$set: {price:2}}); # is equivalent to: update user set price=2 where id=1
3.6 Delete
3.6.1 Delete the specified record
Db. User. Remove ({id:1}); # is equivalent to: delete from user where id=1
3.6.2 Delete all records
Db. User. Remove ({}); # is equivalent to: delete from user
3.6.3DROP
Db. User. Drop (); # is equivalent to: drop table user
4.MapReduce
MapReduce in MongoDB is to write a JavaScript script, and then MongoDB parses and executes the corresponding script. The Java API operation MR is given below. The code is as follows:
MongdbManager class, which is used to initialize MongoDB:
Package cn.mongo.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.mongodb.DB; import com.mongodb.Mongo; import com.mongodb.MongoOptions; / * * @ Date Mar 3, 2015 * * @ author dengjie * * @ Note mongodb manager * / public class MongdbManager {private static final Logger logger = LoggerFactory.getLogger (MongdbManager.class); private static Mongo mongo = null; private static String tag = SystemConfig.getProperty ("dev.tag") Private MongdbManager () {} static {initClient ();} / getDB object public static DB getDB (String dbName) {return mongo.getDB (dbName);} / / getDB object without param public static DB getDB () {String dbName = SystemConfig.getProperty (String.format ("% s.mongodb.dbname", tag)); return mongo.getDB (dbName) } / / init mongodb pool private static void initClient () {try {String [] hosts = SystemConfig.getProperty (String.format ("% s.mongodb.host", tag)) .split (","); for (int I = 0; I
< hosts.length; i++) { try { String host = hosts[i].split(":")[0]; int port = Integer.parseInt(hosts[i].split(":")[1]); mongo = new Mongo(host, port); if (mongo.getDatabaseNames().size() >0) {logger.info (String.format ("connection success,host= [% s], port= [% d]", host, port); break;}} catch (Exception ex) {ex.printStackTrace () Logger.error (String.format ("create connection has error,msg is% s", ex.getMessage ());}} / / set connection pool information MongoOptions opt = mongo.getMongoOptions (); opt.connectionsPerHost = SystemConfig.getIntProperty (String.format ("% s.mongodb.poolsize", tag)) / / poolsize opt.threadsAllowedToBlockForConnectionMultiplier = SystemConfig.getIntProperty (String.format ("% s.mongodb.blocksize", tag); / / blocksize opt.socketKeepAlive = true; opt.autoConnectRetry = true;} catch (Exception e) {e.printStackTrace ();}
The MongoDBFactory class, which is used to encapsulate the operation business code, is as follows:
Package cn.mongo.util; import java.util.ArrayList; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import cn.diexun.domain.MGDCustomerSchema; import com.mongodb.BasicDBList; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBObject; import com.mongodb.util.JSON; / * * @ Date Mar 3, 2015 * * @ Author dengjie * / public class MongoDBFactory {private static Logger logger = LoggerFactory.getLogger (MongoDBFactory.class) / / save data to mongodb public static void save (MGDCustomerSchema mgs, String collName) {DB db = null; try {db = MongdbManager.getDB (); DBCollection coll = db.getCollection (collName); DBObject dbo = (DBObject) JSON.parse (mgs.toString ()); coll.insert (dbo);} catch (Exception ex) {ex.printStackTrace () Logger.error (String.format ("save object to mongodb has error,msg is% s", ex.getMessage ());} finally {if (db! = null) {db.requestDone (); db = null } / / batch insert public static void save (List mgsList, String collName) {DB db = null; try {db = MongdbManager.getDB (); DBCollection coll = db.getCollection (collName); BasicDBList data = (BasicDBList) JSON.parse (mgsList.toString ()); List list = new ArrayList () Int commitSize = SystemConfig.getIntProperty ("mongo.commit.size"); int rowCount = 0; long start = System.currentTimeMillis (); for (Object dbo: data) {rowCount++; list.add ((DBObject) dbo) If (rowCount% commitSize = = 0) {try {coll.insert (list); list.clear () Logger.info (String.format ("current commit rowCount = [% d], commit spent time = [% s] s", rowCount, (System.currentTimeMillis ()-start) / 1000.0);} catch (Exception ex) {ex.printStackTrace () Logger.error (String.format ("batch commit data to mongodb has error,msg is% s", ex.getMessage ());} if (rowCount% commitSize! = 0) {try {coll.insert (list) Logger.info (String.format ("insert data to mongo has spent total time = [% s] s", (System.currentTimeMillis ()-start) / 1000.0));} catch (Exception ex) {ex.printStackTrace () Logger.error (String.format ("commit end has error,msg is% s", ex.getMessage ());} catch (Exception ex) {ex.printStackTrace (); logger.error (String.format ("save object list to mongodb has error,msg is% s", ex.getMessage () } finally {if (db! = null) {db.requestDone (); db = null;}
LoginerAmountMR class, which is a MapReduce calculation class that counts the number of logged-in users. The code is as follows:
Package cn.mongo.mapreduce; import java.sql.Timest import java.util.ArrayList; import java.util.Date; import java.util.List; import org.bson.BSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import cn.diexun.conf.ConfigureAPI.MR; import cn.diexun.conf.ConfigureAPI.PRECISION; import cn.diexun.domain.Kpi; import cn.diexun.util.CalendarUtil; import cn.diexun.util.MongdbManager; import cn.diexun.util.MysqlFactory; import com.mongodb.DB; import com.mongodb.DBCollection Import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.MapReduceOutput; import com.mongodb.ReadPreference; / * * @ Date Mar 13, 2015 * * @ Author dengjie * * @ Note use mr jobs stats user login amount * / public class LoginerAmountMR {private static Logger logger = LoggerFactory.getLogger (LoginerAmountMR.class); / / map function JS string concatenation private static String map () {String map = "function () {" Map + = "if (this.userName! =\"\ ") {"; map + = "emit ({" + "kpi_code:'login_times',username:this.userName," + "district_id:this.districtId,product_style:this.product_style," + "customer_property:this.customer_property}, {count:1});"; map + = "}"; map + = "}" Return map;} private static String reduce () {String reduce = "function (key,values) {"; reduce + = "var total = 0;"; reduce + = "for (var item0)
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.