Hadoop Lab Manual
Hadoop Lab Manual
a) Linked lists
import java.io.*;
public class LinkedList
{
Node head;
static class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
}
return list;
}
// **************TRAVERSAL**************
1
currNode = currNode.next;
}
System.out.println("\n");
}
// **************DELETION BY KEY**************
public static LinkedList deleteByKey(LinkedList list, int key)
{
Node currNode = list.head, prev = null;
if (currNode != null && currNode.data == key)
{
list.head = currNode.next;
System.out.println(key + " found and deleted");
return list;
}
while (currNode != null && currNode.data != key)
{
prev = currNode;
currNode = currNode.next;
}
if (currNode != null)
{
prev.next = currNode.next;
System.out.println(key + " found and deleted");
}
if (currNode == null)
{
System.out.println(key + " not found");
}
return list;
}
// **************DELETION AT A POSITION**************
2
int counter = 0;
while (currNode != null)
{
if (counter == index) {
prev.next = currNode.next;
System.out.println(index + " position element deleted");
break;
}
else {
prev = currNode;
currNode = currNode.next;
counter++;
}
}
if (currNode == null)
{
System.out.println(index + " position element not found");
}
return list;
}
// **************MAIN METHOD**************
3
printList(list);
deleteAtPosition(list, 10);
printList(list);
}
}
Output:
LinkedList: 1 2 3 4 5 6 7 8
1 found and deleted
LinkedList: 2 3 4 5 6 7 8
4 found and deleted
LinkedList: 2 3 5 6 7 8
10 not found
LinkedList: 2 3 5 6 7 8
0 position element deleted
LinkedList: 3 5 6 7 8
2 position element deleted
LinkedList: 3 5 7 8
10 position element not found
LinkedList: 3 5 7 8
4
b) Stacks
import java.io.*;
import java.util.*;
class Test
{
static void stack_push(Stack<Integer> stack)
{
for(int i = 0; i < 5; i++)
{
stack.push(i);
}
}
if(pos == -1)
System.out.println("Element not found");
else
System.out.println("Element is found at position " + pos);
}
Output:
Pop :
4
3
2
1
0
Element on stack top : 4
Element is found at position 3
Element not found
6
c) Queue
import java.util.LinkedList;
import java.util.Queue;
Output:
Elements of queue-[0, 1, 2, 3, 4]
removed element-0
[1, 2, 3, 4]
head of queue-1
Size of queue-4
7
d) Set
import java.util.*;
public class Set_example
{
public static void main(String[] args)
{
Set<String> hash_Set = new HashSet<String>();
hash_Set.add("Geeks");
hash_Set.add("For");
hash_Set.add("Geeks");
hash_Set.add("Example");
hash_Set.add("Set");
System.out.print("Set output without the duplicates");
System.out.println(hash_Set);
System.out.print("Sorted Set after passing into TreeSet");
Set<String> tree_Set = new TreeSet<String>(hash_Set);
System.out.println(tree_Set);
}
}
Output:
Set output without the duplicates[Set, Example, Geeks, for]
Sorted Set after passing into TreeSet[Example, For, Geeks, Set]
8
e) Map
import java.util.*;
class HashMapDemo
{
public static void main(String args[])
{
Map< String,Integer> hm = new HashMap<String,Integer>();
hm.put("a", new Integer(100));
hm.put("b", new Integer(200));
hm.put("c", new Integer(300));
hm.put("d", new Integer(400));
Set< Map.Entry< String,Integer> > st = hm.entrySet();
for (Map.Entry< String,Integer> me:st)
{
System.out.print(me.getKey()+":");
System.out.println(me.getValue());
}
}
}
Output:
a:100
b:200
c:300
d:400
9
2. Perform setting up and installing Hadoop in its three operating modes:
Once you have downloaded Hadoop, you can operate your Hadoop cluster in one of the three
supported modes −
Local/Standalone Mode − After downloading Hadoop in your system, by default, it is
configured in a standalone mode and can be run as a single java process.
Pseudo Distributed Mode − It is a distributed simulation on single machine. Each
Hadoop daemon such as hdfs, yarn, MapReduce etc., will run as a separate java process.
This mode is useful for development.
Fully Distributed Mode − This mode is fully distributed with minimum two or more
machines as a cluster.
Setting Up Hadoop
You can set Hadoop environment variables by appending the following commands
to /etc/profile file.
then it will open an etc file and now add the lines at the end of file
export HADOOP_INSTALL=/home/jmj/hadoop
export PATH=$HADOOP_INSTALL/bin
$ . /etc/profile
Before proceeding further, you need to make sure that Hadoop is working fine. Just issue the
following command −
10
$ hadoop version
If everything is fine with your setup, then you should see the following result −
Hadoop 2.4.1
Subversion https://svn.apache.org/repos/asf/hadoop/common -r 1529768
Compiled by hortonmu on 2013-10-07T06:28Z
Compiled with protoc 2.5.0
From source with checksum 79e53ce7994d1628b240f09af91e1af4
It means your Hadoop's standalone mode setup is working fine. By default, Hadoop is
configured to run in a non-distributed mode on a single machine
Setup hdfs_site.xml
Open hdfs _site.xml from the given path
/home/jmj/hadoop/etc/ hadoop/hdfs_site.xml
11
Put the following lines in the xml file
<configuration>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
<property>
<name>dfs.name.dir</name>
<value>file:///home/hadoop/hadoopinfra/hdfs/namenode </value>
</property>
<property>
<name>dfs.data.dir</name>
<value>file:///home/hadoop/hadoopinfra/hdfs/datanode </value>
</property>
</configuration>
Setup mapred-site.xml
If the file not present copy the mapred_site.xml.template file and rename the copied file
as mapred-site.xml
<configuration>
<property>
<name>mapred.job.tracker</name>
<value>localhost:9001</value>
</property>
</configuration>
Setup yarn-site.xml
12
<configuration>
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
</configuration>
Setup hadoop-env.sh
export JAVA_HOME=/usr/local/java/jdk1.8.0_231
NameNode setup:
After setting up all the xml and sh files format the namenode to activate the setup
Now run the start-dfs.sh and start-yarn.sh scripts to start the hadoop file system
$ $HADOOP_INSTALL/sbin/start-dfs.sh
$ $HADOOP_INSTALL/sbin/start-yarn.sh
http://localhost:50070/
13
1. Create a directory in HDFS at given path(s).
This HDFS command takes path URI’s as an argument and creates directories.Creates
any parent directories in path that are missing (e.g., mkdir -p in Linux).
Command:
hadoop fs -mkdir <paths>
Example:
hadoop fs -mkdir /user/saurzcode/dir1 /user/saurzcode/dir2
2. List the contents of a directory.
This Hadoop HDFS ls command displays a list of the contents of a directory specified by
path provided by the user, showing the names, permissions, owner, size and modification
date for each entry.
Command :
hadoop fs -ls <args>
Example:
hadoop fs -ls /user/saurzcode
3. Upload and download a file in HDFS.
1. Upload:
This hadoop basic command copies the file or directory from the local file system to the
destination within the DFS.
hadoop fs -put:
Copy single src file, or multiple src files from local file system to the Hadoop data
file system
Command:
hadoop fs -put <localsrc> ... <HDFS_dest_Path>
Example:
hadoop fs -put /home/saurzcode/Samplefile.txt /user/saurzcode/dir3/
2. Download:
This HDFS fs command copies the file or directory in HDFS identified by the source to
the local file system path identified by local destination.
hadoop fs -get:
Copies/Downloads files to the local file system
Command:
hadoop fs -get <hdfs_src> <localdst>
Example:
hadoop fs -get /user/saurzcode/dir3/Samplefile.txt /home/
14
Command:
hadoop fs -cat <path[filename]>
Example:
hadoop fs -cat /user/saurzcode/dir1/abc.txt
4. Copy a file from source to destination:
This Hadoop File system shell command copies the file or directory identified by the
source to destination, within HDFS.
Command:
hadoop fs -cp <source> <dest>
Example:
hadoop fs -cp /user/saurzcode/dir1/abc.txt /user/saurzcode/dir2
Similar to put command, except that the source is restricted to a local file reference.
Command:
hadoop fs -copyFromLocal <localsrc> <dest>
Example:
hadoop fs -copyFromLocal /home/saurzcode/abc.txt /user/saurzcode/abc.txt
copyToLocal
Similar to get command, except that the destination is restricted to a local file reference.
Command:
hadoop fs –copyToLocal <src> <localdest>
This basic HDFS command moves the file or directory indicated by the source to
destination within HDFS.
Command :
hadoop fs -mv <src> <dest>
Example:
hadoop fs -mv /user/saurzcode/dir1/abc.txt /user/saurzcode/dir2
15
Remove files specified as argument. Deletes directory only when it is empty
Command :
hadoop fs -rm <arg>
Example:
hadoop fs -rm /user/saurzcode/dir1/abc.txt
Recursive version of delete.
Command :
hadoop fs -rmr <arg>
Example:
hadoop fs -rmr /user/saurzcode/
Command :
hadoop fs -tail <path[filename]>
Example:
hadoop fs -tail /user/saurzcode/dir1/abc.txt
Example:
hadoop fs -du /user/saurzcode/dir1/abc.txt
16