<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by João Torres on Medium]]></title>
        <description><![CDATA[Stories by João Torres on Medium]]></description>
        <link>https://medium.com/@jootorres_11979?source=rss-6a9a21d69804------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*CuSlunnKILaFScj0</url>
            <title>Stories by João Torres on Medium</title>
            <link>https://medium.com/@jootorres_11979?source=rss-6a9a21d69804------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 14:21:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jootorres_11979/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Uploading a Node.js app to the IBM Cloud on Ubuntu 18.04]]></title>
            <link>https://medium.com/@jootorres_11979/uploading-a-node-js-app-to-the-ibm-cloud-on-ubuntu-18-04-3fbc0a049e44?source=rss-6a9a21d69804------2</link>
            <guid isPermaLink="false">https://medium.com/p/3fbc0a049e44</guid>
            <category><![CDATA[ibm]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[apps]]></category>
            <category><![CDATA[api]]></category>
            <dc:creator><![CDATA[João Torres]]></dc:creator>
            <pubDate>Tue, 04 Feb 2020 17:11:25 GMT</pubDate>
            <atom:updated>2020-02-04T17:11:25.236Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is the IBM Cloud?</h3><p>The<strong> IBM</strong> <strong>Cloud</strong> is a platform that helps developers build and run modern apps and services. It provides developers with instant access to the compute and services they need to launch quickly, iterate continuously and scale with success.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z3-Iq6jwyFg6PgXqgbWcYQ.png" /></figure><p><strong>What is Cloud Computing?</strong></p><p>In the simplest terms, <strong>cloud computing</strong> means storing and accessing data and programs over the Internet instead of your <strong>computer’s</strong> hard drive. The <strong>cloud</strong> is just a metaphor for the Internet. … When <strong>you</strong> store data on or run programs from the hard drive, that’s called local storage and <strong>computing</strong>.</p><h3>Pre-Requirements</h3><ul><li>An IBM Cloud account (if you don’t have one it’s pretty easy and quick to create one)<strong>;</strong></li><li>A developed API (Just a simple one will do, if you don’t have one you can follow my previous tutorial in order to create one, just follow the link: <a href="https://medium.com/@jootorres_11979/creating-a-crud-app-using-node-js-and-mongodb-on-ubuntu-18-04-416ae5820d12"><strong><em>https://medium.com/@jootorres_11979/creating-a-crud-app-using-node-js-and-mongodb-on-ubuntu-18-04-416ae5820d12</em></strong></a>);</li><li><em>Ubuntu </em>installed on a Virtual Machine.</li></ul><h3>Step 1: Creating a resource on the IBM Cloud</h3><p>a) After creating your account, login in to <strong><em>IBM Cloud</em></strong>, let’s create a <strong><em>Cloud Foundry app </em></strong><em>(SDK for Node.js)</em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7sBLpDBI_Lonn2I6koarYA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fEaXlIlXDk_xefanOlDIoQ.png" /></figure><p>Then, pick the .js option, this one:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*Hqz8rpGSj2yaOcduFMm2OQ.png" /></figure><p>c) Fill in the blankets and chose a <strong><em>256 MB plan.</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/747/1*AXlQP5i6BTS9j5uj6SLhzg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/594/1*25537-WYvs9KIkYAGiSBNA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/521/1*wICli7_u1PQXClonFCC0qA.png" /></figure><h3>Step 2: Installing IBM Cloud on Ubuntu</h3><p>a) First we need to install <strong><em>curl on Ubuntu</em></strong></p><pre>user@ubuntu:~$ <strong>sudo apt-get update<br></strong>user@ubuntu:~$ <strong>sudo apt install curl<br></strong>user@ubuntu:~$ <strong>curl --version</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/579/1*iAjRaR4ArezBd3TbGrwHpg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*1I1ymdYOaGx4brEnkl-Q0A.png" /></figure><p>b) To <strong>install</strong> <strong><em>IBM Cloud</em></strong> on the Virtual Machine, use the following command:</p><pre>user@ubuntu:~$ curl -fsSL <a href="https://clis.cloud.ibm.com/install/linux">https://clis.cloud.ibm.com/install/linux</a> | sh</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*oT1XtsXGshWZRD2TSu0pqg.png" /></figure><h3>Step 3 — Configuring and Preparing the app files</h3><p>a) Create a .<strong>cfignore </strong>file inside the API directory<strong>. </strong>Just use:</p><pre>user@ubuntu:~/(api directory))$<strong> nano .cfignore</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/578/1*Vaq_zDdRO98d6l-MSd73Pg.png" /></figure><p>and insert this line on the file:</p><pre>node_modules/        -&gt; acrescentar a seguinte linha de comando</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*yyKHcwcDXX2kzviZ_RHR4g.png" /></figure><p>b) Now create a <strong>manifest.yml</strong> file. This file will contain some of the app’s information. Use this command:</p><pre>user@ubuntu:~/(api directory))$<strong> nano manifest.yml</strong></pre><p>And insert this:</p><pre>applications:<br>- name:&lt;name of your app&gt;<br>  random-route: true<br>  mamory: 256M<br>  buildpack: https://github.com/cloudfoundry/nodejs-buildpack</pre><p>The app name on “<em>manifest.yml”</em> must be the same name you gave to your previously created resource.</p><h3>Step 4: Login on IBM CLoud</h3><p>In order to login on IBM Cloud use this command:</p><pre>user@ubuntu:~/(api directory))$<strong> ibmcloud login</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/578/1*-YIH53TvoIdafbmP3RYs3w.png" /></figure><p><strong>a)</strong> Now let’s execute the following command to indentify the <strong><em>org </em></strong>and the <strong><em>space</em></strong>:</p><pre>user@ubuntu:~/(api directory))$<strong> ibmcloud target --cf</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/574/1*Xwq_mTBgRfohdSos7Wyb3w.png" /></figure><p><strong>b)</strong> Insert this command to insert and start the resources that have been created on the<strong> I<em>BM Cloud </em></strong>platform:</p><pre>user@ubuntu:~/(api directory))$<strong> ibmcloud cf apps</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/579/1*KU1H_7Sq8IT4Py3LyaMyRw.png" /></figure><p><strong>NOTE:</strong> <em>I’m covering my e-mail address for privacy purposes, but just remember, on my screenshots where there is a red line, it should be your e-mail appearing there.</em></p><p><strong>c)</strong> Now we access the index.js file and add the following code:</p><pre>user@ubuntu:~/(api directory))$<strong> gedit index.js</strong></pre><p>and add the following line:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/384/1*08VdCydPwDoO8GCbeZUjlg.png" /></figure><p>You can just add it at the end of the file.</p><p><strong>d)</strong> Now we <strong><em>push </em></strong>the to the <strong><em>IBM Cloud</em></strong>, using the following command:</p><pre>user@ubuntu:~/(api directory))$<strong> ibmcloud cf push (app)</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/621/1*vy_N2-1LfFOOMdMDrOagnQ.png" /></figure><p><strong>e)</strong> Now go to your IBM Cloud account in order to access your app. This is the place, follow my lead:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/599/1*Law5_8bLMeS_hLdHowza9Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/1*5-UdS85EZy385Z9HJu6Amw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/864/1*IZ30iBRFBOhwXJaPinP35w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/782/1*NV-6fAknqmFNtbJQfrkfXw.png" /></figure><p>As you can see, the API is running from the Cloud.</p><h3>Final Considerations</h3><p>Hopefully you were able to do all these steps. If you did, congratulations, you now have your own app on the Cloud!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3fbc0a049e44" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating a CRUD app using Node.js and Mongodb on Ubuntu 18.04]]></title>
            <link>https://medium.com/@jootorres_11979/creating-a-crud-app-using-node-js-and-mongodb-on-ubuntu-18-04-416ae5820d12?source=rss-6a9a21d69804------2</link>
            <guid isPermaLink="false">https://medium.com/p/416ae5820d12</guid>
            <category><![CDATA[students]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[mongodb]]></category>
            <category><![CDATA[apple]]></category>
            <category><![CDATA[ubuntu]]></category>
            <dc:creator><![CDATA[João Torres]]></dc:creator>
            <pubDate>Tue, 04 Feb 2020 04:23:19 GMT</pubDate>
            <atom:updated>2020-02-04T04:23:19.833Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is Node.js?</h3><p><strong>Node.js</strong> is an <a href="https://en.wikipedia.org/wiki/Open-source_software">open-source</a>, <a href="https://en.wikipedia.org/wiki/Cross-platform">cross-platform</a>, <a href="https://en.wikipedia.org/wiki/JavaScript">JavaScript</a> runtime environment that executes JavaScript code outside of a browser. Node.js lets developers use JavaScript to write command line tools and for <a href="https://en.wikipedia.org/wiki/Server-side_scripting">server-side scripting</a> — running scripts server-side to produce <a href="https://en.wikipedia.org/wiki/Dynamic_web_page">dynamic web page</a> content before the page is sent to the user’s web browser.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/1*tXWaWxRqLAdJ_cBMn_qyLQ.png" /></figure><p><strong>In this tutorial we will try to use Node.js to create a simple CD Collection app.</strong></p><p>We will also use the <strong>Express framework</strong> and the <strong>MongoDB</strong> database.</p><ul><li><strong>Express:</strong> it’s a <a href="https://en.wikipedia.org/wiki/Web_application_framework">web application framework</a> for <a href="https://en.wikipedia.org/wiki/Node.js">Node.js</a>, designed for building <a href="https://en.wikipedia.org/wiki/Web_application">web applications</a> and <a href="https://en.wikipedia.org/wiki/API">APIs</a>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/302/1*FCm-RRJQPdgS0ZvqcHHMGQ.png" /></figure><ul><li><a href="https://www.mongodb.com/"><strong>MongoDB</strong></a><strong>:</strong> It’s the database where we will save our application data.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/780/1*dqiYC6CSouYCwg3ESYsP2w.png" /></figure><ul><li><a href="https://en.wikipedia.org/wiki/Create,_read,_update_and_delete"><strong>CRUD</strong></a><strong>:</strong> It’s the acronym for <strong>C</strong>reate, <strong>R</strong>ead, <strong>U</strong>pdate and <strong>D</strong>elete. It’s a group of operations that we have to execute on the server (POST, GET, PUT e DELETE, respectively).</li><li><strong>Create (POST)</strong> —Inserts a value on the database;</li><li><strong>Read (GET)</strong> — Selects data to show;</li><li><strong>Update (PUT)</strong> — Updates a value on the database;</li><li><strong>Delete (DELETE)</strong> — Deletes a value from de database.</li></ul><p>Now that you learned what everything really is, let’s start the tutorial!</p><h3>Pre-Requirements:</h3><ul><li>Ubuntu 18.04 installed on a Virtual Machine.</li><li>Node.js installed on the machine.</li></ul><p>Maybe you don’t have node.js installed, so just use this command to install it:</p><pre>$ sudo apt get uptade<br>$ sudo apt install nodejs<br>$ sudo apt install npm</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*8iS_aHYWtPjW05o7ZBREpA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/579/1*tBFjxUn9pKvd08rEf_3ADg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*q6sSI7nrf4gJd_kW7ccL1w.png" /></figure><h3>Step 1: Creating our project</h3><p>a) First we need to create a directory for our project, using these commands:</p><pre>user@ubuntu:~$ <strong>cd /home/user/Desktop<br></strong>user@ubuntu:~/Desktop$ <strong>mkdir cd_app<br></strong>user@ubuntu:~/Desktop/cd_app$ <strong>cd cd_app</strong></pre><p>b) Configuring the <strong>package.json </strong>file:</p><pre>user@ubuntu:~/Desktop/cd_app$ <strong>npm init</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*E2pYt3HWhac4oeuFtjnjew.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/579/1*Z-cr4O7bskn57vBr1emzmA.png" /></figure><p>Just fill what is needed, in my case I filled the Description and the Artist brackets.</p><p>c) Create the <strong>index.js </strong>file</p><pre>user@ubuntu:~/Desktop/cd_app$ <strong>touch index.js</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*HP7vLyrzp6PE4yYEI_upKQ.png" /></figure><p><strong>index.js</strong> is the <em>main</em> file of our app, as seen on <strong>package.json</strong>.</p><h3>Step 2: App Building</h3><p>a) Install <strong><em>Express. </em></strong>Just use this command:</p><pre>user@ubuntu:~/Desktop/cd_app$ <strong>npm install express --save</strong></pre><p>b) Let’s edit the <strong>index.js</strong> file</p><pre>user@ubuntu:~/Desktop/cd_app$ <strong>gedit index.js</strong></pre><p>And add the following code:</p><pre><strong>const</strong> express = require(&#39;express&#39;);<br><strong>const</strong> app = express();</pre><pre><strong>app.listen</strong>(8090, function(){<br>    console.log(&quot;Servidor em execução na porta 8090&quot;);<br>});</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/719/1*qsyMB2EKTu2XqK8BTIdKLA.png" /></figure><p>Let’s see if everything is working correctly, use this command:</p><pre>user@ubuntu:~/Desktop/cd_app$ <strong>node index.js</strong></pre><p>This is the command we will use everytime we want to test our app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/589/1*QY1hURPd5lx4FAWoAFEGzQ.png" /></figure><p>This tells us the server is running correctly. Now let’s go to our <em>browser</em> and enter <a href="http://localhost:3001/"><strong>http://localhost:</strong></a><strong>8090</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/577/1*5GZfW9S8Y13rnksiG5kUsg.png" /></figure><p>This means the browser is connecting with the Express server.</p><h3>Step 4: Read</h3><p>The <strong><em>Read</em></strong> operations is executed by the <em>browsers </em>exerytume there’s a ‘GET’ request.</p><p>The message ‘<em>Cannot GET /</em>’, means that there’s no information yet to be sent to the browser.</p><p>Add the following code to <strong>index.js:</strong></p><pre>app.get(&#39;/&#39;, (req, res) =&gt; {<br>    res.send(&#39;App cd&#39;s&#39;);<br>});</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/592/1*mny6bin8jHQM3VtNg3uzWw.png" /></figure><p>Save and we will see if this worked.</p><p>REMEMBER! Since we made a change, what do we do? That’s right, we need to use the <strong><em>nodejs index.js</em></strong> command again!</p><p>After doing this we can go to <a href="http://localhost:3001/"><strong>http://localhost:</strong></a><strong>8090 </strong>again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/587/1*FllcLEwfU8QGbc0aslnbqA.png" /></figure><p>There we go, it works! The GET method is working as intended!</p><h3>Step 5: .ejs Files (Embedded Javascript)</h3><p>Let’s create the .ejs files for the visual part.</p><p>To install <strong>ejs</strong>, use this command:</p><pre>user@ubuntu:~/Desktop/cd_app$ <strong>npm install ejs --save</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/586/1*UgVRKcuw96ZP7KDUxbhaBQ.png" /></figure><p>Edit the<strong> index.js </strong>file<strong> </strong>again and add the following code line:</p><pre>app.set(&#39;view engine&#39;, &#39;ejs&#39;);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*qQa3JaqmECG2ImNvlPFFEg.png" /></figure><p>We can now create HTML code that will later appear on the browser. Create the <strong><em>template.ejs</em></strong> inside a folder named ‘<strong><em>views</em></strong>’:</p><pre>user@ubuntu:~/Desktop/cd_app$ <strong>mkdir views<br></strong>user@ubuntu:~/Desktop/cd_app$ <strong>cd views<br></strong>user@ubuntu:~/Desktop/cd_app/views$ <strong>touch template.ejs<br></strong>user@ubuntu:~/Desktop/cd_app/views$ <strong>gedit template.ejs</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*Cg1rtlqNhS86wN_uNraMKw.png" /></figure><p>In<strong><em> template.ejs </em></strong>write the following lines:</p><pre>&lt;!DOCTYPE &lt;html&gt;<br>&lt;html lang=&quot;en&quot;&gt;<br>&lt;head&gt;<br>  &lt;meta charset=&quot;UTF-8&quot;&gt;<br>  &lt;title&gt;CD Collection&lt;/title&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>  &lt;h2&gt;Insert New&lt;h2&gt;<br>  &lt;form action=&quot;/show&quot; method=&quot;POST&quot;&gt;<br>    &lt;input type=&quot;text&quot; placeholder=&quot;Title&quot; name=&quot;title&quot;&gt;<br>    &lt;input type=&quot;text&quot; placeholder=&quot;Artist&quot; name=&quot;artist&quot;&gt;<br>    &lt;input type=&quot;text&quot; placeholder=&quot;Genre&quot; name=&quot;genre&quot;&gt;<br>    &lt;button type=&quot;submit&quot;&gt;Submit&lt;/button&gt;<br>  &lt;/form&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*c4jrHHtqCLZbOAg5G15-uQ.png" /></figure><p>Now we need to render the file we’ve just created so we can see it on the browser. Let’s change the following code on the <strong>index.js</strong> file:</p><pre>app.get(&#39;/&#39;, (req, res) =&gt; {<br>    res.render(&#39;template.ejs&#39;);<br>});</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/589/1*NWj7eD6R0aHBZILr9W3BZw.png" /></figure><p>Let’s see if it worked, initiate the server and visit <a href="http://localhost:3001/"><strong>http://localhost:</strong></a><strong>8090.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/592/1*OAqoR8qDahbYy7OqhqHRIQ.png" /></figure><h3>Step 6: Create</h3><p>The <strong><em>Create</em></strong> operation is executed by the <em>browser </em>if a <strong>POST</strong> request is sent to the server.</p><p>Add this to <strong>index.js:</strong></p><pre>app.post(&#39;/show&#39;, (req, res) =&gt; {<br>    console.log(&#39;Test&#39;);<br>});</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/585/1*tuD1oI3KrqtH6wbu_trwhA.png" /></figure><p>Reboot the server again, and if this step worked, you will see the word Test on your Terminal:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*DsT2IJ-Gw9B-jKbI5CnCTA.png" /></figure><p>Amazing, it works!</p><p>Now we nee to install another package called body-parser. Use this command:</p><pre>user@ubuntu:~/Desktop/cd_app$ <strong>npm install body-parser --save</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/583/1*jBT0oAQ9rpPOxpgHWkMpzw.png" /></figure><p>Another change on the <strong>index.js</strong> file. Add this:</p><pre><strong>const </strong>bodyParser = require(&#39;body-parser&#39;);<br>app.use(bodyParser.urlencoded({ extended: true }));</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*6k699VbOAWxKxF0dZTIExQ.png" /></figure><p>The urlencoded method inside of the <em>body-parser</em> tells <em>body-parser</em> to extract data from&lt;form&gt; and add them to body on the requestobject.</p><p>Add this line inside the <strong>post </strong>method:</p><pre>console.log(req.body);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*2bf9XoFDnIO_rrMTMMm3NQ.png" /></figure><p>Run the server and go to <a href="http://localhost:3001/"><strong>http://localhost:</strong></a><strong>8090.</strong></p><p>Fill in the form and press ‘<em>Submit</em>’.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/592/1*IMF01wjOG8pBkK5gqDiG0A.png" /></figure><p>When you press the Submit button, the information you entered should appear on the Terminal, like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/583/1*VOiC_q_EHW1vYoMuutrZSA.png" /></figure><h3>Step 7:Connecting to MongoDB</h3><p>To install MongoDB use this command:</p><pre>user@ubuntu:~/Desktop/cd_app$ <strong>npm install mongodb --save</strong></pre><p>Once it’s done, let’s create a MongoDB account, you can do this following the link: <a href="https://www.mongodb.com/cloud/atlas">https://www.mongodb.com/cloud/atlas</a></p><p>After creating the account you can chose the option to create a Cluster and then do like me, since this way it’s free:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*nzui39wLtHsDiSrsGzpLGw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/585/1*S4TstTQb5IUywwhFcxoGwA.png" /></figure><p>Now go to<strong> COLLECTIONS</strong> &gt; <strong>Create New Database</strong> and fill in the gaps:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/578/1*-oYTONrDxFoCRxhbt4kTwQ.png" /></figure><p>I did it like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/269/1*xBNyY1ZFi7BvSPxuWepf9g.png" /></figure><p>Then, go to the Security tab and Add a New User:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/587/1*Ong0y_IsRnPffon646hlOA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*aOnaLHNzPJNTGwrIuF4QxQ.png" /></figure><p>User is now created.</p><p>The MongoDB connection string is available at <strong>Connect</strong> &gt; <strong>Connect your application.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*Ph_TchRcXZ7bX7B_ntKmng.png" /></figure><p>Click ‘<strong>Full Driver Example</strong>’, so you can connect your MongoDB database to the Node.js <em>driver</em>. <strong><em>Copy </em></strong>the example code.</p><p>Now we add that code to <strong>index.js, </strong>making the needed changes, (substitute &lt;password&gt; for the password you chose earlier) aswell as the DB and Collection name. <em>(</em>In my case: Database<em>: mongodb </em>Collection<em>: cds).</em></p><p>Do it like this in index.js:</p><pre><strong>const </strong>MongoClient = require(&#39;mongodb&#39;).MongoClient;<br><strong>const </strong>uri = &quot;mongodb+srv://torres:jmft@crud-nodejs-aamhl.mongodb.net/test?retryWrites=true&quot;;<br><strong>const </strong>client = new MongoClient(uri, { useNewUrlParser: true });client.connect(err =&gt; {<br>    db = client.db(&quot;mongodb&quot;).collection(&quot;cds&quot;);<strong>    </strong>app.listen(3001, function(){<br>        console.log(&quot;O servidor está a correr na porta 8090&quot;);<br>    });<br>});</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*P7EHxHHNXUh2E3zt3yLMSQ.png" /></figure><p>You can test by starting the app via <strong><em>node index.js</em></strong>. If the output on the console is the same as in the<em> </em><strong>console.log </strong>on the <strong>listen </strong>method, then it’s working.</p><h3>Step 8: CRUD</h3><p><strong>a) Save data in the database— CREATE</strong></p><p>Since we’ve already created the ‘cds’ collection when we created the databse, now we just need to call the <strong><em>insertOne()</em></strong> method from MongoDB, which a allows us to insert something in the Database.</p><p>Change the <strong>app.post</strong> method on <strong>index.js:</strong></p><pre>app.post(&#39;/show&#39;, (req, res) =&gt; {<br>    db.insertOne(req.body, (err, result) =&gt; {<br>        if (err) return console.log(&quot;Erro: &quot; + err);<br> <br>        console.log(&quot;Registo guardado com sucesso na BD!&quot;);<br>        res.redirect(&#39;/&#39;);<br>    });<br>});</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*pxzf3Bxo_H6zp2WfKf77ug.png" /></figure><p>To see if this is working, start the app again, fill in the form and press “Submit”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*wrzEChU7znVLM76qrakJYw.png" /></figure><p>A success message should appear on the Terminal.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*vM2B6ZZVchmRuOvQI-Fzvw.png" /></figure><p>You can see the data entry on the <em>dashboard</em> in Atlas MongoDB, in the ‘<strong>Collections</strong>’ tab:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/575/1*2bZWlWsFtNrKqPRXX18qPQ.png" /></figure><p><strong>b)</strong> <strong>Ler</strong> <strong>dados da base de dados — READ</strong></p><p>To obtain the database data, we use the <strong><em>find()</em></strong> method. Add this to <strong>index.js:</strong></p><pre>app.get(&#39;/&#39;, (req, res) =&gt; {<br>    <strong>const</strong> cursor = db.find();<br>});</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/586/1*yimMmqApV4RdfQWZv0GuAQ.png" /></figure><p><strong><em>find()</em></strong> returns a vursor(a Mongo object), and this objecs our database data. It has other methods and one of them is the <strong><em>toArray </em></strong>method.</p><p><strong><em>toArray</em></strong> receives a callback function that let uas do some actions.</p><p>Add these lines to <strong>index.js </strong>inside the <strong>post </strong>method.</p><pre>db.find().toArray((err, results) =&gt; {<br>console.log(results);<br>});</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*i6EnmYgaorfDvxNemu4sSQ.png" /></figure><p>Run the server, fill in the form, Submit and check the Terminal, hopefully ir shows something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*jxDBvd_2cyIzhYFBbxNixQ.png" /></figure><p>There’s a Coldplay registry I just added and there’s the one we added earlier.</p><p><strong>c) Rendering the content on a <em>template engine</em></strong></p><p>Create a file inside ‘<strong><em>views</em></strong>’ with the name “<strong>show.ejs</strong>”:</p><pre>user@ubuntu:~/Desktop/cd_app$ <strong>cd views<br></strong>user@ubuntu:~/Desktop/cd_app/views$ <strong>touch show.ejs</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*m2KYGd2q1Ip1-v7A0-Rbog.png" /></figure><p>Open <strong>show.ejs:</strong></p><pre>user@ubuntu:~/Desktop/cd_app/views$ <strong>gedit show.ejs</strong></pre><p>and add this code, that’s basically a table for the information to be shown:</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html lang=&quot;en&quot;&gt;<br>&lt;head&gt;<br>  &lt;meta charset=&quot;UTF-8&quot;&gt;<br>  &lt;title&gt;CD Collection&lt;/title&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>  &lt;h2&gt;Records&lt;h2&gt;<br>  &lt;table border=&quot;1&quot;&gt;<br>    &lt;thead&gt;<br>      &lt;tr&gt;<br>        &lt;td&gt;Title&lt;/td&gt;<br>        &lt;td&gt;Artist&lt;/td&gt;<br>        &lt;td&gt;Genre&lt;/td&gt;<br>      &lt;/tr&gt;<br>    &lt;/thead&gt;<br>    &lt;tbody&gt;<br>    &lt;% cds.forEach(function(details) { %&gt;<br>      &lt;tr&gt;<br>        &lt;td&gt;&lt;%= details.title %&gt;&lt;/td&gt;<br>        &lt;td&gt;&lt;%= details.artist %&gt;&lt;/td&gt;<br>        &lt;td&gt;&lt;%= details.genre %&gt;&lt;/td&gt;<br>      &lt;/tr&gt;<br>    &lt;% }) %&gt;<br>    &lt;/tbody&gt;<br>    &lt;button&gt;&lt;a href=&quot;/&quot;&gt;Back&lt;/a&gt;&lt;/button&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/587/1*qFRpJSiJfzAqdLvGbV3k-g.png" /></figure><p>Let’s add some methods to <strong>index.js:</strong></p><pre>app.get(&#39;/show&#39;, (req, res) =&gt; {<br>    db.find().toArray((err, results) =&gt; {<br>        if (err) return console.log(&quot;Error: &quot;+ err);<br>        res.render(&#39;show.ejs&#39;, { cds: results });<br>    });<br>});</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/587/1*kNaURJt4NLtCn-pU0q7NXQ.png" /></figure><p>Run the server and fill in the form again. When we ‘<em>Submit</em>’ we will be redirected to <a href="http://localhost:3001/show">http://localhost:8090/show</a>, and we’ll see all we have in our database:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/585/1*Hc8ij_V6VLco88J9XpB2Vg.png" /></figure><p><strong>d) Update the data— UPDATE</strong></p><p><strong>Update </strong>is used when we want to change database content.</p><p>Let’s add buttons to ‘<strong>show.ejs</strong>’ so we can ‘<strong>Edit</strong>’ and ‘<strong>Delete</strong>’:</p><pre>user@ubuntu:~/Desktop/cd_app$ <strong>cd views</strong><br>user@ubuntu:~/Desktop/cd_app/views$ <strong>gedit show.ejs</strong></pre><p>Add the following code: (Only the code un <strong>BOLD</strong>)</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html lang=&quot;en&quot;&gt;<br>&lt;head&gt;<br>  &lt;meta charset=&quot;UTF-8&quot;&gt;<br>  &lt;title&gt;CD Collection&lt;/title&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>  &lt;h2&gt;Records&lt;h2&gt;<br>  &lt;table border=&quot;1&quot;&gt;<br>    &lt;thead&gt;<br>      &lt;tr&gt;<br>        &lt;td&gt;Title&lt;/td&gt;<br>        &lt;td&gt;Artist&lt;/td&gt;<br>        &lt;td&gt;Genre&lt;/td&gt;<br>      &lt;/tr&gt;<br>    &lt;/thead&gt;<br>    &lt;tbody&gt;<br>    &lt;% cds.forEach(function(details) { %&gt;<br>       &lt;tr&gt;<br>          &lt;td&gt;&lt;%= details.title %&gt;&lt;/td&gt;<br>          &lt;td&gt;&lt;%= details.artist %&gt;&lt;/td&gt;<br>          &lt;td&gt;&lt;%= details.genre %&gt;&lt;/td&gt;<br>          <strong>&lt;td&gt;&lt;a href=&quot;/edit/&lt;%= details._id %&gt;&quot;&gt;Edit&lt;/a&gt; | &lt;a href=&quot;/delete/&lt;%= details._id %&gt;&quot;&gt;Delete&lt;/a&gt;&lt;/td&gt;</strong><br>      &lt;/tr&gt;<br>    &lt;% }) %&gt;<br>    &lt;/tbody&gt;<br>    &lt;button&gt;&lt;a href=&quot;/&quot;&gt;Insert New&lt;/a&gt;&lt;/button&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><p>Now let’s create routes to the CRUD operations. This simplifies the code.</p><p>Edit ‘<strong>index.js</strong>’ with the following code:</p><pre>var ObjectId = require(&#39;mongodb&#39;).ObjectID;<strong>//EDIT</strong><br>app.route(&#39;/edit/:id&#39;)<br>.get((req,res) =&gt; {<br>    var id = req.params.id;db.find(ObjectId(id)).toArray((err, result) =&gt; {<br>        if (err) return console.log(&quot;Error: &quot; + err);<br>     res.render(&#39;edit.ejs&#39;, { cds: result });<br>    });<br>})<br>.post((req,res) =&gt; {<br>    var id = req.params.id;<br>    var title = req.body.title;<br>    var artist= req.body.artist;<br>    var genre = req.body.genre;db.updateOne({_id: ObjectId(id)}, {<br>        $set: {<br>     title: title,<br>     artist: artist,<br>     genre: genre    <br> }<br>    }, (err, result) =&gt; {<br>     if (err) return res.send(err);<br>        res.redirect(&#39;/show&#39;);<br> console.log(&quot;Successfully Updated!&quot;);<br>    })<br>});</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/589/1*dKfkb-eV6lBrEKwgRaHlFA.png" /></figure><p>Now create the <em>template engine</em> inside the ‘<strong><em>views</em></strong>’ directory with the name “<strong>edit.ejs”.</strong></p><p>This is the code you should insert. (You already learned how to enter the views directory and how to create a new file. Come on, I know you can do it!)</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html lang=&quot;en&quot;&gt;<br>&lt;head&gt;<br>  &lt;meta charset=&quot;UTF-8&quot;&gt;<br>  &lt;title&gt;CD Collection&lt;/title&gt;<br>&lt;/head&gt;<br>&lt;body&gt;&lt;h2&gt;Edit&lt;/h2&gt;<br>  &lt;% cds.forEach(function(details) { %&gt;<br>    &lt;form action=&quot;/edit/&lt;%= details._id %&gt;&quot; method=&quot;POST&quot;&gt;<br>      &lt;input type=&quot;text&quot; value=&quot;&lt;%= details.title %&gt;&quot; name=&quot;title&quot;&gt;<br>      &lt;input type=&quot;text&quot; value=&quot;&lt;%= details.artist %&gt;&quot; name=&quot;artist&quot;&gt;<br>      &lt;input type=&quot;text&quot; value=&quot;&lt;%= details.genre %&gt;&quot; name=&quot;genre&quot;&gt;<br>      &lt;br /&gt;<br>      &lt;button&gt;&lt;a href=&quot;/show&quot;&gt;List All&lt;/a&gt;&lt;/button&gt;<br>      &lt;button type=&quot;submit&quot;&gt;Edit&lt;/button&gt;<br>    &lt;/form&gt;<br>  &lt;% }) %&gt;&lt;/body&gt;<br>&lt;/html&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/594/1*rOFzGp7gnTjW0BtudlGsQw.png" /></figure><p>Let’s test our ‘<strong>Edit</strong>’ option:</p><p>Access <a href="http://localhost:3001/">http://localhost:</a>8090 and try to edit the first CD.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/289/1*dRhx2O84GRiEn7dnta8s8A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/357/1*8upID7Y_1IvlQ2t4UOeDtw.png" /></figure><p>As you can see, mine worked, hopefully yours worked too! If it did, you must have gotten the Success Message too!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*0SI0u24x4u7bb-KUC5TJAA.png" /></figure><p><strong>e) Delete Entry— DELETE</strong></p><p>The <strong>Delete</strong> operation is simple. We only have to call the <strong><em>deleteOne()</em></strong> method and pass the id of the object we want to delete.</p><p>Add the route(<strong>/delete/:id</strong>) to <strong>index.js:</strong></p><pre>//DELETE<br>app.route(&#39;/delete/:id&#39;)<br>.get((req,res) =&gt; {<br>    var id = req.params.id;db.deleteOne({_id: ObjectId(id)}, (err, result) =&gt; {<br>     if (err) return res.send(500, err);<br> console.log(&quot;Registo eliminado com sucesso!&quot;);<br> res.redirect(&#39;/show&#39;);<br>    });<br>});</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*3cyu1cBWBFVimW9QSNaKIg.png" /></figure><p>Let’s test the <strong>DELETE </strong>feature. Enter a new CD in the form and submit. Then, try to Delete it. I’ll just call it “Delete Test”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*JdnVMPO1605aDp0_8wCrqg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/408/1*D_2DnDohhP-y9x1oej1v4g.png" /></figure><p>I press delete and… it’s gone!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/1*9A59hbQOtCtV6BoI0VmRWw.png" /></figure><p>And you should get the successfully removed message:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*A_F17ePUeTlbc73bndxISg.png" /></figure><p>And this is all for our tutorial!</p><h3>Final Considerations</h3><p>If you reached this point, hopefully you were able to create your CRUD app with my help. If not, feel free to ask some questions if you have any doubts.</p><p>As you can see you can do this relatively quickly and with this base knowledge I tried to pass on to you, you can make bigger and better CRUD apps, just need some ideas.</p><p>Goodbye, until next time!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=416ae5820d12" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Install and Set Up an Apache Spark Cluster on Hadoop 18.04]]></title>
            <link>https://medium.com/@jootorres_11979/how-to-install-and-set-up-an-apache-spark-cluster-on-hadoop-18-04-b4d70650ed42?source=rss-6a9a21d69804------2</link>
            <guid isPermaLink="false">https://medium.com/p/b4d70650ed42</guid>
            <category><![CDATA[students]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[apache-spark]]></category>
            <category><![CDATA[apache]]></category>
            <dc:creator><![CDATA[João Torres]]></dc:creator>
            <pubDate>Mon, 03 Feb 2020 21:58:46 GMT</pubDate>
            <atom:updated>2020-02-03T21:58:46.401Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/810/1*czc9lq8kZnCsyWa2VX-z2Q.jpeg" /></figure><p>In this arcticle I will explain how to install <strong>Apache Spark</strong> on a multi-node cluster, providing step by step instructions.</p><h3>Spark Architecture</h3><p>Apache Spark follows a master/slave architecture with two main daemons and a cluster manager.</p><ul><li>Master Daemon — (Master/Driver Process)</li><li>Worker Daemon –(Slave Process)</li><li>Cluster Manager</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/481/0*nhLHky0_u2k0zQuH.png" /></figure><p>A spark cluster has a single Master and any number of Slaves/Workers. The driver and the executors run their individual Java processes and users can run them on the same horizontal spark cluster or on separate machines.</p><h3>Pre-requirements</h3><ul><li>Ubuntu 18.04 installed on a virtual machine.</li></ul><h3>1st Step:</h3><p>Create 2 clones of the Virtual Machine you’ve previously created.</p><p>Make sure that you have the option “Generate new MAC addresses for all network adapters selected. Also, choose the option “Full Clone”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/507/1*TMN_DyUNoEH_zMXCD3pQRQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/495/1*4eOK1M69JqRh8dNbR2l_MA.png" /></figure><h3>2nd Step:</h3><p>Make sure all the VM’s have the following network configuration on Apapter 2:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/524/1*TJHhSL1AcCPYuM-qoMoQzA.png" /></figure><h3>3rd Step:</h3><p>Let’s change the hostname on each virtual machine. Open the file and type the name of the machina. Use this command:</p><pre>sudo nano /hostname</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*x0d0LOtqkjvc6fMrxWMUPA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/586/1*B1qJWW6kbmMDWR7VrkJ3yQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*4D3SAq018ZM63u1xfb1gOA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/585/1*7Me2IrZAoTGytKjBGyu0gg.png" /></figure><h3>4th Step:</h3><p>Now let’s figure out what our ip address is. To do that just type the command:</p><pre>ip addr</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*QZu3fosPiNzav2UxagSskw.png" /></figure><p>This is on the master VM, as you can see our IP is 192.168.205.10. For you this will be different.</p><p>This means that our IP’s are:</p><p>master: 192.168.205.10</p><p>slave1: 192.168.205.11</p><p>slave2: 192.168.205.12</p><h3>5th Step:</h3><p>We need to edit the <strong>hosts</strong> file. Use the following command:</p><pre>sudo nano /etc/hosts</pre><p>and add your network information:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*TjWQpPwfVBgwA5j3WEg9mQ.png" /></figure><h3>6th Step:</h3><p>In order for the machines to assimilate the previous steps we need to reboot them. Use the following command in all of them:</p><pre>sudo reboot</pre><h3>7th Step:</h3><p>Do this step on all the Machines, master and slaves.</p><p>Now, in order to install Java we need to do some things. Follow these commands and give permission when needed:</p><pre>$ sudo apt-get install software_properties_common<br>$ sudo add-apt-repository ppa:webupd8team/java<br>$ sudo apt-get update<br>$ sudo apt-get install openjdk-11-jdk</pre><p>To check if java is installed, run the following command.</p><pre>$ java -version</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*5sHGuk9_DfYtAph4EThk8w.png" /></figure><h3>8th Step:</h3><p>Now let’s install Scala on the master and the slaves. Use this command:</p><pre>$ sudo apt-get install scala</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/579/1*wxcRwRaqILTf0TsqgA76ag.png" /></figure><p>To check if Scala was correctly installed run this command:</p><pre>$ scala -version</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*SnwG2_wkowPsFgFfH-K9tQ.png" /></figure><p>As you can see, Scala version 2.11.12 is now installed on my machine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/635/1*Lpcl3_EuQ0uYb8rJ3kx1ZQ.png" /></figure><h3>9th Step:</h3><p>We will configure SSH, but this step in on master only.</p><p>We need to install the<strong> Open SSH Server-Client</strong>, use the command:</p><pre>$ sudo apt-get install openssh-server openssh-client</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/585/1*TefvHsX2F_vRRFe3UsoxWw.png" /></figure><p>Now generate key pairs:</p><pre>$ ssh-keygen -t rsa -P &quot;&quot;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/585/1*htA6Tx0mz0ZZEf4k0oKcMg.png" /></figure><p>Use the following command in order to make this key an authorized one:</p><pre>cat ~/.ssh/id_rsa.pub &gt;&gt; ~/.ssh/authorized_keys</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*EnCN-Mr2YEuvQ3c9OIMTRA.png" /></figure><p>Now we need to copy the content of <em>.ssh/id_rsa.pub</em> (of master) to <em>.ssh/authorized_keys</em> (of all the slaves as well as master). Use these commands:</p><pre>ssh-copy-id user@pd-master<br>ssh-copy-id user@pd-slave1<br>ssh-copy-id user@pd-slave2</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/578/1*Z3c1JKIb97nmOB1b6viP5g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*jmHSwlUkKiJW1VG6FawzsQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/587/1*rgSnmAzsLyNWxrqkWCyGKQ.png" /></figure><p>Let’s check if everything went well, try to connect to the slaves:</p><pre>$ ssh slave01<br>$ ssh slave02</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*VIi3AMWypalEhUslKdykdQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/578/1*fML_Ca8Tsg1xuvuLeW6JCw.png" /></figure><p>As you can see everything went well, to exit just type the command:</p><pre>exit</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/579/1*rPAbdPaICqhX8npaA8I71w.png" /></figure><h3>10th Step:</h3><p>Now we Download the latest version of Apache Spark.</p><p><strong>NOTE: Everything inside this step must be done on all the virtual machines.</strong></p><p>Use the following command :</p><pre>$ wget <a href="http://www-us.apache.org/dist/spark/spark-2.3.0/spark-2.3.0-bin-hadoop2.7.tgz">http://www-us.apache.org/dist/spark/spark-2.4.4/spark-2.4.4-bin-hadoop2.7.tgz</a></pre><p>This is the most recent version as of the writing of this arcticle, it might have changed if you try it later. Anyway, I think you’ll still be good using this one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*KQXJSrMD4vw2bBkBtW_wnA.png" /></figure><p><strong>Extract the Apache Spark file you just downloaded</strong></p><p>Use the following command to extract the Spark tar file:</p><pre>$ tar xvf spark-2.4.4-bin-hadoop2.7.tgz</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*OXzSoIhdbMT851hN-zvREg.png" /></figure><p><strong>Move Apache Spark software files</strong></p><p>Use the following command to move the spark software files to respective directory (<em>/usr/local/bin</em>)</p><pre>$ sudo mv spark-2.4.4-bin-hadoop2.7 /usr/local/spark</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*BTzOVcOUqhzV7hs15V2V6Q.png" /></figure><p><strong>Set up the environment for Apache Spark</strong></p><p>Edit the <em>bashrc</em> file using this command:</p><pre>$ sudo gedit~/.bashrc</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*bLlgk1tcSOWis_RBi8FVRg.png" /></figure><p>Add the following line to the file. This adds the location where the spark software file are located to the PATH variable.</p><pre>export PATH = $PATH:/usr/local/spark/bin</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*YVRyutJeR5dW8dOlNHmk6A.png" /><figcaption><strong>Note:</strong> this screenshot has a mistake, when you’re doing this don’t leave a space like I did. Just write “PATH=$PATH”.</figcaption></figure><p>Now we need to use the following command for sourcing the <em>~/.bashrc</em> file:</p><pre>$ source ~/.bashrc</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*7ULtcZ_EUW2uQsxkfTOpMQ.png" /></figure><h3>11th Step:</h3><p>Apache Spark Master Configuration (do this step on the Master VM only)</p><p><strong>Edit spark-env.sh</strong></p><p>Move to spark <em>conf</em> folder and create a copy of the template of <em>spark-env.sh</em> and rename it.</p><pre>$ cd /usr/local/spark/conf<br>$ cp spark-env.sh.template spark-env.sh</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/577/1*ua1wObt8YJm5zSTMQopXKQ.png" /></figure><p>Now edit the configuration file <em>spark-env.sh.</em></p><pre>$ sudo vim spark-env.sh</pre><p>And add the following parameters:</p><pre>export SPARK_MASTER_HOST=&#39;&lt;MASTER-IP&gt;&#39;export JAVA_HOME=&lt;Path_of_JAVA_installation&gt;</pre><p><strong>Add Workers</strong></p><p>Edit the configuration file <em>slaves</em> in (<em>/usr/local/spark/con</em>f).</p><pre>$ sudo nano slaves</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*z_OloPYDwzJoyhOMBwUMcw.png" /></figure><p>And add the following entries.</p><pre>pd-master<br>pd-slave01<br>pd-slave02</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/587/1*HCco_dNy7blSj3-kkmhajQ.png" /></figure><h3>12th Step:</h3><p><strong>Let’s try to start our Apache Spark Cluster, hopefully everything is ok!</strong></p><p>To start the spark cluster, run the following command on master.:</p><pre>$ cd /usr/local/spark<br>$ ./sbin/start-all.sh</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*EKpt0rvNRiBJgEyI_7ce2w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*sm7HUmF56aLCUSVxbenzaA.png" /></figure><p>I won’t stop it, but in case you want to stop the cluster, this is the command:</p><pre>$ ./sbin/stop-all.sh</pre><h3>13th Step:</h3><p>To check if the services started we use the command:</p><pre>$ jps</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/588/1*jqcSLXVUk3CYemLB1-n-Dw.png" /></figure><h3>14th Step:</h3><p>Browse the Spark UI to know about your cluster. To do this, go to your browser and type:</p><pre>http://&lt;MASTER-IP&gt;:8080/</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*AoiGcabfwjBd3TPl44tSJg.png" /></figure><p>As you can see we have 2 Alive Workers, our slaves, which means it’s all done!</p><h3>Final considerations:</h3><p>Hopefully you managed to successfully follow this tutorial and have a perfectly working Apache Spark Cluster.</p><p>Any doubts feel free to ask me.</p><p>See you later!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b4d70650ed42" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How To Set Up a Hadoop 3.2.1 Multi-Node Cluster on Ubuntu 18.04 (2 Nodes)]]></title>
            <link>https://medium.com/@jootorres_11979/how-to-set-up-a-hadoop-3-2-1-multi-node-cluster-on-ubuntu-18-04-2-nodes-567ca44a3b12?source=rss-6a9a21d69804------2</link>
            <guid isPermaLink="false">https://medium.com/p/567ca44a3b12</guid>
            <category><![CDATA[hadoop]]></category>
            <category><![CDATA[apache-hadoop]]></category>
            <category><![CDATA[cluster]]></category>
            <category><![CDATA[ubuntu]]></category>
            <category><![CDATA[students]]></category>
            <dc:creator><![CDATA[João Torres]]></dc:creator>
            <pubDate>Mon, 03 Feb 2020 00:03:12 GMT</pubDate>
            <atom:updated>2020-02-03T00:03:12.804Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*Opium3FGv0Iiu9cm5A_OQg.jpeg" /></figure><h3>To start: What is Hadoop?</h3><p><strong>Apache Hadoop </strong>is a collection of open-source software utilities that allows the distribution of larges amounts of data sets across clusters of computers using simple programing models.</p><p>It provides a software framework for distributed storage and the processing of big data using the MapReduce. It was originally design for computer clusters (“is a set of tightly connected computers that work together, so they can be viewed as a single system”) commodity hardware, it has also found use on cluster of higher-end hardware.</p><p>The base Apache Hadoop framework is composed of the following modules:</p><ul><li><strong><em>Hadoop Common</em></strong> <strong>— </strong>contains libraries and utilities needed by other Hadoop modules;</li><li><strong><em>Hadoop Distributed File System (HDFS)</em> — </strong>a distributed file-system that stores data on commodity machines, providing very high aggregate bandwidth across the cluster;</li><li><strong><em>Hadoop YARN</em> — </strong>(introduced in 2012) a platform responsible for managing computing resources in clusters and using them for scheduling users’ applications;</li><li><strong><em>Hadoop MapReduce</em> — </strong>an implementation of the MapReduce programming model for large-scale data processing.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xTPu2yLwWN181dswx5Cpgg.png" /></figure><h3>Pre-requirements</h3><ul><li>Ubuntu 18.04 installed on a virtual machine.</li></ul><h3>What are we going to install in order to create the Hadoop Multi-Node Cluster?</h3><ul><li>Java 8;</li><li>SSH;</li><li>PDSH;</li></ul><h3>1st Step: Configuring our Network</h3><p>Go to the <strong>Network Settings</strong> of your Virtual Machine and <strong>Enable Adapter 2</strong>. Then, instead of NAT, chose <strong>Virtual Host-Only Adapter</strong> and where it says “Promiscuous Mode” select the option “<strong>Allow All</strong>”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/605/1*AuXhjGqnCeutajwdeWzuWQ.png" /></figure><h3>2nd Step:</h3><p>Install SSH using the following command:</p><pre>sudo apt install ssh</pre><p>It will ask you for the password. When it asks for confirmation, just give it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/583/1*9r4iFz5LiaZbUfLT3Hp14Q.png" /></figure><h3>3rd Step:</h3><p>Install PDSH using the following command:</p><pre>sudo apt install pdsh</pre><p>Just as before, give confirmation when needed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*0JQbP3LNJ8zZWAMbNniplQ.png" /></figure><h3>4th Step:</h3><p>Open the <strong>.bashrc</strong> file with the following command:</p><pre>nano .bashrc</pre><p>At the end of the file just write the following line:</p><pre>export PDSH_RCMD_TYPE=ssh</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*XCCXp1OrGgTVaJ3R054GYg.png" /></figure><h3>5th Step:</h3><p>Now let’s configure SSH. Let’s create a new key using the following command:</p><pre>ssh-keygen -t rsa -P &quot;&quot;</pre><p>Just press <strong>Enter</strong> everytime that is needed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*ZZkWMWBd5wxJSXG8Xkv5dQ.png" /></figure><h3>6th Step:</h3><p>Now we need to copy the public key to the authorized_keys file with the following command:</p><pre>cat ~/.ssh/id_rsa.pub &gt;&gt; ~/.ssh/authorized_keys</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*HGfCMP74R5MCaTmJy4xYeA.png" /></figure><h3>7th Step:</h3><p>Now we can verify the SSH configuration by connecting to the localhost:</p><pre>ssh localhost</pre><p>Just type <strong>“yes”</strong> and press <strong>Enter</strong> when needed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*-Dk0trTS_U00c-Gu6T0K4g.png" /></figure><h3>8th Step:</h3><p>This is the step where we install Java 8. We use this command:</p><pre>sudo apt install openjdk-8-jdk</pre><p>Just as previously, give confirmation when needed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/579/1*WlCb-u53k4o2Sg83MUsbvw.png" /></figure><h3>9th Step:</h3><p>This step isn’t really a step, it’s just to check if Java is now correctly installed:</p><pre>java -version</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*0wg9X3EYRToFPCYOeGzr9w.png" /></figure><h3>10th Step:</h3><p>Download Hadoop using the following command:</p><pre>sudo wget -P ~ <a href="https://mirrors.sonic.net/apache/hadoop/common/hadoop-3.2.1/hadoop-3.2.1.tar.gz">https://mirrors.sonic.net/apache/hadoop/common/hadoop-3.2.1/hadoop-3.2.1.tar.gz</a></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*3Cc4VSY5lsHgzADvtQ7nUw.png" /></figure><h3>11th Step:</h3><p>We need to unzip the <strong>hadoop-3.2.1.tar.gz </strong>file with the following command:</p><pre>tar xzf hadoop-3.2.1.tar.gz</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*hZS6ULhNwqLftJkVST7dXA.png" /></figure><h3>12th Step:</h3><p>Change the <strong>hadoop-3.2.1</strong> folder name to <strong>hadoop</strong> (this maked it easier to use). Use this command:</p><pre>mv hadoop-3.2.1 hadoop</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/589/1*H0gygPGgnmM1AuU8rVI_oA.png" /></figure><h3>13th Step:</h3><p>Open the <strong>hadoop-env.sh</strong> file in the nano editor to edit <strong>JAVA_HOME:</strong></p><pre>nano ~/hadoop/etc/hadoop/hadoop-env.sh</pre><p>Paste this line to JAVA_HOME:</p><pre>export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/</pre><p>(I forgot to take a screenshot for this step, but it’s really easy to find. Once you find it just remove the # commentary tag and do what I said, copy it).</p><h3>14th Step:</h3><p>Change the <strong>hadoop</strong> folder directory to <strong>/usr/local/hadoop. </strong>This is the command:</p><pre>sudo mv hadoop /usr/local/hadoop</pre><p>Provide the password when needed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/586/1*xuoV5mTIY7pcQcoKaqT7rw.png" /></figure><h3>15th Step:</h3><p>Open the <strong>environment</strong> file on nano with this command:</p><pre>sudo nano /etc/environment</pre><p>Then, add the following configurations:</p><pre>PATH=&quot;/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/hadoop/bin:/usr/local/hadoop/sbin&quot;JAVA_HOME=&quot;/usr/lib/jvm/java-8-openjdk-amd64/jre&quot;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/585/1*qfCL3SUzfAoh-4fZN4RtRA.png" /></figure><h3>16th Step:</h3><p>Now we will add a user called <strong>hadoopuser</strong>, and we will set up it’s configurations:</p><pre>sudo adduser hadoopuser</pre><p>Provide the password and you can leave the rest blank, just press <strong>Enter.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*SwICF0ulKGotdeG_pib-pQ.png" /></figure><p>Now type these commands:</p><pre>sudo usermod -aG hadoopuser hadoopuser<br>sudo chown hadoopuser:root -R /usr/local/hadoop/<br>sudo chmod g+rwx -R /usr/local/hadoop/<br>sudo adduser hadoopuser sudo</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/583/1*2EvsrdV9IKWxiTscxW48lw.png" /></figure><h3>17th Step:</h3><p>Now we need to verify the machine ip address:</p><pre>ip addr</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/589/1*Ikt7dPzJa1YUo9baavWnTA.png" /></figure><p>Now, as you can see, my IP is <strong>192.168.205.7</strong>, just remember this will be different for you, you need to act accordingly when the IP addresses are used later.</p><p>My network will be as follows:</p><p>master: <strong>192.168.205.7</strong></p><p>slave1: <strong>192.168.205.8</strong></p><p>slave2:<strong> 192.168.205.9</strong></p><p>In your case, just keep adding 1 to the last number of the IP you get on your machine, just as I did for mine.</p><h3>18th Step:</h3><p>Open the <strong>hosts </strong>file and insert your Network configurations:</p><pre>sudo nano /etc/hosts</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/587/1*N28dZnVijJI487OBgHpTdg.png" /></figure><h3>19th Step:</h3><p>Now is the time to create the Slaves.</p><p>Shut Down your Master Virtual Machine and clone it twice, naming one Slave1 and the Other Slave2.</p><p>Make sure the “Generate new MAC addresses for all network adapters” option is chosen.</p><p>Also, make a Full Clone.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/532/1*cp3AYcpiw4IK1K3qgLNxFA.png" /><figcaption>Clone for Slave1, do the same for Slave2.</figcaption></figure><h3>20th Step:</h3><p>On the master VM, open the <strong>hostname </strong>file on<strong> </strong>nano:</p><pre>sudo nano /etc/hostname</pre><p>Insert the name of your master virtual machine. (note, it’s the same name you entered previously on the hosts file)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/583/1*PfnwRxCBT52P3QXjVOKW3A.png" /></figure><p>Now do the same on the slaves:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/583/1*6qYGYz2AqsfNmc3ZFcCHhA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/585/1*z6vk53m-Yfds1sLXS0yx1w.png" /></figure><p>Also, you should reboot all of them so this configuration taked effect:</p><pre>sudo reboot</pre><h3>21st Step:</h3><p>Configure the SSH on hadoop-master, with the hadoopuser. This is the command:</p><pre>su - hadoopuser</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/585/1*U2qFjVGv5MjZwZDDYlW2xw.png" /></figure><h3>22nd Step</h3><p>Create an SSH key:</p><pre>ssh-keygen -t rsa</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*M-yCHcb64oAXaGAn32mIGA.png" /></figure><h3>23rd Step:</h3><p>Now we need to copy the SSH key to all the users. Use this command:</p><pre>ssh-copy-id hadoopuser@hadoop-master<br>ssh-copy-id hadoopuser@hadoop-slave1<br>ssh-copy-id hadoopuser@hadoop-slave2</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/583/1*BF9tFCnN539_eT9Xpwzfwg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*COiGq3zYGLvjgAY8yfAYUw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/586/1*hP1YT10K-K9QvGN38IPTXg.png" /></figure><h3>24th Step:</h3><p>On hadoop-master, open <strong>core-site.xml </strong>file on nano:</p><pre>sudo nano /usr/local/hadoop/etc/hadoop/core-site.xml</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*gONeFIc0mpifRhb4M-hSMg.png" /></figure><p>Then add the following configurations:</p><pre>&lt;configuration&gt;<br>&lt;property&gt;<br>&lt;name&gt;fs.defaultFS&lt;/name&gt;<br>&lt;value&gt;hdfs://hadoop-master:9000&lt;/value&gt;<br>&lt;/property&gt;<br>&lt;/configuration&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/585/1*qFmxEO-XVZLdDybSmy4oaQ.png" /></figure><h3>25th Step:</h3><p>Still on hadoop-master, open the <strong>hdfs-site.xml </strong>file.</p><pre>sudo nano /usr/local/hadoop/etc/hadoop/hdfs-site.xml</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/586/1*VxR-T0TexU33kcRkurGfwA.png" /></figure><p>Add the following configurations:</p><pre>&lt;configuration&gt;<br>&lt;property&gt;<br>&lt;name&gt;dfs.namenode.name.dir&lt;/name&gt;&lt;value&gt;/usr/local/hadoop/data/nameNode&lt;/value&gt;<br>&lt;/property&gt;<br>&lt;property&gt;<br>&lt;name&gt;dfs.datanode.data.dir&lt;/name&gt;&lt;value&gt;/usr/local/hadoop/data/dataNode&lt;/value&gt;<br>&lt;/property&gt;<br>&lt;property&gt;<br>&lt;name&gt;dfs.replication&lt;/name&gt;<br>&lt;value&gt;2&lt;/value&gt;<br>&lt;/property&gt;<br>&lt;/configuration&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/588/1*RTr_mKjo0vBm0pv0HFVqEw.png" /></figure><h3>26th Step:</h3><p>We’re still on hadoop-master, let’s open the <strong>workers </strong>file:</p><pre>sudo nano /usr/local/hadoop/etc/hadoop/workers</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/585/1*KmFcZt9dqpz1kg-WNB85QQ.png" /></figure><p>Add these two lines: (the slave names, remember the hosts file?)</p><pre>hadoop-slave1<br>hadoop-slave2</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*pV90ZOOQGPgi_UPgXuQGbg.png" /></figure><h3>27th Step:</h3><p>We need to copy the Hadoop Master configurations to the slaves, to do that we use these commands:</p><pre>scp /usr/local/hadoop/etc/hadoop/* hadoop-slave1:/usr/local/hadoop/etc/hadoop/<br>scp /usr/local/hadoop/etc/hadoop/* hadoop-slave2:/usr/local/hadoop/etc/hadoop/</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*oiC7girl84IPPox93O9Qug.png" /><figcaption>Copying information to Slave1</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*YlxDNlJIXVIgfBgiEoCJgw.png" /><figcaption>Copying Information to Slave2</figcaption></figure><h3>28th Step:</h3><p>Now we need to format the HDFS file system. Run these commands:</p><pre>source /etc/environment<br>hdfs namenode -format</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*b7puLjP-Cm3qWo63fC1cfw.png" /></figure><h3>29th Step:</h3><p>Start HDFS with this command:</p><pre>start-dfs.sh</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*JuLX96vOCqTsJMzoN_s9tg.png" /></figure><p>To check if this worked, run the follwing command. This will tell you what resources have been initialized:</p><pre>jps</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*VAFJThOqhSC5FVU2DlM4sw.png" /></figure><p>Now we need to do the same in the slaves:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*klXKoJ_AT62tjEuLYFuM-Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*rhVZZVJ_bBiJGKk8rqfDFQ.png" /></figure><h3><strong>30th Step:</strong></h3><p>Let’s see if this worked:</p><p>Open your browser and type <a href="http://hadoop-master:8088/cluster">hadoop-master:9870</a>.</p><p>This is what mine shows, hopefully yours is showing the same thing!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*pgOYYBWUsNokBe18imVY3A.png" /></figure><p>As you can see, both nodes are operational!</p><h3>31st Step:</h3><p>Let’s configure <strong>yarn</strong>, just execute the following commands:</p><pre>export HADOOP_HOME=&quot;/usr/local/hadoop&quot;<br>export HADOOP_COMMON_HOME=$HADOOP_HOME<br>export HADOOP_CONF_DIR=$HADOOP_HOME/etc/hadoop<br>export HADOOP_HDFS_HOME=$HADOOP_HOME<br>export HADOOP_MAPRED_HOME=$HADOOP_HOME<br>export HADOOP_YARN_HOME=$HADOOP_HOME</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*X0ep1p-Tt3iVK5m9oKrvZg.png" /></figure><h3>32nd Step:</h3><p>In both slaves, open <strong>yarn-site.xml</strong> on nano:</p><pre>sudo nano /usr/local/hadoop/etc/hadoop/yarn-site.xml</pre><p>You have to add the following configurations on both slaves:</p><pre>&lt;property&gt;<br>&lt;name&gt;yarn.resourcemanager.hostname&lt;/name&gt;<br>&lt;value&gt;hadoop-master&lt;/value&gt;<br>&lt;/property&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*_S4HS5SLy3fLvWOFgfN48w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*fOhEIngE_UYUWRe8xihnCw.png" /></figure><h3>33rd Step:</h3><p>On the master, let’s start yarn. Use this command:</p><pre>start-yarn.sh</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/577/1*qWutsoPdt5YtfAl-Q36jDA.png" /></figure><h3>34th Step:</h3><p>Open your browser. Now you will type <a href="http://hadoop-master:8088/cluster">http://hadoop-master:8088/cluster</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*Q6JvaURdvnKJoeMi59Hh0A.png" /></figure><p>As you can see, the cluster shows 2 active nodes!</p><h3>35th Step:</h3><p>Just kidding, there are no more steps. Hopefully you managed to do it all correctly, and if so, congratulations on building a Hadoop Multi-Node Cluster!</p><p>If not, try again and again until you get it right!</p><p>Cheers.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=567ca44a3b12" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Criação de uma API RESTful com autenticação (LoopBack + MongoDB) no Ubuntu]]></title>
            <link>https://medium.com/@jootorres_11979/cria%C3%A7%C3%A3o-de-uma-api-restful-com-autentica%C3%A7%C3%A3o-loopback-mongodb-no-ubuntu-c461fc138682?source=rss-6a9a21d69804------2</link>
            <guid isPermaLink="false">https://medium.com/p/c461fc138682</guid>
            <category><![CDATA[loopback]]></category>
            <category><![CDATA[mongodb]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[João Torres]]></dc:creator>
            <pubDate>Fri, 19 Jul 2019 00:40:28 GMT</pubDate>
            <atom:updated>2019-07-19T11:36:34.715Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*g2fY53542AjOgEjW.png" /></figure><h3>Objetivo</h3><p>O objetivo deste tutorial é a criação de uma aplicação para um restaurante que permita inserir e consultar sobremesas.</p><p>Vamos usar, como linguagem de programação, o Node.js, como base de dados o MongoDB e a framework LoopBack que nos vai permitir criar facilmente <em>end-to-end</em> REST APIs.</p><p>Para a criação de uma API RESTful com autenticação devem ter o Node.js já instalado. Caso ainda não tenha, basta executar os seguintes comandos:</p><pre>$ sudo apt-get uptade<br>$ sudo apt-get install nodejs<br>$ sudo apt-get install npm</pre><h3>O que é o LoopBack?</h3><p>É uma framework Node.js <em>open source</em> altamente extensível usada para criar dinamicamente APIs REST de forma rápida. As APIs geradas através do LoopBack são APIs Swagger.</p><h3>O que é a Arquitetura REST?</h3><p>A <em>Representational State Transfer</em> (REST), em português Transferência de Estado Representacional, é um estilo de arquitetura que define um conjunto de restrições e propriedades baseados em HTTP. <em>Web Services</em>que obedecem ao estilo arquitetural REST, ou <em>web services</em> RESTful, fornecem interoperabilidade entre sistemas de computadores na Internet.</p><h3>Criação do projeto</h3><ol><li>Criar diretório para o projeto</li></ol><pre>user@ubuntu:~$ <strong>mkdir restaurante<br></strong>user@ubuntu:~$ <strong>cd restaurante<br></strong>user@ubuntu:~/restaurante$</pre><h3>Instalação do LoopBack</h3><p>De forma a instalar o loopback globalmente, deve executar o comando:</p><pre>user@ubuntu:~/restaurante$ <strong>sudo npm install -g loopback-cli</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/724/1*nXWZm85PnXwo5iQKF8N3mw.png" /></figure><h3>Criação da Aplicação</h3><p>Para configurar e criar a nossa aplicação, basta executar o comando <strong><em>lb </em></strong>e preencher os campos, como mostra a seguinte figura:</p><pre>user@ubuntu:~/restaurante$ <strong>lb<br></strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/724/1*x1nYF5SEVtDfScAZIqxnpQ.png" /></figure><p>Quando esta acção acabar, a nossa app está criada!</p><p>Vamos iniciar o servidor para comprovar:</p><pre>user@ubuntu:~/restaurante$<strong> node .</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/1*N9fCPR-9_LB4ZHfAN3Ci3A.png" /></figure><p>Aceda a<strong> </strong><a href="http://localhost:3000/explorer?source=post_page---------------------------"><strong>http://localhost:3000/explorer</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/735/1*9twlpQXrtnB2Cttj1aFGfQ.png" /></figure><p>A API está a funcionar!</p><h3>Ligação da aplicação à base de dados MongoDB</h3><p>Para efetuar a ligação à base de dados, necessita de criar um <em>cluster </em>no<strong>mongoDB Atlas</strong><em>.</em></p><p>De seguida, copie a <em>string </em>de ligação do MongoDB</p><pre>mongodb+srv://joao:<strong>&lt;password&gt;</strong>@crud-nodejs-oghzg.mongodb.net/test?retryWrites=true&amp;w=majority</pre><pre>mongodb+srv://joao:<strong>&lt;password&gt;</strong>@crud-nodejs-oghzg.mongodb.net/test?retryWrites=true</pre><ol><li>Instalação do <strong><em>MongoDB Connector</em></strong></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/727/1*mPYE69Ch_heK6Nhq6FThLw.png" /></figure><p>Ligação ao conector</p><p>Vamos dar o nome ‘<strong><em>test</em></strong>’ à nossa base de dados e configurar os parâmetros de autenticação para que seja criada no mongoDB Atlas.</p><pre>user@ubuntu:~/restaurante$<strong> sudo lb datasource test --connector mongoDB</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/736/1*K5Ra-HLUSDB7yZNY3y7IPA.png" /></figure><h3>Criação do modelo de dados</h3><p>Depois de criada a base de dados, vamos agora criar as “tabelas”, que no mongoDB se chamam <em>collections</em>. Para este exemplo vamos criar a <em>collection </em>(model) ‘<em>Sobremesas’</em>.</p><p>Para criar um modelo basta executar o comando <strong><em>lb model</em></strong> e preencher os dados solicitados.</p><pre>user@ubuntu:~/restaurante$<strong> lb model</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/729/1*wyb9xJKR3zy-MqvVzsaD9g.png" /></figure><p>Vamos agora iniciar novamente o servidor e verificar se já existem dois modelos na nossa API (Sobremesas e Users).</p><pre>user@ubuntu:~/restaurante$<strong> node .</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/735/1*o4TBg9uuYa48IsE02FA80Q.png" /></figure><h3>Inserir dados</h3><p>Para testar se a nossa API está a funcionar corretamente, vamos inserir um novo registo para as Sobremesas, ou seja, fazer um <strong>POST</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/642/1*UjCufQrYBhOPGg-hiyFl9Q.png" /></figure><p>Clique em <strong><em>Try it Out!</em></strong></p><p>Deverá obter uma resposta com o código 200, que significa sucesso na operação.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*qGUeyU8c2s1np-Okgg-Gcw.png" /></figure><p>Para ver se o registo foi inserido, vamos fazer <strong>GET</strong> das Sobremesas.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/641/1*aGyWoR9Je7sWfZLHFks7IQ.png" /></figure><p>Podemos também ver se o registo foi inserido em <a href="http://localhost:3000/api/Sobremesas?source=post_page---------------------------"><strong>http://localhost:3000/api/Sobremesas</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/605/1*gGxVQFioO2_gjPUzJ8gubA.png" /></figure><p>Ou então no nosso <em>cluster</em>, no Atlas:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/591/1*AXHsu9XdWAyM_KGUTq6KFw.png" /></figure><h3>Autenticação</h3><p>Para configurar a autenticação, devemos executar o seguinte comando:</p><pre>user@ubuntu:~/restaurante$<strong> lb acl</strong></pre><p>Proceda à seguinte configuração:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/729/1*sC8Yfj7JBwc62rwFU6Ik9Q.png" /></figure><p>Vamos agora fazer <strong>GET</strong> das sobremesas: <a href="http://localhost:3000/api/Sobremesas?source=post_page---------------------------"><strong>http://localhost:3000/api/Sobremesas</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/713/1*qc74H2OayTv__pwlFSlwcg.png" /></figure><p>Como pode reparar, não podemos ver as Sobremesas sem estabelecer uma autenticação.</p><p>Para fazer a autenticação, precisamos de criar um utilizador (user).</p><p>Vamos proceder à criação de um utilizador (<strong>POST /Users</strong>)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/641/1*U1BGgXVSxWQFrRf2Egih4A.png" /></figure><p>Para fazer login, vamos fazer um <strong>POST /Users/login</strong> , indicando o <em>username</em> e <em>password</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/641/1*kvkgbA3oFwn4cArF2gS8Iw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/641/1*SBam_1Uw_LqNL8syh06zIw.png" /></figure><p>Para efetuar a autenticação, vamos precisar de definir um <em>Token</em> de acesso<em>. </em>Para tal, vamos copiar o “<strong>id</strong>” retornado e colar no campo que está no início da página (canto superior direito). De seguida, clique em ‘<strong><em>Set Access Token’.</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/691/1*HWOljk60CmyqCjl4jyv99g.png" /></figure><p>Após definir o <em>Token</em>, estamos autenticados, o que significa que já conseguimos fazer <strong>GET</strong> das Sobremesas.</p><ol><li>Instalação do <strong><em>MongoDB Connector</em></strong></li></ol><pre>user@ubuntu:~/restaurante$<strong> npm i loopback-connector-mongodb</strong></pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c461fc138682" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Integração de API usando dedos meteorológicos com node.js]]></title>
            <link>https://medium.com/@jootorres_11979/integra%C3%A7%C3%A3o-de-api-usando-dedos-meteorol%C3%B3gicos-com-node-js-dc3c383af030?source=rss-6a9a21d69804------2</link>
            <guid isPermaLink="false">https://medium.com/p/dc3c383af030</guid>
            <category><![CDATA[apple]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[weather]]></category>
            <dc:creator><![CDATA[João Torres]]></dc:creator>
            <pubDate>Thu, 18 Jul 2019 17:44:29 GMT</pubDate>
            <atom:updated>2019-07-18T17:45:00.675Z</atom:updated>
            <content:encoded><![CDATA[<h3>Integração de API usando dados meteorológicos com node.js</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/0*fsqTvGk8fwP38Rh2.jpg" /></figure><p>Neste tutorial irei ensinar como desenvolver uma API com a qual iremos analisar dados meteorológicos.</p><p><strong>Requisitos:</strong></p><ul><li><strong>Ubuntu instalado (ou virtualizado).</strong></li><li><strong>Node.js instalado</strong> — interpretador de código javascript open-source que funciona do lado do servidor.</li><li>Criar conta no endereço <a href="https://openweathermap.org/?source=post_page---------------------------">https://openweathermap.org/</a></li></ul><h3>1) Instalação do Node.js</h3><p>Para instalar o node.js:</p><pre>user@ubuntu:~$ <strong>sudo apt-get update<br></strong>user@ubuntu:~$ <strong>sudo apt-get install nodejs</strong></pre><p>Também será necessário instalar o gestor de pacotes do node.js (npm- Node Package Manager). Para tal executamos o comando:</p><pre>user@ubuntu:~$ <strong>sudo apt-get install npm</strong></pre><h3>2) Preparação</h3><p>Primeiro vamos criar uma pasta no nosso ambiente de trabalho, com o nome meteo.</p><pre>user@ubuntu:~$ <strong>cd /home/user/Desktop<br></strong>user@ubuntu:~/Desktop$ <strong>mkdir meteo</strong></pre><p>Agora iniciamos o nosso projeto com o comando npm init, que ira criar um ficheiro package.json, que mais tarde iremos configurar.</p><pre>user@ubuntu:~/Desktop$ <strong>cd meteo<br></strong>user@ubuntu:~/Desktop/API-IP$ <strong>npm init</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/718/1*LkL996SGw6Jdo4OKCjs4nA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/718/1*LyL7IoBhRuLOobID6cIqBg.png" /></figure><p>Agora precisamos criar um ficheiro dentro da pasta criada anteriormente, com nome ‘<strong>index.js</strong>’.</p><pre>user@ubuntu:~/Desktop/meteo <strong>touch index.js</strong></pre><h3>Pubic IP Address API</h3><p>Iremos usar um modulo chamado npm request para fazer as requisições HTTP, instalando através do seguinte comando:</p><pre>user@ubuntu:~/Desktop/API-IP$ <strong>npm install request --save</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/721/1*5e7T3vT1GPe5AavdCIoasg.png" /></figure><h3>3) Programar a nossa API</h3><p>Começamos por editar o ficheiro ‘<strong>index.js</strong>’:</p><pre>user@ubuntu:~/Desktop/meteorologia$ <strong>gedit index.js</strong></pre><p><strong>Inserimos o seguinte código no ficheiro index:</strong></p><pre>let request = require(&#39;request&#39;);<br> <br>let apiKey = &#39;e306bc0cb38032abdae3550cf4782400&#39;;<br>let cidade = &#39;Guarda&#39;;<br>let url = `<a href="http://api.openweathermap.org/data/2.5/weather?q=%24%7Bcidade%7D&amp;appid=%24%7BapiKey%7D&amp;units=metric%60&amp;source=post_page---------------------------">http://api.openweathermap.org/data/2.5/weather?q=${cidade}&amp;appid=${apiKey}&amp;units=metric`</a><br>let dados =&#39;&#39;;<br> <br>request(url, function (err, response, body) {<br> if(err){<br>  console.log(&#39;error:&#39;, error);<br> } else {<br> let weather = JSON.parse(body);<br> dados = `Dados Metereológicos para a ${weather.name}:<br> -Temperatura: ${weather.main.temp}ºc<br> -Velocidade vento:  ${weather.wind.speed}~<br> -Temp Máxima: ${weather.main.temp_max}ºC<br> -Temp Minima: ${weather.main.temp_min}ºC<br> -Humidade: ${weather.main.humidity}%`console.log(dados);<br>   }<br>});</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/732/1*mD9Gkrw7f0vX49YdGkFPDA.png" /></figure><p><strong>A apiKey presente no código anterior é obtida a partir d</strong><a href="https://home.openweathermap.org/?source=post_page---------------------------"><strong>e https://home.openweathermap.org</strong></a><strong>/api_keys</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/719/1*_hQObfp1xt6ojRICg7ccXg.png" /></figure><h3>4) Correr a Aplicação</h3><p>Para correr a aplicação usamos o código:</p><pre>user@ubuntu:~/Desktop/meteo$ <strong>node index.js</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/723/1*pj62Z_KC6Qk6ykzSW3_BDw.png" /></figure><p>Como podemos ver, a nossa aplicação já mostra os dados meteorológicos que queríamos que ela nos mostrasse.</p><h3>5) Correr a Aplicação num servidor http</h3><p>Caso queira correr a aplicação num servidor http, é extremamente fácil:</p><p>Para tal, vamos editar o ficheiro index.js. Ainda se lembra do código usado para o editar?</p><pre>user@ubuntu:~/Desktop/meteo$ <strong>gedit index.js</strong></pre><p>No ficheiro index.js, vamos acrescentar o seguinte código:</p><pre>const http = require(&#39;http&#39;);<br>var server = http.createServer(function(req, res) {<br> res.writeHead(200,{&#39;Content-Type&#39;:&#39;text/json, charset=utf-8&#39;});<br> res.write(dados);<br> res.end();<br>});server.listen(8001, () =&gt;{<br> console.log(&quot;Server is running on <a href="http://localhost:8001/?source=post_page---------------------------">http://localhost:8001</a>&quot;);<br>});</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/707/1*OxWD1GgOCHmfQkMjJtkVSg.png" /></figure><p>Para correr, escrevemos o código:</p><pre>user@ubuntu:~/Desktop/temperatura$ <strong>node</strong> <strong>index.js</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/733/1*kmvjBcTkT5ZeVli9HzHdXg.png" /></figure><p>Como podemos ver, o terminal diz-nos que a API está a correr em <a href="http://localhost:8001/?source=post_page---------------------------">http://localhost:8001</a>. Então, tudo o que temos que fazer é visitar esse link no nosso browser.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/621/1*_DQIsm8v4xqoAbWMxL-dQg.png" /></figure><p>Como podemos ver, a informação desejada quanto aos dados meteorológicos está a ser mostrada no browser.</p><h3>Notas Finais</h3><p>Com isto chegamos ao final do tutorial, espero que tenho conseguido construir esta pequena API com dados meteorológicos e espero também que tenha gostado. Até à próxima!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dc3c383af030" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Integração de API usando Endereço IP com node.js]]></title>
            <link>https://medium.com/@jootorres_11979/integra%C3%A7%C3%A3o-de-api-usando-endere%C3%A7o-ip-com-node-js-4dbfd2acaffb?source=rss-6a9a21d69804------2</link>
            <guid isPermaLink="false">https://medium.com/p/4dbfd2acaffb</guid>
            <dc:creator><![CDATA[João Torres]]></dc:creator>
            <pubDate>Thu, 18 Jul 2019 17:07:49 GMT</pubDate>
            <atom:updated>2019-07-18T17:14:59.468Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*HrIVsrBb2n-Z0UCf.png" /></figure><p>Neste tutorial desenvolveremos uma API que nos permitirá obter dados relativos da máquina através do endereço de IP publico .</p><p>Requisitos:</p><ul><li><strong>Ubuntu instalado (ou virtualizado).</strong></li><li><strong>Node.js instalado</strong> — interpretador de código javascript open-source que funciona do lado do servidor.</li><li><strong>Pubic IP Address API</strong> — consultar site <a href="http://www.ip-api.com/?source=post_page---------------------------"><strong>www.ip-api.com</strong></a></li></ul><h3>1) Instalar o node.js</h3><p>Para instalar o node.js deverá correr os seguintes comandos:</p><pre>user@ubuntu:~$ sudo apt-get update<br>user@ubuntu:~$ sudo apt-get install nodejs</pre><p>Vamos também instalar o gestor de pacotes do node.js (npm - Node Package Manager) através do seguinte comando :</p><pre>user@ubuntu:~$ <strong>sudo apt-get install npm</strong></pre><h3>2) Preparação</h3><p>Começamos por criar uma pasta no nosso ambiente de trabalho, com o nome API-IP, através dos seguintes códigos.</p><pre>user@ubuntu:~$ <strong>cd /home/user/Desktop<br></strong>user@ubuntu:~/Desktop$ <strong>mkdir API-IP</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/732/1*hOTJz4KuIfAi_Dr6rY_KQw.png" /></figure><p>Agora iniciamos o nosso projeto com o comando npm init, que irá criar um ficheiro package.json que vamos configurar mais tarde.</p><pre>user@ubuntu:~/Desktop$ <strong>cd API-IP<br></strong>user@ubuntu:~/Desktop/API-IP$ <strong>npm init</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/729/1*DFm8gJ3yef3m7b691Im_VQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/733/1*tK3gdAzYpZoXbsmW-iS4SQ.png" /></figure><p>De seguida vamos criar um ficheiro dentro da pasta criada anteriormente, com nome ‘<strong>index.js</strong>’.</p><pre>user@ubuntu:~/Desktop/API-IP$ <strong>touch index.js</strong></pre><h3>Pubic IP Address API</h3><p>Iremos usar um modulo chamado npm request para fazer as requisições HTTP, instalando através do seguinte comando:</p><pre>user@ubuntu:~/Desktop/API-IP$ <strong>npm install request --save</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/728/1*1Vfc6j6DWPEXfmwNbwVFaw.png" /></figure><h3>3)Programar a nossa API</h3><p>Para começar editamos o ficheiro ‘<strong>index.js</strong>’. Para tal usamos o seguinte comando:</p><pre>user@ubuntu:~/Desktop/API-IP$ <strong>gedit index.js</strong></pre><p>Inserimos o seguinte código no ficheiro index:</p><pre>let request = require(‘request’);<br> <br>let url = `<a href="http://ip-api.com/json%60?source=post_page---------------------------">http://ip-api.com/json`</a><br>let dados = ‘’;<br> <br>request(url, function (err, response, body) {<br>   if(err){<br>        console.log(‘error:’, err);<br>   } else {<br>       let ipInfo = JSON.parse(body);<br>       dados = `<br>                — — — — — Localização — — — — — <br>                — — — — — — — — — — — — — — — -<br>                IP: ${ipInfo.query}<br>                País: ${ipInfo.country}<br>                Cidade: ${ipInfo.city}<br>                Região: ${ipInfo.regionName}<br>                Lat: ${ipInfo.lat}<br>                Lon: ${ipInfo.lon}<br>                Organização: ${ipInfo.org}<br>                 — — — — — — — — — — — — — — — -<br> `<br>       console.log(dados);<br> }<br>});</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/734/1*C8xYAe0tL4GfUx7_rreoFw.png" /></figure><p>Agora deve aceder a <a href="http://ip-api.com/json?source=post_page---------------------------">ip-api.com</a> e copiar a URL da API, que será <a href="http://ip-api.com/json?source=post_page---------------------------"><strong>http://ip-api.com/json</strong></a></p><h3>4) Correr a Aplicação</h3><p>Para correr a aplicação usamos o código:</p><pre>user@ubuntu:~/Desktop/API-IP$ <strong>node index.js</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/677/1*vzCtmHmHUqyHK5lW5KU1WQ.png" /></figure><p>Como podemos ver, já apresenta as informações que pretendemos mostrar com esta API.</p><h3>5) Correr a Aplicação num servidor http</h3><p>Caso queira correr a aplicação num servidor http, é extremamente fácil:</p><p>Para tal, vamos editar o ficheiro index.js. Ainda se lembra do código usado para o editar?</p><pre>user@ubuntu:~/Desktop/API-IP$ <strong>gedit index.js</strong></pre><p>E vamos acrescentar o seguinte código:</p><pre>const http = require<strong>(</strong>&#39;http&#39;<strong>)</strong>;var server = http.createServer<strong>(function(</strong>req, res<strong>)</strong> <strong>{</strong><br> res.writeHead<strong>(</strong>200<strong>)</strong>;<br> res.write<strong>(dados)</strong>;<br>        res.end();<br><strong>})</strong>;server.listen<strong>(</strong>8001, () =&gt; {<br>        console.log(&quot;Server is running on http://localhost:8001&quot;);<br>}<strong>)</strong>;</pre><p>Para correr o programa, fazemos de novo:</p><pre>user@ubuntu:~/Desktop/API_IP$ <strong>node</strong> <strong>index.js</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/1*lE5YBiQpuKcOTdtY1jZDbQ.png" /></figure><p>Como podemos ver, o terminal diz-nos que a API está a correr em <a href="http://localhost:8001/">http://localhost:8001</a>. Então, tudo o que temos que fazer é visitar esse link no nosso browser.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/1*k-D2QPvfcnz9km7OGd0Iug.png" /></figure><p>Como podemos ver, a informação desejada está a ser mostrada no browser.</p><h3>Notas Finais</h3><p>Com isto chegamos ao final do tutorial, espero que tenho conseguido construir esta pequena API e espero também que tenha gostado. Até à próxima!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4dbfd2acaffb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Criação de uma API RESTful com node.js e loopback]]></title>
            <link>https://medium.com/@jootorres_11979/cria%C3%A7%C3%A3o-de-uma-api-restful-com-node-js-e-loopback-fc441fa88290?source=rss-6a9a21d69804------2</link>
            <guid isPermaLink="false">https://medium.com/p/fc441fa88290</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[restful-api]]></category>
            <category><![CDATA[loopback]]></category>
            <dc:creator><![CDATA[João Torres]]></dc:creator>
            <pubDate>Thu, 30 May 2019 22:11:31 GMT</pubDate>
            <atom:updated>2019-05-30T22:11:31.714Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/264/1*o_4IQfLvXULbJP82m1KxeQ.png" /></figure><h3>O que é o Loopback?</h3><p>Loopback é uma framework Node.js open-source altamente extensível usada para criar dinamicamente APIs REST facilmente.</p><h3>O que é a Arquitectura REST?</h3><p>REpresentational State Transfer é um modelo de arquitectura que usa o protocolo HTTP para comunicação.</p><h3>Instalação</h3><p>Primeiro, começamos por abrir a Linha de Comandos do nosso computador e vamos instalar o Loopback.</p><pre>npm install -g loopback-cli</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U4AATOg-pcjNDC3BiCK9Ag.png" /></figure><p>De seguida, criamos um projecto:</p><pre>lb</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*-xk57jgiinRMALMVUlO_0A.png" /></figure><p>Entramos no directório do testeloopback (no meu caso, se tiverem dado outro nome, terão que colocar o nome que utilizaram):</p><pre>cd testeloopback</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/664/1*MagVRoqYzETu_hY2bUIwjw.png" /></figure><p>Depois, criamos um model. Aparecerão várias opções mas como isto é apenas um teste, não será necessário fazer muitas coisas:</p><pre>lb model</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/665/1*gesYusHq0ueFchwT_K4d2A.png" /></figure><p>Agora basta correr o server, para isso usamos:</p><pre>node .</pre><p>Agora, no browser vamos a:</p><pre><a href="http://localhost:3000/explorer">http://localhost:3000/explorer</a></pre><p>E podemos ver o nosso teste de loopback:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/968/1*s_xgOZf1arcKOD65xsPdJg.png" /></figure><p>Se chegou aqui, conseguiu criar uma RESTful API com node.js e loopback!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fc441fa88290" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Virtualização em ProxMox]]></title>
            <link>https://medium.com/@jootorres_11979/virtualiza%C3%A7%C3%A3o-em-proxmox-860a00cd8bae?source=rss-6a9a21d69804------2</link>
            <guid isPermaLink="false">https://medium.com/p/860a00cd8bae</guid>
            <category><![CDATA[virtualization]]></category>
            <category><![CDATA[proxmox]]></category>
            <dc:creator><![CDATA[João Torres]]></dc:creator>
            <pubDate>Thu, 30 May 2019 21:15:29 GMT</pubDate>
            <atom:updated>2019-05-30T22:19:54.434Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*5QdT3tR-u0z3Zk8dt_72lg.jpeg" /></figure><h3>Objetivo</h3><p>Neste tutorial vamos aprender como instalar o ProxMox VE. Neste caso usaremos o Oracle VM VirtualBox.</p><h3>O que é o ProxMox?</h3><p>O ProxMox VE é um ambiente de virtualização de servidores de código aberto. É uma distribuição Linux baseada no Debian com um kernel Ubuntu LTS modificado.</p><h3>Instalação</h3><h3>1) Para começar, fazer o download do ISO do ProxMox, para tal seguir o link:</h3><p><a href="https://www.proxmox.com/en/downloads/category/iso-images-pve">Get the free Proxmox VE ISO installer</a></p><h3>2) Criamos uma Virtual Machine (no meu caso será criada no Oracle VM VirtualBox)</h3><p>Na criação, as seguintes opções devem estar seleccionadas.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/411/1*KViDJxpl0YYHLfQtFEBflA.png" /><figcaption>Criação da Virtual Machine — parte 1</figcaption></figure><p>Uma vez que servirá de host para várias máquinas, é importante dar um tamanho razoável de RAM e disco.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/410/1*zmB4U_2DveayJxgeXqBf5A.png" /><figcaption>Criação da Virtual Machine — parte 2</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/430/1*MgdziMga9NFCRfEStX50WQ.png" /><figcaption>Criação da Virtual Machine — parte 3</figcaption></figure><p>Depois de clicarmos em “Create”, temos que aceder às Settings da nossa VM, e na parte de Storage, vamos inserir o ISO do ProxMox do qual fizemos o download previamente, para isso basta seguir estes passos:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/631/1*pE6a53KaRfAK8WaB_MaIiw.png" /><figcaption>Escolha do ProxMox — parte 1</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/648/1*YPxEzwLxtfx-ekkMzrS5PQ.png" /><figcaption>Escolha do ProxMox — parte 2</figcaption></figure><p>Também devemos ir à aba “Network” e mudar para “Bridged Adapter”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/647/1*eyFcx1S1anV0feyZeXZTqw.png" /></figure><p>Depois disto, podemos iniciar a nossa Virtual Machine!</p><h3>2) Instalação do ProxMox VE</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/643/1*2E4CFlIsFcuPTPVTy2ioDQ.png" /><figcaption>ProxMox — Instalação</figcaption></figure><p>Seleccionamos a opção “Install ProxMox VE” e na página seguinte concordamos com os termos de instalação e vamos também seleccionar o país em que estamos.</p><p>Depois de feitos os passos anteriores, chegaremos a uma página em que devemos inserir uma password e um e-mail válido.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I8VaMx11ZbaRQhZiSsBJSA.png" /></figure><p>Agora é hora de configurar a rede:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1002/1*hwHQWuLWrJy2RM0nsm7qLQ.png" /></figure><p>Clicando em “Next”, deverá aparecer um ecrã parecido com este:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/994/1*HOX9ufyMoU020GrpaN_xlA.png" /></figure><p>De seguida, clicamos em instalar e esperamos que a instalação seja concluída.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/616/1*3h6e1MkgtO-BdpY3Efwl4Q.png" /><figcaption>Instalação do ProxMox VE</figcaption></figure><p>Quando acaba de instalar, carregamos em “Reboot”.</p><p>Quando acabar de reiniciar, fechamos a VM e retiramos o ISO.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/747/1*k4_SW33fOOfIO9baunEaCQ.png" /></figure><p>Depois, voltamos a reiniciar a máquina e passado algum tempo irá aparecer a seguinte página que pedirá a password escolhida anteriormente. O login será “root”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/801/1*-kGYOTNU_E3HLWc58D0zSQ.png" /></figure><p>Se conseguir chegar aqui, significa que conseguiu instalar o ProxMoxVE com sucesso.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=860a00cd8bae" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>