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 > Database >
Share
Shulou(Shulou.com)06/01 Report--
I. get to know mongodb
MongoDB is a product between relational database and non-relational database, which is the most functional and most like relational database in non-relational database. It supports a very loose data structure, which is similar to json's bson format, so it can store more complex data types. The most important feature of Mongo is that the query language it supports is very powerful, and its syntax is somewhat similar to the object-oriented query language. It can almost achieve most of the functions similar to the single table query of relational database, and also supports the establishment of data indexing.
II. Spring boot Project Integration mongodb
1. Add mongodb dependency
Org.springframework.boot spring-boot-starter-data-mongodb
two。 Configure the connection for mongodb
Spring: data: mongodb: # uri: mongodb://localhost:27017/data_exploration uri: mongodb://root:dhcc-mongodb@192.168.100.87:27017/data_exploration?authSource=admin&authMechanism=SCRAM-SHA-1
Parsing: the above uri represent local configuration and remote connection, respectively
3. Operate the database
(1) Save
@ Repositorypublic class ExplorationJobDao {@ Autowired MongoTemplate mongoTemplate; public void save (ExplorationJob explorationJob) {mongoTemplate.save (explorationJob);}}
(2) modify a piece of data according to ID (the principle first conforms to the data of ID, and then delete the first item of the query result)
Public void updateExecutionStatusById (int executionStatus, String jobId) {Query query = new Query (Criteria.where ("jobId") .is (jobId)); Update update = new Update () .set ("executionStatus", executionStatus); mongoTemplate.updateFirst (query, update, ExplorationJob.class);}
(3) modify multiple pieces of data according to the bar (query all data that conform to ID, and then modify all data)
Public void update (BusinessExploration businessExploration) {Query query = new Query (Criteria.where ("_ id") .is (businessExploration.getId ()); Update update = new Update (). Set ("sourceUnit", businessExploration.getSourceUnit ()) .set ("appSystem", businessExploration.getAppSystem ()) .set ("businessImplication", businessExploration.getBusinessImplication ()) .set ("safetyRequire", businessExploration.getSafetyRequire ()) MongoTemplate.updateMulti (query, update, TableExploration.class);}
(4) Delete (delete according to ID)
Public void delExplorationJobById (String jobId) {Query query=new Query (Criteria.where ("jobId") .is (jobId)); mongoTemplate.remove (query,ExplorationJob.class);}
(5) query according to conditions (according to ID query)
Public ExplorationJob getExplorationJobByJobId (String jobId) {Query query = new Query (Criteria.where ("jobId") .is (jobId)); ExplorationJob explorationJob = mongoTemplate.findOne (query, ExplorationJob.class); return explorationJob;}
(6) inquire about all
MongoTemplate.findAll (TableExploration.class)
(7) Multi-conditional dynamic query
Public List getExplorationByCondition (ExplorationJob explorationJob) {Query query = new Query (); if (explorationJob.getJobName ()! = null) {Pattern pattern = Pattern.compile ("^. *" + explorationJob.getJobName () + ". * $", Pattern.CASE_INSENSITIVE); query.addCriteria (Criteria.where ("jobName") .regex (pattern)) } if (explorationJob.getDsType ()! = null) {query.addCriteria (Criteria.where ("dsType") .is (explorationJob.getDsType ());} if (explorationJob.getExecutionStatus ()! = null) {query.addCriteria (Criteria.where ("executionStatus") .lte (explorationJob.getExecutionStatus ();} List explorationJobs=mongoTemplate.find (query, ExplorationJob.class); return explorationJobs }
(8) query maximum
Public Date getMaxExplorationDate (String tableName) {FindIterable iterable = mongoTemplate.getCollection ("tableExploration") .find (new BasicDBObject ("tableName", tableName)) .sort (new BasicDBObject ("explorationDate",-1)) .skip (0) .limit (1); Document doc= null; if (getDocuments (iterable). Size () > 0) {doc=getDocuments (iterable) .get (0) Date date = doc.getDate ("explorationDate"); return date;} else {return null;}} / * * tool method * * @ param iterable * @ return * / public static List getDocuments (FindIterable iterable) {List results = new ArrayList () If (null! = iterable) {MongoCursor cursor = iterable.iterator (); Document doc = null; while (cursor.hasNext ()) {doc = cursor.next (); results.add (doc);}} return results;}
(9) grouping query (sorting is still used here)
Public List getAllTableExplorationGroupByTableName (String jobId) {Aggregation aggregation = Aggregation.newAggregation (Aggregation.match (Criteria.where ("jobId") .is (jobId)), Aggregation.sort (new Sort (Direction.DESC, "explorationDate")) Aggregation.group ("tableName"). First ("_ id"). As ("tableName"). First ("databaseType"). As ("databaseType"). First ("databaseName"). As ("databaseName"). First ("networkSituation"). As ("networkSituation") .fir st ("userName") .as ("userName") .first ("password"). As ("password"). First ("url"). As ("url"). First ("dataStorage"). As ("dataStorage"). First ("dataIncrement"). As ("dataIncrement"). First ("explorationDate"). As ("explorationDate") / / .push ("columnExplorations") .as ("columnExplorations") .first ("jobId") .as ("jobId")) AggregationResults aggregationResults= mongoTemplate.aggregate (aggregation, "tableExploration", TableExploration.class); List tableExplorations=aggregationResults.getMappedResults (); return tableExplorations [summary of common operations]
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.