Installation and use of NoSQL database

(1) Completed Redisinstallation and use. Complete database insertion, deletion, and query.

  RedisIt is a key-value ( key-value) storage system, that is, a key-value pair non-relational database, and Memcachedsimilar, which is currently being adopted by more and more Internet companies. RedisAs a high-performance key-value database, it not only makes up for memcachedthe deficiency of this type of key-value storage to a large extent, but also can play a very good supplementary role to relational databases in some occasions. RedisProvides Python, Ruby, Erlang, PHPclients, which are very convenient to use.

①Log in as hadoopa user ubuntu kylin, windowsuse the system to save the file transfer FileZillain compressed format in the directory, and now unzip the package to .redis-5.0.5.tar.gz“/home/hadoop/下载”/usr/local
insert image description here

②Decompress redis-5.0.5.tar.gzthe file and save it to “/usr/local/”the directory. insert image description here
③Rename it redis-5.0.5to redis, and redisgive the directory permission to hadoopthe user.
insert image description here
④Enter “/usr/local/redis”the directory, enter compile and install Redis.
insert image description here
insert image description here
⑤The Redisinstallation has been completed, now start Redisthe server.
insert image description here
⑥Create a new terminal and start Redisthe client. After the client connects to the server, “127.0.0.1:6379>”the command prompt information will be displayed, indicating that IPthe address of the server is 127.0.0.1and the port is 6379. Now you can perform simple operations, such as setting the key to ”hello”the value ”world”, and fetching the value corresponding ”hello”to . At this point, Redisthe installation and operation are successful, and then the database can be operated Redis.
insert image description here
RedisThe database stores data in the form of . When <key,value>storing the data in the table into the database, the determination method of the sum is as follows :Rediskeyvalue

* key=表名:主键值:列名
* value=列值 

⑦Insert data: To Redisinsert a piece of data, you only need to design the sum first key, valueand then use setthe command to insert the data. For example, Courseto insert a new course "Big Data" into the table, 4 credits, the operation commands and results are shown in the figure below.
insert image description here
⑧Delete data: RedisThere is a special command to delete data— delcommand, and the command format is " delkey". Therefore, if you want to delete the previously added course "Big Data", you only need to enter the command “del Course:8:Cname”, as shown in the figure below, when you enter it “del Course:8:Cname”, it will return “1”, indicating that a piece of data has been successfully deleted.
insert image description here
⑨Query data: RedisThe easiest way to query is to use getcommands. Enter getthe command query, if the output is empty, it means that the data is deleted successfully.
insert image description here

(2) MongoDBInstallation and use.

   Complete MongoDBbasic shellcommands. MongoDBIt is a database based on distributed file storage, between relational databases and non-relational databases. It is the most functional among non-relational databases and most similar to relational databases. The data structure it supports is very loose and is a similar jsonformat bson, so it can store more complex data types. MongoThe biggest feature is that the query language it supports is very powerful, and its syntax is somewhat similar to object-oriented query language, which can almost realize most of the functions similar to single-table query of relational database, and also supports indexing of data.
①Using apt-getcommands for online installation MongoDBcan avoid many inexplicable problems.
The command line sudo apt-get install mongodbcan be downloaded and installed MongoDB. The default installed version is MongoDB 2.6.10, but MongoDBit has been upgraded to 3.2.8. You can install the version by adding software sources 3.2.8.

a. First open the terminal, import the public keyto the package manager
insert image description here
b. Create MongoDBa list of files.
insert image description here
c. Update the package manager and install MongoDB.
insert image description here
insert image description here
d. MongoDBAfter the installation is complete, enter the following command in the terminal to view MongoDBthe version.
insert image description here
MongoDBstart and stop.

insert image description here
③Enter the MongoDBcommand shellmode. The database connected by default is testthe database. Before that, make sure that it has been started MongoDB, otherwise an error will occur. After starting, the successful operation is as follows.
insert image description here
④Common operation commands
Database-related
show dbs: display database list
show collections: display collections in the current database (similar to tables in relational databases table)
show users: display all users
use yourDB: switch current database to yourDB
db.help(): display database operation commands
db.yourCollection.help(): display collection operation commands, which yourCollectionis the collection name
insert image description here

MongoDBThere is no command to create a database. If you want to create a “School”database, run use Schoolthe command first, and then do some operations (such as: create a collection db.createCollection('teacher')), so that you can create a “School”database named .
insert image description here
⑤Taking a Schooldatabase as an example, Schoolcreate two collections teacherand sums in the database student, and studentperform basic operations of addition, deletion, modification and query on the data in the collection (a collection Collectionis equivalent to a table in a relational database table).
a. Switch to Schoolthe database (Switch to Schoolthe database. MongoDBThere is no need to pre-create Schoolthe database, it will be automatically created when it is used)
insert image description here
b. Create Collection(create an aggregate collection. MongoDBIn fact, when inserting data, the corresponding collection will also be automatically created, no pre-defined collection is required )
insert image description here
c. Similar to database creation, MongoDBcollections are automatically created when data is inserted. There are two ways to insert data: insertand save.
insert image description here
Insert data successfully
insert image description here

_idSame, update data
insert image description here

_idSame, insert failed, no operation.

d. The structure of the added data is loose, as long as jsonthe format is acceptable, the column attributes are not fixed, and the added data shall prevail. Define the data first and then insert, you can insert multiple pieces of data at one time.
insert image description here

e. After running the above example, studentit has been automatically created, indicating that MongoDBit does not need to be pre-defined, and it will be automatically created collectionafter the first data insertion .collection
insert image description here

⑥ Find data
a. db.student.find()Query all records. Equivalent to:select * from student
insert image description here

