Thursday, December 11, 2014

Ubuntu 14.04 - Install MongoDB

In this tutorial, you will be introduce how to install MongoDB on Ubuntu Server 14.04

Step 1: make sure your server is up-to-date
1
2
apt-get update
apt-get dist-upgrade

Step 2: install MongoDB
1
apt-get install -y mongodb

After installing, MongoDB service automatically starts. You can check by typing:
1
ps aux | grep mongodb

And the screen will display:
1
mongodb    2163  0.8  1.1 382048 46256 ?        Ssl  15:36   0:03 /usr/bin/mongod --config /etc/mongodb.conf

More

1. Start MongoDB

Issue the following command to start mongodb:
1
sudo service mongodb start

2. Stop MongoDB

Issue the following command to stop mongodb:
1
sudo service mongodb stop

3. Restart MongoDB

Issue the following command to restart mongodb:
1
sudo service mongodb restart


Wednesday, December 10, 2014

Ubuntu Server 14.04 - Install Nodejs

In this tutorial, you will be introduced how to install Nodejs by using Distro-Stable Version or a PPA (personal package archive).

How To Install the Distro-Stable Version
First, you should refresh your local package index prior and then install from the repositories:
1
2
sudo apt-get update
sudo apt-get install nodejs

Then, you should install npm (Node Package Manager) because by default Ubuntu Server 14.04 does not include npm package in nodejs package
1
sudo apt-get install npm

How To Install Using a PPA from NodeSource
The versions of Node.js on NodeSource are more up-to-date than the official Ubuntu repositories.

First, install the PPA in order to get access to its contents:
1
curl -sL https://deb.nodesource.com/setup | sudo bash -

Then, install Node.js package:
1
sudo apt-get install nodejs

Note that when you install from NodeSource, the Node.js package also contains npm package, so you do not need to install npm. But to make sure all npm packages can run right, you should need to install build-essential packge:
1
sudo apt-get install build-essential

To verify Node.js installed successfully, you type nodejs and see the command line >

Tuesday, December 9, 2014

Ubuntu Server 14.04 - Enable SSH root access

Be default, Ubuntu does not allow to access ssh through root user. To do this, following steps:

Step 1: set password for root user. See details at Change root user password

Step 2: edit file "/etc/ssh/sshd_config"
1
vi /etc/ssh/sshd_config

Step 3: comment and add new line like this
1
2
#PermitRootLogin without-password
PermitRootLogin yes

Step 4: restart SSH
1
service ssh restart

Ubuntu Server 14.04 - Config static IP address

Ubuntu 14.04 no longer allows you to edit "/etc/resolv.conf directly". Instead, you have to modify "/etc/resolvconf/resolv.conf.d/" head or tail. But it will be easiest to set this along side your ethernet config, in my opinion.

You need to edit the file "/etc/network/interfaces":

1
sudo  vi /etc/network/interfaces

Then change to include the following:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
#iface lo inet interface
iface eth0 inet static
address 192.168.1.200
netmask 255.255.255.0
gateway 192.168.1.1
network 192.168.1.0
broadcast 192.168.1.255
dns-nameservers 8.8.8.8 8.8.4.4

Finally, restart your ethernet
1
ifdown eth0 && ifup eth0

Ubuntu Server 14.04 - Change password

To do it command line:

To change the root password:
1 sudo passwd

To change your user password:
1 passwd

To change other users password:
1 sudo passwd USERNAME

Tuesday, July 15, 2014

Ubuntu Server 14.04 - MySQL 5.5 installation and configuration

A. Installation:
1. Install MySQL
1
root@ubuntu:~# apt-get install mysql-server
 By default MySQL 5.5 will be installed.


2. Install MySQL Connector JAR file
1
root@ubuntu:~# apt-get install mysql-connector-java*

B. Configuration:
1. Set password for root user:
1
root@ubuntu:~# mysqladmin -u root -p'<your_password_here>' password $mysqlpassword

2. Remove unneccessary information from log and STDOUT
1
root@ubuntu:~# mysqladmin -u root 2>&1 >/dev/null

3.Start / Stop / Restart MySQL
1
2
3
root@ubuntu:~# service mysql start
root@ubuntu:~# service mysql stop
root@ubuntu:~# service mysql restart

Friday, July 11, 2014

