Showing posts with label Interview FAQ's. Show all posts
Showing posts with label Interview FAQ's. Show all posts

Sunday, 24 April 2016

HDFS Commands Reference

In this article, the basic syntax of hadoop file system i.e HDFS has been explained with examples and screen shot. This is very useful for the beginners who are interested to explore the big data world and HDFS is the gate to that world.

Hadoop is open source software [It is a java frame work] which runs on a cluster of commodity hardware machines. It provides both storage [HDFS] and processing [MAP REDUCE] in distributed manner. It has capable of processing huge volume of data that is ranging from Giga bytes to Peta bytes.

HDFS Commands

hdfs dfs

































hadood fs:

































hdfs dfs/hadoop fs

1.  Creating a directory [HDFS]

Syntax:

- hdfs dfs –mkdir <Directory name along with path details >

Example:

- hdfs dfs –mkdir /user/root/hadoop_mahendhar

Screenshot



2.  Listing the contains of the hadoop directory

Syntax:

- hdfs dfs –ls < argument like absolute path of the file >

Example:

- hdfs  dfs –ls /user/root/hadoop_mahendhar

Screen Shot:






3.  Create a file in local file system and put the file in HDFS

Create a file in local system by  vi <file_name>, add some texts and save, exit.

Syntax: vi First_hadoop.txt

Putting the normal file to hadoop file system

Syntax:
- hdfs dfs –put <Local file path with file name > <hadoop destination path with file name >

Example:
- hdfs dfs –put  First_hadoop.txt  /user/root/hadoop_mahendhar

Screen Shot:








4. Moving a normal file to hadoop file system 

Syntax: 

- hdfs dfs – moveFromLocal <Local file path with file name > <hadoop destination path with file name > 

Example:

- hdfs dfs  –moveFromLocal  /root/Second_hadoop.txt  /user/root/hadoop_mahendhar

Screenshot:








Note:

1. Before executing the above command, ensure that the second_hadoop.txt file is created in the Local normal file system. 

2. This operation will move the local file, so there is no local copy of the file exist after this operation. 

5. For listing all directories and sub-directories recursively 

Syntax:

- hdfs dfs –lsr <hadoop directory>

Example:

- hdfs  dfs -lsr /user/root/hadoop_mahendhar/ 

- Note: Create more directory and sub directory to validate this command correctly. 

6. Check the size of the file in HDFS 

Syntax: 

- hdfs dfs – du <File path with file name > 

Example:

- hdfs  dfs – du /user/root/hadoop_mahendhar/  
Screen Shot:







7. Download a file from HDFS to normal file system 

Syntax: 

- hdfs dfs – get <hadoop file path details with file name > < local file path details with file name> 

Example:

- hdfs dfs – get /user/root/hadoop_mahendhar/Second_hadoop.txt /root/local_files/ 

Screen Shot:






8. Getting a directory of files from HDFS and merge into a single file in normal file system 

Syntax: 

- hdfs dfs – getmerge <HDFS file directory > < Local file path with file details > < add new line> 

Example:

- hdfs dfs – getmerge /user/root/hadoop_mahendhar/ /root/local_files/hadoop_merge_file.txt 

Screen Shot:






Note:

1. The add newline is optional and it will just add a new line at the end of the each file. 

2. Before this make sure you have created 2-3 files in HDFS so that you can check and validate the file contain with normal file w.r.t size 

9. Copying data from one node to another node in HDFS 

Syntax:

- hdfs dfs – distcp <node1 file path details > <node 2 file path details > 

10. Display the contain of the data 

Syntax: 

- hdfs dfs – cat < File path details with file name > 

Example:

- hdfs  dfs –cat /user/root/hadoop_mahendhar/ First_hadoop.txt 

Screen Shot:




11. Change the group, owner, permission of the file or directory 

Syntax: 

- hdfs dfs –chgrp [-R] <New group Name > <File or directory > 

- hdfs dfs –chmod [ -R] <Fileor directory> 

- hdfs dfs –chown <New Owner name> < File or directory> 

12. Copying and Moving files within HDFS 

Syntax:

- hdfs dfs –cp <First file path details > < Destination file details >

- hdfs dfs –mv <First file path details > < Destination file details > 

13. Empty the hadoop thrash 

Syntax: 

- hdfs dfs –expunge 

Screen Shot:




Apache Hadoop Terms/Abbreviations click here 

Thursday, 21 April 2016

How to use unix tool AWK

This artical will help you understand how to work with the AWK utility in UNIX. It also gives
the meaning of some of the AWK Built-in Variables


These few AWK one liners give very basic and random examples which will help to understand basic about this UNIX tool.

Meaning of some of the Awk Built-in Variables used below:

NF               : Number of fields in current line/record

NR               : Ordial number of current line/record
FS                : Field Separator (Also -F can be used)
OFS             : Output Field Separator (default=blank)

FILENAME : Name of current input file