b. db.student.find({sname: 'zhangsan'})Query sname='zhangsan'records. Equivalent to:select * from student where sname='zhangsan'
insert image description here

c. db.student.find({},{sname:1, sage:1})Query the specified column snameand sagedata. Equivalent to: select sname,sage from student.

sname:1 means to return snamethe column, and the default _idfield is also returned. It can be added _id:0(meaning not to return _id) to write {sname: 1, sage: 1,_id:0}, and the default _idfield will not be returned
insert image description here

d. db.student.find({sname: 'zhangsan', sage: 22})
andQuery with conditions. Equivalent to:select * from student where sname = 'zhangsan' and sage = 22
insert image description here

e. db.student.find({$or: [{sage: 22}, {sage: 25}]})
orConditional query. Equivalent to:select * from student where sage = 22 or sage = 25
insert image description here

f. db.youCollection.find(criteria, filterDisplay) criteria:
query condition, optional filterDisplay: filter and display part of the data, such as displaying specified column data, optional (when selected, the first parameter cannot be omitted, if the query condition is empty, it can be used as a placeholder {})
insert image description here

⑦ The query conditions db.youCollection.update(criteria, objNew, upsert, multi )

criteria: updatefor , similar to the objects behind sql updatethe query : and some update operators (such as ), can also be understood as the following in the query . : If the record does not exist, whether to insert it or not, the default is to not insert it. : By default , only the first record found will be updated. If this parameter is true, all multiple records detected according to the condition will be updated. By default , only the first matching data is modified. Where and are required parameters, and optional parameters. , which is equivalent to: ;whereobjNewupdate$setsql updateset

upsertupdateobjNewtruefalse
multimongodbfalsefalse

criteriaobjNewupsertmultidb.student.update({sname: 'lisi'}{$set: {sage: 30}}, false, true)
update student set sage =30 where sname = 'lisi'
insert image description here

⑧Delete data db.student.remove({sname: 'chenliu'})Equivalent to :delete from student where sname='chenliu'
insert image description here

⑨Delete collection
insert image description here

⑩Exit shellcommand mode
Input exitor Ctrl+Cexit shellcommand mode
insert image description here
insert image description here

(3) Use Java APIthe pair MongoDBto access.

①The Java MongoDB Driverdriver jarpackage has been downloaded and saved to ’/home/hadoop/下载/’the directory.
insert image description here
② Open Eclipseand create a new one Java Project.
insert image description here
Import the driver package mongodb-driver-3.8.0.jar.
insert image description here
Create new Class Average_grade
insert image description here
insert image description here
Clear Average_grade.javathe code inside, and then enter the complete studentcode for performing the addition, deletion, modification, and query operations of the collection in this file.
insert image description here
After the program runs, Consolethe running result information will be displayed in the " " panel at the bottom.
insert image description here
Each time the program is executed, you can return to shellthe mode to view the results. For example: After eclipseperforming the update operation, shellenter in the mode db.student.find(), you can view studentall the data of the collection.
insert image description here

(4) How is it different Redisfrom traditional Mysqldatabases?

①It mysqlis a relational database, which is mainly used to store persistent data. The data is stored in the hard disk, and the reading speed is relatively slow. redisYes NOSQL, that is, a non-relational database is also a cache database, that is, the data is stored in the cache. The cache reads fast and can greatly improve the operating efficiency, but the storage time is limited.
mysqlAs a relational database for persistent storage, the relatively weak point is that every time a request is made to access the database, there are I/Ooperations, if the database is accessed repeatedly.
First: A lot of time will be spent on repeatedly connecting to the database, resulting in too slow operation efficiency;
Second: Repeated access to the database will also lead to excessive load on the database, so the concept of caching is derived at this time.
③The cache is the buffer ( ) for data exchange cache. When the browser executes the request, it will first search in the cache, and if it exists, it will get it; otherwise, it will access the database. The advantage of caching is that the reading speed is fast.
④The redisdatabase is a cache database, which is used to store frequently used data, so as to reduce the number of access to the database and improve operating efficiency.

(5) MongoDBWhat are the characteristics, and Mysqlwhere is the difference from the database?

Features:
Mongodb It is a non-relational database ( nosql), which belongs to the document database. A document is mongoDBthe basic unit of data in a database. It is similar to a row in a relational database. Multiple key-value pairs are placed together in an orderly manner to form a document. The syntax is somewhat similar to an object-oriented javascriptquery language. It is a set-oriented, free-mode document type database.
Storage method: virtual memory + persistence. Query statement: It is a unique Mongodbquery method. Suitable scenarios: event recording, content management or blogging platform, etc.
Architecture features: High availability can be achieved through replica sets and fragmentation.
Data processing: The data is stored on the hard disk, but the data that needs to be read frequently will be loaded into the memory, and the data will be stored in the physical memory to achieve high-speed reading and writing.
Maturity and breadth: Emerging databases have low maturity. NosqlAmong the databases, they are closest to relational databases and are relatively complete DB. The applicable population is constantly growing.

Difference:
MongoDB Another biggest disadvantage is that it takes up a lot of space. When adding, MongoDBdeleting, and modifying data frequently in the database, if the record changes, such as the data size changes, some data fragments are likely to occur at this time, and fragmentation will cause As a result, one is that the index will have performance problems.
MySQLBoth MongoDBare common open source databases, but MySQLthey are traditional relational databases, MongoDBwhile non-relational databases, also called document databases, are a kind NoSQLof database. They each have their own advantages, the key is to see where they are used. SQLSo the (full name ) statements we are familiar with Structured Query Languageare not applicable MongoDB, because SQLstatements are the standard language of relational databases.

Guess you like

Origin blog.csdn.net/weixin_51571728/article/details/125275880