Arlon's CSUMB Intro to Database Systems CST-363 Module 6 Learning Journal #6/#22 for the week Wed 06/02-Tues 06/08, year 2021

Arlon's CSUMB Intro to Database Systems CST-363 Module 6 Learning Journal #6/#22 for the week Wed 06/02-Tues 06/08, year 2021

Two Prompts for week 6:

When would you use a relational database such as MySQL or Oracle versus some other NoSQL database?

I went to town with Mongo DB this week but I would not throw out MySQL just yet. Mongo DB is fun but I don't actually know - how - you would - actually connect - it into your PHP website. Right now my web stack is, well LAMP with PHP. After Spring Boot Server week - I almost wonder if Spring Boot Server is also a feasible addition to the stack as well. PHP with MySQL is a piece of cake to get up and running live, well it helps if you have a hosting account - but most Hosting providers that I know of let you host PHP but not necessarily Java - then there's home hosting - I'm trying to aim that direction - still don't know all the nuances to port forwarding - especially not from behind several routers I don't want to physically get at and can't remotely get at because they're already locked into their configuration. How is that for a tangent. So anyway, my answer for now is - mostly always - I'll stick with MySQL unless I come up with a specific application to throw Mongo DB at. I love Mongo DB, but I don't exactly know how I could replace MySQL with it.

How would you decide whether to use a document database like MongoDB versus a wide column database like Cassandra?

I don't care where they say they got the name for Cassandra, it came from Wayne's World. It's sort of tough to say when I would use Cassandra because I haven't yet used Cassandra. But it seems interesting and also simple, according to the lecture it is based on MySQL syntax and has a very simple storage system. Mongo DB seems more versatile and like putty to me, perhaps because it uses JavaScript, and you can put whatever additional data you want in anywhere you want, any way you want. The lecture mentioned Cassandra can have very dynamic column names which are transformed and stored as row headers.

Actual Journal Notes:

Wow

of

JavaScript of

I learned an extremely missing JavaScript syntax I didn't know until this week. I've been calling myself an expert JavaScripter - here - I'd never even seen this so important syntax of of in JavaScript!! Until this week, in database class.

It's so important! I'd been using

for(x in a){...a[x]....}
when all along I could have just
for(x of a){...x...}
and I'd actually looked for that - and never found it - so thank you so much for that so important for-each I was missing in JavaScript!!!!!

I will use this - all over the place - starting this week!!!!!

I think it's so cool, I may even run through AccountBlaster and update it with that in places where I can.

AccountBlaster definitely has for(x in a){...a[x]....} all over the place and the 'of' paradigm to replace it would be SOOO much better: for(x of a){...x...} Very exciting - new better ways of programming - and in my favorite language no less so thank you a million times for that

Interestingly designed quiz this week - some thought had to have gone into that - hard - but with funny kind of a threshold, and designed so you learn what the answers are, that you didn't know, too, rather than just being a quiz - it's a teaching quiz. I still don't fully understand the one that had the question that lists three lock sequences and asks which if any of them are two-phase locking - but doesn't explicitly show any of them doing any two-phase locking.

You gotta love mongo db!

First of all, Mongo db is semi-human readable - that's why I like JSON as data - I have my own framework that uses JSON as data on both server and client, the JSON being created by a Java builder program that I made that goes with it. I chose JSON for communication between programs because humans can read it - which is helpful for so many reasons - troubleshooting problems - common sense human examination of the data - just seeing what's there - lots of reasons.

I was practically instantly up and running with it, like it already all made sense, off to the races! It took me a coule of days to wrap my mind around the problems and get how I thought it worked to be how it actually worked but once I figured out all the syntax and where everything goes, it's 99% javascript (with some exceptions) and it's really pretty fun to work with. I couldn't get setTimeout() like JavaScript has - it was for a hack it turned out I didn't need anyway - but that's when I realized it wasn't exactly pure JavaScript. I got mostly everything else I tried though, with a little bit of differences with how I instantiated the framework object. Pure JavaScript I'm pretty sure I could do it in one line, Mongo made me use two. (Not counting all the stuff in the middle as part of that line obviously, the line that makes the framework which is like:

var helloMongo_363={
  // ...
  ,init:(instance)=>{
    // print('Hello World');
    // ...
  }
};
helloMongo_363.init(helloMongo_363);
whereas if it were browser JavaScript you could just say:
var helloJavaScript_testFramework={
  // ...
  test:()=>{
      console.log('line 4');
  }
  ,init:function(){
     setTimeout(()=>{
        // print('Hello World');
        // helloJavaScript_testFramework.something.(...
        helloJavaScript_testFramework.test();
     },0);
    return this;
  }
}.init();
That actually works in browser JavaScript - but not without the timeout.)

And I'm using the Mint 20.1 Ubuntu release for Mongo DB which is 3.6.3, I noticed other people have version 4 in their Windowses - it could be a newer Mongo DB has a different JavaScript engine.

So anyways, back to Mongo DB, I think the system is very well thought out and easy to pick up once you understand the basics. I only got stuck a couple of times when reduce wasn't get called because there were single member groups and when I just wasn't connecting things right - but the system is really easy to troubleshoot - send troubleshooting messages through with the rest of the values! You can figure stuff out pretty quick with it, JavaScript style. And it's so great that it uses JSON - actually JSON-Like - structure - for data structure!! JSON is all you need!

One really interesting thing about that - it uses - not JSON - but a JSON-LIKE-structure. I bet - that's so you don't have to quote the labels! That is exactly what I did with my own WebBlaster framework - it also uses something very similar - not JSON - but JSON-LIKE-data-structures - and I know - the reason I did that was because I didn't want the quotes around the labels which JSON requires. ie mongo/webblaster json-like is like: {a:'b'} whereas JSON would be {"a":"b"} with the label quoted too, and it actually requires double quotes, too, which is, I guess to me just a bit more on the eyeballs than single quotes if you want it more human-readable. To me it makes sense to quote the material the label labels, but not the label, kindof like how we don't quote variable names in Java.

Gotta throw out props to Mint for really coming through for the OS - hit window key - menu comes up. You can bind that in Knoppix but I haven't tried it for a while because in 8.1 you could bind it but the bind would stop working which irritated me. So Mint: Thanks for consistency and a rock solid OS I can carry through school between classes, installing Apache, Wine, (BTW not through the software manager, in console just