All of following Awk one liner examples is based on the input file 'test1.txt’

$ cat test1.txt                                                               
Continent:Val
AS:12000
AF:9800                                                               
AS:12300
NA:3400
OC:12000
AF:500
AS:1000

Know more bout UNIX FILE PERMISSIONS   Click here


Scenario
Print 'line number' NR and 'Number of fields' NF for each line
Command
awk -F ":" '{print NR,NF}' test1.txt
Output
1 2

2 2

3 2

4 2

5 2

6 2

7 2

8 2


Scenario
Print first field, colon delimited
Command
awk -F ":" '{print $1}' test1.txt

Continent

AS

AF

AS

NA

OC

AF

AS


Scenario
Print first field, colon delimited, but excluding the 'first line' (NR!=1)
Command
awk -F ":" 'NR!=1 {print $1}' test1.txt
Output
AS

AF

AS









NA

OC

AF

AS


Scenario
Print first field, colon delimited, but only for line number 1 (NR==1)
Command
awk -F ":" 'NR==1 {print $1}' test1.txt
Output
Continent


Scenario
Print first and second field, colon delimited, but excluding the 'first line' (NR!=1)
Command
awk -F ":" 'NR!=1 {print $1,$2}' test1.txt
Output
AS 12000

AF 9800

AS 12300

NA 3400



OC 12000

AF 500

AS 1000


Scenario
Setting output field separator as pipe
Command
awk -F ":" 'BEGIN{OFS="|"} NR!=1 {print $1,$2}' test1.txt
Output
AS|12000

AF|9800

AS|12300

NA|3400

OC|12000

AF|500

AS|1000


Scenario
Anything on BEGIN executes first
Command
awk 'BEGIN{FS=":"; OFS="|"; print "Con|SomeVal"} NR!=1 {print $1,$2}' test1.txt
Output
Con|SomeVal

AS|12000

AF|9800

AS|12300

NA|3400

OC|12000

AF|500

AS|1000


Scenario
Printing FILENAME, will be printed for all the lines
Command
awk -F ":" '{print FILENAME}' test1.txt
Output
test1.txt

test1.txt

test1.txt

test1.txt

test1.txt

test1.txt

test1.txt

test1.txt



Scenario
Printing FILENAME, but printing only last instance using END clause
Command
awk -F ":" ' END {print FILENAME}' test1.txt
Output
test1.txt


Scenario
Printing the last field of the file, same as printing $2 as there are only 2 fields
Command
awk -F ":" '{print $NF}' test1.txt
Output
Val

12000

9800

12300

3400

12000

500

1000


Scenario
Matching, printing lines begin with "AS"
Command
awk -F ":" '/^AS/' test1.txt
Output
AS:12000

AS:12300

AS:1000


Scenario
Matching, printing lines not begining with "AS"
Command
awk -F ":" '!/^AS/' test1.txt
Output
Continent:Val

AF:9800

NA:3400

OC:12000

AF:500


Scenario
Direct matching, first field as "AS"
Command
awk -F ":" '$1=="AS"' test1.txt
Output
AS:12000

AS:12300

AS:1000


Scenario
Direct matching, first field as "AS", Print 2nd Column
Command
awk -F ":" '$1=="AS" {print $2}' test1.txt
Output
12000

12300

1000


Scenario
$0 prints the full line, same as {print}
Command 1
awk -F ":" '$1=="AS" {print $0}' test1.txt
Output
AS:12000

AS:12300

AS:1000





Command 2
awk -F ":" '$1=="AS" {print}' test1.txt
Output
AS:12000

AS:12300

AS:1000


Scenario
'Or' and 'AND' together
Command
awk -F ":" '($1=="AS" || $1=="OC") && $NF > 11000 {print}' test1.txt
Output
AS:12000

AS:12300

OC:12000


Scenario
Partial Matching
Command
awk -F ":" '$1 ~ /A/ {print}' test1.txt
Output
AS:12000

AF:9800

AS:12300

NA:3400

AF:500

AS:1000


Scenario
Reading from STDOUT
Command
cat test1.txt | awk -F ":" '!/Continent/ {print $1}' | sort | uniq
Output
AF

AS

NA

OC


Scenario
Add value 1000 to the 2nd field, where first field is "AF" and then print the output file
Command
awk -F ":" '$1=="AF" {$2+=1000} {print}' test1.txt
Output
Continent:Val

AS:12000

AF 10800

AS:12300

NA:3400

OC:12000

AF 1500

AS:1000


Scenario
Sum of 2nd fields, exclude first line
Command
awk -F ":" 'NR!=1 {sum+=$NF} END {print sum}' test1.txt
Output
51000


Scenario
Set 2nd value as 0 where first field is "AS"
Command
awk -F ":" 'BEGIN {OFS=":"} $1=="AS" {$2=0} {print}' test1.txt
Output
Continent:Val

AS:0

AF:9800

AS:0

NA:3400