Ubuntu Server 14.04 - Install Oracle JDK 1.7.x directly from PPA

Here is the codes that you can use to install Oracle JDK 1.7.x directly from PPA

1
2
3
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer

Thursday, May 29, 2014

Pivot and Unpivot operations in Oracle 11g Release 2

This article shows how to use the new PIVOT and UNPIVOT operators in 11g release 2.

PIVOT
For starting, we need some data
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
create table sales
(
    customer_id varchar2(10),   
    product_id varchar2(10),
    month number,
    amount number
);

insert into sales values('Adam','Product 1',1,134);
insert into sales values('Adam','Product 2',2,250);
insert into sales values('Adam','Product 3',3,190);
insert into sales values('Jones','Product 1',1,200);
insert into sales values('Jones','Product 3',2,126);
insert into sales values('Kanes','Product 2',1,240);
insert into sales values('Kanes','Product 2',2,210);
insert into sales values('Kanes','Product 3',3,250);
commit;


As all we know, relational tables are tabular - that is, they are presented in a column-value pair. Now the simple select query is below:
1
2
SELECT a.customer_id, a.product_id, a.month, a.amount
  FROM sales a;

And this is the output:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
CUSTOMER_ID PRODUCT_ID MONTH  AMOUNT    
----------- ---------- ------ --------- 
Adam        Product 1       1       134
Adam        Product 2       2       250
Adam        Product 3       3       190
Jones       Product 1       1       200
Jones       Product 3       2       126
Kanes       Product 2       1       240
Kanes       Product 2       2       210
Kanes       Product 3       3       250

Suppose if we want to transpose the query output to row-value instead of column-value, it is called Pivoting (Rows to Columns Conversion) in SQL. The output will be transposed as
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
CUSTOMER_ID MONTH  PRODUCT_1    PRODUCT_2    PRODUCT_3    
----------- ------ ------------ ------------ ------------ 
Adam             1          134                          
Adam             2                       250             
Adam             3                                    190
Jones            1          200                          
Jones            2                                    126
Kanes            1                       240             
Kanes            2                       210             
Kanes            3                                    250

Prior to Oracle Database 11g, you would do that via some sort of a DECODE function for each value and write each distinct value as a separate column (in this article we do not mention about DECODE function). The technique is quite nonintuitive however.

Fortunately, you now have a great new feature called PIVOT for presenting any query in the crosstab format using a new operator, appropriately named PIVOT. Here is how you write the query:
1
2
3
4
5
6
7
SELECT   *
    FROM sales PIVOT (SUM (amount)
               FOR product_id
               IN  ('Product 1' AS Product_1,
                   'Product 2' AS Product_2,
                   'Product 3' AS Product_3))
ORDER BY customer_id, month;

With the SELECT statement above, we notice that we have some NULL or empty values. To replace NULL values with another value like zero value we can use COALESCE function like this:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
SELECT   customer_id,
         month,
         COALESCE (Product_1, 0) Product_1,
         COALESCE (Product_2, 0) Product_2,
         COALESCE (Product_3, 0) Product_3
    FROM sales PIVOT (SUM (amount)
               FOR product_id
               IN  ('Product 1' AS Product_1,
                   'Product 2' AS Product_2,
                   'Product 3' AS Product_3))
ORDER BY customer_id, month;

Next, suppose that we have n products, how can we list these n products not product by product? In Oracle 11g, we can do this by using PIVOT plus XML clause:
1
2
3
4
5
SELECT   *
    FROM sales PIVOT XML (SUM (amount)
               FOR product_id
               IN (SELECT DISTINCT product_id FROM sales))
ORDER BY customer_id, month;