sudo apt install wine64
but for some reason wine from the software manager chokes. That's important to know - it got me for a while - wine is super important - step 1: wine, step 2: Notepad++. Very important. I don't know why Notepad++ is a windows app, but it is, and it's up there with my very top favorite softwares.) I still of course give props to Knoppix as being a rock solid OS I fall back on to see if Mint is working right! Ha! But I can' get some things on it like for example, VirtualBox, AndroidStudio, MySQL Workbench, Spring Boot Framework (actually that might work on Knoppix, I'm not sure) - generally 64 bit - only programs - Chrome - Knoppix has Chromium - which is almost the same but can't run Netflix - minorly important but important. Anyways and recently I added Slax to the list of extremely useful Debian derivants, and figured out how to triple boot Knoppix, Mint, and Slax on one flash drive so I can pick a different one if I need to.

But - I always have sound - fun-ness - ask people - how many times do I go into a meeting and come back because something about my sound system isn't working - in - mic - out - speakers/headphones - I don't blame Mint per se - and really that's what keeps me going anyway - broken stuff - my dad's like that too - if it's broken - for some reason, that's better - it gives us something to fix. And when it comes down to it I always make it work by changing audio input to and fro basically and it works. Anyway - I was just saying - I'm always having sound issues - could be me - could be my chincy hardware - could be Mint - my guess it's a little of all of the above - story of my life anyway. If the Mint engineers want something to look into - test out older but decent hardware with a webcam, firefox, chrome, zoom, google meet, youtube, RamBox, it all usually mostly works, but, just saying, I have to - like - mess with and re-mess with the pulse audio sound settings a lot of times to make it work. It does work better on my newer laptop, but yeah, that's no fun, I'd rather test something more broken.

BTW it took me a minute to figure out so if anyone else wants to know, the difference between unlabeled output and labeled output is: Unlabeled:

return count;
Labeled:
return {count:count};
(in the reduce function return call, and maybe in the map function too, depending on if there are going to be single-member groups or not).

Here's one way you could manage a big set of operations for Mongo DB with a mini-JavaScript (Mongo JavaScript) Framework:



var helloMongo_363={
  api:{
    showFind:(i,collectionName,what)=>i.api.show(i.api.find(collectionName,what))    
    ,find:(collectionName,what)=>eval('db.'+collectionName+'.find(what)')    
    ,showOne:(collection_section,collectionName)=>printjson(eval('db.'+collectionName+'.findOne(collection_section)'))    
    ,show:(collection_section)=>{while(collection_section.hasNext())printjson(collection_section.next());}
    ,reduce:function(collectionName,mapF,reduceF,temp_new_output_collection_name){
      eval('db.'+collectionName+'.mapReduce(mapF,reduceF,{out:\''+temp_new_output_collection_name+'\'})');}
    ,reduce2object:function(collectionName,mapF,reduceF,temp_new_output_collection_object){
      eval('db.'+collectionName+'.mapReduce(mapF,reduceF,{out:temp_new_output_collection_object})');}
    ,showReduce:(i,collectionName,mapF,reduceF)=>{
      var temp_new_output_collection_name='temp_out_collection';

      eval('db.'+collectionName+'.mapReduce(mapF,reduceF,{out:\''+temp_new_output_collection_name+'\'})');
      eval('var showNext_cursor=db.'+temp_new_output_collection_name+'.find()');
      
      i.api.show(showNext_cursor);
    }
  }
  ,examples:{
    class_example_4:(i)=>i.examples.joins.class_example_4.two(i)
    ,basic:{
      everything:{
        customers:{
          zero:(i,what)=>{  //  ...show a customer. 
            print('\nShow a customer:');
            // printjson(db.customers.findOne({customer_name:'US Postal Service'}));
            i.api.showOne(what,'customers');
          }
          ,one:(i)=>{  //  ...show the customers.
            print('\nShow the customers:');    //    Label what's happening
            // i.api.show(db.customers.find());    //    That just shows all of them
            // i.api.show(i.api.find('customers'));    //    That just shows all of them
            i.api.showFind(i,'customers');    //    That just shows all of them
          }
          ,five:(i)=>{  //  Show the zip codes that start with 9 and how many customers are in that zip code using map reduce.  
            //  ...show the customers. Now with a map reduce.
            print('\nHomework Problem #2:');//fully labelled
            print('Show the customers zip codes and count of how many of that zip code with a map reduce, labelled count:');
            //  ... (insert answer here!)
          }
        }
      }
    }
    ,joins:{
      class_example_4:{
        two:(i)=>{    //    Refactor Zero: students, instructors, grades, join to get grades/instructors - by reducing into a third temporary table
          print('\nJoin Class Example 4: Students, instructors, grades, join to get grades/instructors - by reducing into a third temporary table:');
          i.api.reduce('students'
            ,mapF1=function(){
              var grades={A:4.0, B:3.0, C:2.0, D:1.0, F:0.0}; // hashmap of grade values
              for(course of this.courses){
                grade = grades[course.grade]
                emit(course.courseid, {grade_sum:grade, grade_count:1} );
              }
            }
            , reduceF1=function(key,values){
              sum = 0,count = 0;
              for(x of values){
                sum += x.grade_sum;
                count += x.grade_count;
              }
              return {grade_sum:sum, grade_count:count}
            },'temp1');
          
          i.api.reduce2object('instructors'
            ,mapF2=function(){
              for (courseid of this.courses)emit(courseid, {name:this.name});
            }
            ,reduceF2=function(key,values){
              sum = 0,count = 0,name = '';
              for(x of values){
                if('grade_sum' in x){
                  sum += x.grade_sum;
                  count += x.grade_count;
                }
                if('name' in x)name = x.name;
              }
              return {grade_sum:sum, grade_count:count, name:name};
            }
            ,{reduce:'temp1'}
          );
          
          print('\nAverage grade by instructor:');
          i.api.showReduce(i,'temp1'
            ,mapF3=function(){
              emit(
                this.value.name
                ,{  grade_sum:this.value.grade_sum
                  ,grade_count:this.value.grade_count
                  ,average:this.value.grade_sum/this.value.grade_count  // *new line:*
                }  /*  average added-in here \ to work with Mongo DB 3.6.3 otherwise - */
              );  /*  if reduced doesn't need to be called - only one element - it isn't. [workaround/fixed]*/
            }  /*  later versions of Mongo DB perhaps call reduce anyway. Version 3.6.3 does not.*/
            ,reduceF3=function(key,values){  /*  Starting off with Average already in */
              var sum=0,count=0;          /*    the Map Function fixes that.    */
              for(x of values){
                sum += x.grade_sum;
                count += x.grade_count;
              }
              return {grade_sum:sum, grade_count: count, average:sum/count};
            }
          );
        }
      }
      ,customers_orders:{
        three_fully_labelled:(i)=>{    //    Zip Codes, No. items sold in that zip code. Zip Code/Quantity Items. Over Customers, Orders

          print('\nHomework Problem No.3:');
          print('Join 2 from Class Example 4: Orders, Customers, zip codes, join to get zip codes/orders - by reducing into a third temporary table:');
          print('...ie: Count of orders per zip code:');

          //  ... (insert (labelled) answer here!)

        }
        ,four_no_labels:(i)=>{    //    Zip Codes, No. items sold in that zip code. Zip Code/Quantity Items. Over Customers, Orders

          print('\nHomework Problem No.3:');//  Join orders, customers, zip codes, to get zip codes/orders - by strategy of reducing into a third temporary table:
          print('Join 2 from Class Example 4: Orders, Customers, zip codes, join to get zip codes/orders - by reducing into a third temporary table:');
          print('Join orders, customers, zip codes, to get zip codes/orders - by strategy of reducing into a third temporary table:');
          print('...ie: Count of orders per zip code:');

          //  ... (insert answer - no labels - here!)

        }
      }
    }
    ,extra_credit:{
      average_quantity_of_all_orders:{
        zero:(i)=>{  // Homework Extra Credit Problem ? The Average_quantity_of_all_orders.
          //  ie, this order's quantity is 5:
          //  {itemNo:1,qty:4},{itemNo:2,qty:1}

          print('\nHomework Extra Credit Problem');
          print('Average Quantity Per Order, including all orders:');

          //  ... (insert answer here!)
          
        }
      }
    }
  }
  ,mo:(i)=>{
    // db.customers.findOne({customer_name:'US Postal Service'});
    // print('Hello World!');
    // i.examples.basic.everything.customers.zero(i,{customer_name:'US Postal Service'})
    // i.examples.basic.everything.customers.one(i);;  // customer example
    // i.examples.basic.everything.customers.five(i);  // Homework Problem No.2 labelled with zip code labels too
    // i.examples.joins.class_example_4.two(i);  // class_example_4 - with average added into the map to fix 'the bug' (missing average)?
    i.examples.class_example_4(i);	// class_example_4 - with average added into the map to fix 'the bug' (missing average)?
    // i.examples.joins.customers_orders.three_fully_labelled(i);  // Homework Problem No.3 finished
    // i.examples.joins.customers_orders.four_no_labels(i);  // Homework Problem No.3 finished
    // i.examples.extra_credit.average_quantity_of_all_orders.zero(i);  // Homework Extra Credit Problem
  }
  ,init:(instance)=>{
    // print('Hello World');
    instance.mo(instance);
  }
};
helloMongo_363.init(helloMongo_363);

This is my mongo db test framework (mongo) JavaScript script.

Just to be clear this does NOT have any actual class homework answers embedded in it - it does have a couple basic examples and the fixed class example 4 embedded in it, and plenty of room in the design to add tons of other features, that's the point of doing it like that. (I have a version which does have my homework answers embedded in it, but that is not this.)

I post that because it's a cool way of building a small framework in JavaScript and you can use it to build a program that has lots of tricks and that references them logically in Mongo DB JavaScript!!!

Thanks for reading!

Comments

Popular posts from this blog

Service Learning Journal 56, Friday week 8 CST-462s Race,gender, Class in the Digital World

Module 2 Learning Journal 1-19-21

Arlon's CSUMB Intro to HTML CSS JavaScript Node PHP and Web Programming CST-336 Modules 8 Learning Journal #8/#48 for the week Wed 12/15-Tues 12/21, year 2021