Week 11.pdf
Document Details
Uploaded by ZippyPelican
null
Tags
Full Transcript
CP476B Internet Computing Week 11 – 1 Introduction to MongoDB Shaun Gao, Ph.D., P.Eng. Objectives Review week 10 topics The Basics about MongoDB MongoDB Architecture MongoDB: Hierarchical Objects MongoDB Processes MongoDB limit Week 10 topics Concept comparison between MongoDB and RDBMS MongoDB CRUD...
CP476B Internet Computing Week 11 – 1 Introduction to MongoDB Shaun Gao, Ph.D., P.Eng. Objectives Review week 10 topics The Basics about MongoDB MongoDB Architecture MongoDB: Hierarchical Objects MongoDB Processes MongoDB limit Week 10 topics Concept comparison between MongoDB and RDBMS MongoDB CRUD operations Demo from DOS command line Node.js and MongoDB Installing the NPM Modules Connection from node.js Node.js MongoDB CRUD operation Introduction A document-oriented database documents encapsulate and encode data (or information) in some standard formats or encoding NoSQL database non-adherence to the widely used relational database highly optimized for retrieve and append operations uses BSON format (BSON - Binary JavaScript Object Notation) schema-less No more configuring database columns with types No data definition language is required The Basics about MongoDB A MongoDB instance may have zero or more databases A database may have zero or more collections. Collection can be thought of as the relation (table) in DBMS, but with many differences. A collection may have zero or more documents. Documents in the same collection don’t even need to have the same fields Documents are the records in RDBMS Documents can embed other documents Documents are addressed in the database via a unique key A document may have one or more fields. The Basics about MongoDB – cont. Simple queries Makes sense with most web applications Easier and faster integration of data Not well suited for heavy and complex transactions systems MongoDB is Faster Anywhere from 2 to 10 times faster than MySQL Example: Mongo Document user = { name: “Shan", occupation: “Scientist", location: “Waterloo" } { name: ‘John Steve’, address: { street: ‘6 Oak Terrace’, city: ‘Waterloo’ } } Key – value pair Queries in MongoDB Query expression objects indicate a pattern to match db.users.find( {last_name: 'Smith’} ) Several query objects for advanced queries db.users.find( {age: {$gte: 23} } ) db.users.find( {age: {$in: [23,25]} } ) Exact match an entire embedded object db.users.find( {address: {street: ‘6 Oak Terrace', city: ‘waterloo'}} ) Dot-notation for a partial match db.users.find( {"address.city": ‘waterloo'} ) Indexing Indexing is a way to optimize the performance of a database by minimizing the number of queries. Indexes in MongoDB are similar to indexes in RDBMS. MongoDB supports indexes on any field or sub-field contained in documents. MongoDB defines indexes on a per collection level. All MongoDB indexes use a B-tree data structure MongoDB Architecture Traditional architecture MongoDB Architecture – cont. A MongoDB server may host many individual databases (A,B,C) Horizontal scalability Mongos - Sharding process manages the distributed MongoDB MongoDB Architecture – cont. Many applications can use the Mongo server simultaneously with no danger of overwriting data MongoDB: Hierarchical Objects A MongoDB instance may have zero or more ‘databases’ A database may have zero or more ‘collections’. A collection may have zero or more ‘documents’. A document may have one or more ‘fields’. MongoDB Processes Mongod – Database instance Mongos - Sharding processes Analogous to a database router. Processes all requests Decides how many and which mongods should receive the query Mongos collates the results, and sends it back to the client. Mongo – an interactive shell ( a client) Fully functional JavaScript environment for use with a MongoDB You can have one mongos for the whole system no matter how many mongods you have. OR you can have one local mongos for every client if you wanted to minimize network latency. How to start and connect to MongoDB Create an empty folder to contain the mongo database files; e.g. c:\data\db (Any folder is OK as long as it is not write-restricted) Start mongo server: "C:\Program Files\MongoDB\Server\5.0\bin\mongod.exe" --dbpath c:\data\db In a separate terminal window, run the mongo client shell to interactively access the database: "C:\Program Files\MongoDB\Server\5.0\bin\mongo.exe“ Limitations of MongoDB No referential integrity Updating something in many places instead of one because its horizontal scalability. Lack of predefined schema Database schema sometimes is required. You must have a model in your app Objects within a collection can be completely inconsistent in their fields Summary The Basics about MongoDB MongoDB Architecture MongoDB: Hierarchical Objects MongoDB Processes MongoDB limit Announcement Project presentation start from next Monday You need to record your presentation (6 minutes) Submit the recording and PowerPoint to myls before the day you present Group project presentation order Mar. 27 (Wednesday): 13, 10, 3 Apr. 1(Monday): Group 5, 7, 2, 6 Apr. 3(Wednesday): Group 12, 8, 4, 14 Apr. 5(Friday): Group 11, 15, 1, 9 Project report – first page indicates either SDD or TR Software design document (SDD) Technical Report (TR) CP476B Internet Computing Week 11 – 2 Course review & project presentation Shaun Gao, Ph.D., P.Eng. Agenda Course Review Apache server PHP server site scripting language Node.JS JavaScript scripting language Summary Announcement Course evaluation Apache server History Initially started in 1996 by Robert McCool. Since 1996, Apache web server has been the most popular HTTP server in the market on the World Wide Web. The Apache was the first web server architecture that was used by the Netscape Communication Corporation. Many programming languages are supported by the Apache Server are as follows: PHP, Perl, Python and alongside with MySql. Apache server – cont. Overview Apache is an open-source HTTP web server. It handles HTTP Requests sent to it and then it is able to them Apache uses a modular approach so that anyone can add to the basic functionality of the server without disturbing the basic Core implementation. Each module can be considered as an independent process Apache server – cont. Multithreaded and Multi-processed Web Servers Web servers that can handle simultaneous requests, manage the requests in two ways. Either they start a new process or they start a new thread within a process. Web servers that start a new process on each request are called multi-processed Web servers, while those that start a new thread within the main process are called multithreaded Web servers. The is a slight difference among those terminology. IIS (Internet Information Services) on the Windows platform is an example of a multithreaded Web server. Apache on a Unix platform is a multi-processed Web server. Apache Server Architecture Apache server can break down the server into two main Components. The Apache Core: Which Handles the Basic functionality of the Server. Such as allocating requests and maintaining and pooling all the connections. The Apache Modules: Which are in a sense the added extensions to the server which handle a lot of the other types of processing the server must achieve such as doing user Authentication. Apache Server Architecture – cont. Apache core component. The core components are a series of classes that handle specific tasks. The Apache Core provides us with the Main functionality of a HTTP web server. It contains the following source code. http_protocol.c: This is the component that handles all of the routines that communicate directly with the client by using the HTTP protocol. http_main.c: this component is responsible for the startup of the server and contains the main server loop that waits for and accepts connections. http_request.c: This component handles the flow of request processing, passing control to the modules as needed in the right order. http_core.c: the component implementing the most basic functionality, it just bairly serves documents. alloc.c: the component that takes care of allocating resource pools, and keeping track of them. http_config.c : this component provides functions for other utilities, including reading configuration files and managing the information gathered from those files (), as well as support for virtual hosts. Apache Server Architecture – cont. Apache core component. Apache Server Architecture – cont. Modules In Apache architecture, modules do not know directly about each other and not one module alone can completely fill or process the request that is made to the Apache server. Most requests are processes by sending the information from one module back to the core then back to another module until the request is completely handled and then it is sent back to the client. Apache has something called Request Phases and is handled by the HTTP_REQUEST component of the core. Modules are connected to the Apache core all the same way. Modules since they do not know directly about each other must pass all in formation back to the core and then the core sends that information to another appropriate module through the use of the HTTP_REQUEST component of the Apache Core. Apache Server Architecture – cont. Modules – cont. The functionality of Apache can be easily changed by writing new modules which complements or replace the existing one. The server is also highly configurable, at different levels and modules can define their own configuration commands. Modules have something inside them that are called Handlers. Handlers: A handler is for Apache the action that must be performed in some phase of servicing a request. For example a handler that requests a file must open the file then read the file then send it to the Apache core to then be sent to the client. Handlers are defined by the modules depending on when they are needed to fulfill a request then the Handlers are the ones that send back the processing from the Apache Module to the Apache Core HTTP_REQUEST component Apache Server Architecture – cont. Modules – cont. Overview of the Handler system within an Apache Module. As you can see the Handler does what it needs to do to fulfill a request then the sends that process back to the HTTP_REQUEST component of the Apache core in order to be sent to another module for processing or back to the client. Concurrency in Apache Apache provides access to two levels of concurrency. The concurrent processes executing truly simultaneously, in the case that they run on separate processors, as in the case of separate processes running on a multitasking system. As well, if multi-threading is supported by the operating system, a default of up to 50 threads is allowed for each process. Course review PHP Server-side scripting language Code is executed on web server Flexible language Support many database systems Open-source software Course review – cont. Node.js New type web server Single process architecture Event-driven architecture Asynchronous programming is required Open-source software Course review – cont. JavaScript Another scripting language Originally it is for front end development, but now it is for both front end and back end development because of node.js Asynchronous programming Promises Dynamic web functionality (AJAX) Course review – cont. VS Code Integrated development environment Compatible with major OS such as Mac, Linux, Windows Easy to use Debug skills Debugging is an important steps in software engineering. VS code and Xdebug provides tools for debugging web applications. Summary Apache + PHP Vs. Node.js + JavaScript Announcement Group project presentation order Mar. 27 (Wednesday): 13, 10, 3 Apr. 1(Monday): Group 5, 7, 2, 6 Apr. 3(Wednesday): Group 12, 8, 4, 14 Apr. 5(Friday): Group 11, 15, 1, 9 Course evaluation