And here is the result:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
CUSTOMER_ID MONTH  PRODUCT_ID_XML 
----------- ------ -------------- 
Adam             1 <PivotSet><item><column name = "PRODUCT_ID">Product 1</column><column name = "SUM(AMOUNT)">134</column></item><item><column name = "PRODUCT_ID">Product 2</column><column name = "SUM(AMOUNT)"></column></item><item><column name = "PRODUCT_ID">Product 3</column><column name = "SUM(AMOUNT)"></column></item></PivotSet>
Adam             2 <PivotSet><item><column name = "PRODUCT_ID">Product 1</column><column name = "SUM(AMOUNT)"></column></item><item><column name = "PRODUCT_ID">Product 2</column><column name = "SUM(AMOUNT)">250</column></item><item><column name = "PRODUCT_ID">Product 3</column><column name = "SUM(AMOUNT)"></column></item></PivotSet>
Adam             3 <PivotSet><item><column name = "PRODUCT_ID">Product 1</column><column name = "SUM(AMOUNT)"></column></item><item><column name = "PRODUCT_ID">Product 2</column><column name = "SUM(AMOUNT)"></column></item><item><column name = "PRODUCT_ID">Product 3</column><column name = "SUM(AMOUNT)">190</column></item></PivotSet>
Jones            1 <PivotSet><item><column name = "PRODUCT_ID">Product 1</column><column name = "SUM(AMOUNT)">200</column></item><item><column name = "PRODUCT_ID">Product 2</column><column name = "SUM(AMOUNT)"></column></item><item><column name = "PRODUCT_ID">Product 3</column><column name = "SUM(AMOUNT)"></column></item></PivotSet>
Jones            2 <PivotSet><item><column name = "PRODUCT_ID">Product 1</column><column name = "SUM(AMOUNT)"></column></item><item><column name = "PRODUCT_ID">Product 2</column><column name = "SUM(AMOUNT)"></column></item><item><column name = "PRODUCT_ID">Product 3</column><column name = "SUM(AMOUNT)">126</column></item></PivotSet>
Kanes            1 <PivotSet><item><column name = "PRODUCT_ID">Product 1</column><column name = "SUM(AMOUNT)"></column></item><item><column name = "PRODUCT_ID">Product 2</column><column name = "SUM(AMOUNT)">240</column></item><item><column name = "PRODUCT_ID">Product 3</column><column name = "SUM(AMOUNT)"></column></item></PivotSet>
Kanes            2 <PivotSet><item><column name = "PRODUCT_ID">Product 1</column><column name = "SUM(AMOUNT)"></column></item><item><column name = "PRODUCT_ID">Product 2</column><column name = "SUM(AMOUNT)">210</column></item><item><column name = "PRODUCT_ID">Product 3</column><column name = "SUM(AMOUNT)"></column></item></PivotSet>
Kanes            3 <PivotSet><item><column name = "PRODUCT_ID">Product 1</column><column name = "SUM(AMOUNT)"></column></item><item><column name = "PRODUCT_ID">Product 2</column><column name = "SUM(AMOUNT)"></column></item><item><column name = "PRODUCT_ID">Product 3</column><column name = "SUM(AMOUNT)">250</column></item></PivotSet>

We see that the PRODUCT_ID_XML column returns XML data type and it is very hard to see the result.
Now let's get the first XML value to see.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<PivotSet>
 <item>
  <column name = "PRODUCT_ID">Product 1</column>
  <column name = "SUM(AMOUNT)">134</column>
 </item>
 <item>
  <column name = "PRODUCT_ID">Product 2</column>
  <column name = "SUM(AMOUNT)"/>
 </item>
 <item>
  <column name = "PRODUCT_ID">Product 3</column>
  <column name = "SUM(AMOUNT)"/>
 </item>
</PivotSet>

With XML data type, we can use EXTRACTVALUE function with XPath structure to get the value that we want. We can do like this:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
SELECT customer_id,
       month,
       EXTRACTVALUE (product_id_xml, '/PivotSet/item[1]/column[2]') Product_1,
       EXTRACTVALUE (product_id_xml, '/PivotSet/item[2]/column[2]') Product_2,
       EXTRACTVALUE (product_id_xml, '/PivotSet/item[3]/column[2]') Product_3
  FROM (SELECT   *
            FROM sales PIVOT XML (SUM (amount)
                       FOR product_id
                       IN (SELECT DISTINCT product_id FROM sales))
        ORDER BY customer_id, month);

And here is what we want to see:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
CUSTOMER_ID MONTH  PRODUCT_1 PRODUCT_2 PRODUCT_3
----------- ------ --------- --------- ---------
Adam             1 134                 
Adam             2           250       
Adam             3                     190
Jones            1 200                 
Jones            2                     126
Kanes            1           240       
Kanes            2           210       
Kanes            3                     250

UNPIVOT
For starting, we need some data
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
create table sales_month
(
    customer_id varchar2(10),  
    product_id varchar2(10),
    january number,
    february number,
    march number
);
 
insert into sales_month values('Kanes','Product 1',0,0,1);
insert into sales_month values('Jones','Product 1',0,1,1);
insert into sales_month values('Kanes','Product 3',0,1,0);
insert into sales_month values('Kanes','Product 2',1,0,0);
insert into sales_month values('Adam','Product 2',0,1,0);
insert into sales_month values('Adam','Product 1',1,0,0);
insert into sales_month values('Adam','Product 3',0,0,1);
commit;


The UNPIVOT operator converts column-based data into separate rows. See the simple query before going to converting column-based data.
1
select * from sales_month;

And the result is:
1
2
3
4
5
6
7
8
9
CUSTOMER_ID PRODUCT_ID JANUARY  FEBRUARY  MARCH  
----------- ---------- -------- --------- ------ 
Kanes       Product 1         0         0      1
Jones       Product 1         0         1      1
Kanes       Product 3         0         1      0
Kanes       Product 2         1         0      0
Adam        Product 2         0         1      0
Adam        Product 1         1         0      0
Adam        Product 3         0         0      1

Prior to 11g, we can use the DECODE function and a pivot table with the correct number of rowsby using the CONNECT BY clause in a query from dual to generate the correct number of rows for the unpivot operation. But in this article we do not mention about this.

So we will use UNPIVOT function to convert column-based data into separate rows:
1
2
3
4
5
SELECT   *
    FROM sales_month UNPIVOT (yes
                     FOR month
                     IN ("JANUARY", "FEBRUARY", "MARCH"))
ORDER BY customer_id, product_id;

Here is the result:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
CUSTOMER_ID PRODUCT_ID MONTH    YES  
----------- ---------- -------- ---- 
Adam        Product 1  JANUARY     1
Adam        Product 1  FEBRUARY    0
Adam        Product 1  MARCH       0
Adam        Product 2  FEBRUARY    1
Adam        Product 2  JANUARY     0
Adam        Product 2  MARCH       0
Adam        Product 3  FEBRUARY    0
Adam        Product 3  JANUARY     0
Adam        Product 3  MARCH       1
Jones       Product 1  FEBRUARY    1
Jones       Product 1  JANUARY     0
Jones       Product 1  MARCH       1
Kanes       Product 1  MARCH       1
Kanes       Product 1  JANUARY     0
Kanes       Product 1  FEBRUARY    0
Kanes       Product 2  FEBRUARY    0
Kanes       Product 2  JANUARY     1
Kanes       Product 2  MARCH       0
Kanes       Product 3  MARCH       0
Kanes       Product 3  FEBRUARY    1
Kanes       Product 3  JANUARY     0

With the result above, we want to sort the month column in the order. How can we do that?
We use ORDER BY clause and DECODE function:
1
2
3
4
5
6
7
SELECT   *
    FROM sales_month UNPIVOT (yes
                     FOR month
                     IN ("JANUARY", "FEBRUARY", "MARCH"))
ORDER BY customer_id,
         product_id,
         DECODE (month,  'JANUARY', 1,  'FEBRUARY', 2,  'MARCH', 3);

And here:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
CUSTOMER_ID PRODUCT_ID MONTH    YES  
----------- ---------- -------- ---- 
Adam        Product 1  JANUARY     1
Adam        Product 1  FEBRUARY    0
Adam        Product 1  MARCH       0
Adam        Product 2  JANUARY     0
Adam        Product 2  FEBRUARY    1
Adam        Product 2  MARCH       0
Adam        Product 3  JANUARY     0
Adam        Product 3  FEBRUARY    0
Adam        Product 3  MARCH       1
Jones       Product 1  JANUARY     0
Jones       Product 1  FEBRUARY    1
Jones       Product 1  MARCH       1
Kanes       Product 1  JANUARY     0
Kanes       Product 1  FEBRUARY    0
Kanes       Product 1  MARCH       1
Kanes       Product 2  JANUARY     1
Kanes       Product 2  FEBRUARY    0
Kanes       Product 2  MARCH       0
Kanes       Product 3  JANUARY     0
Kanes       Product 3  FEBRUARY    1
Kanes       Product 3  MARCH       0