0% found this document useful (0 votes)
2 views16 pages

Hadoop Lab Manual

The document provides Java implementations for various data structures including Linked Lists, Stacks, Queues, Sets, and Maps, along with their functionalities and example outputs. It also details the setup and installation of Hadoop in three modes: Standalone, Pseudo Distributed, and Fully Distributed, including configuration steps and commands for managing Hadoop. Additionally, it outlines file management tasks in Hadoop, such as creating directories, uploading/downloading files, and viewing file contents.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views16 pages

Hadoop Lab Manual

The document provides Java implementations for various data structures including Linked Lists, Stacks, Queues, Sets, and Maps, along with their functionalities and example outputs. It also details the setup and installation of Hadoop in three modes: Standalone, Pseudo Distributed, and Fully Distributed, including configuration steps and commands for managing Hadoop. Additionally, it outlines file management tasks in Hadoop, such as creating directories, uploading/downloading files, and viewing file contents.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

1.

Implement the folowing data structures in java

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;
}
}

public static LinkedList insert(LinkedList list, int data)


{
Node new_node = new Node(data);
new_node.next = null;
if (list.head == null)
{
list.head = new_node;
}
else {
Node last = list.head;
while (last.next != null)
{
last = last.next;
}

}
return list;
}

// **************TRAVERSAL**************

public static void printList(LinkedList list)


{
Node currNode = list.head;
System.out.print("\nLinkedList: ");
while (currNode != null)
{
System.out.print(currNode.data + " ");

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**************

public static LinkedList deleteAtPosition(LinkedList list, int index)


{
Node currNode = list.head, prev = null;
if (index == 0 && currNode != null)
{
list.head = currNode.next;
System.out.println(index + " position element deleted");
return list;
}

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**************

public static void main(String[] args)


{
LinkedList list = new LinkedList();
list = insert(list, 1);
list = insert(list, 2);
list = insert(list, 3);
list = insert(list, 4);
list = insert(list, 5);
list = insert(list, 6);
list = insert(list, 7);
list = insert(list, 8);
printList(list);
deleteByKey(list, 1);
printList(list);
deleteByKey(list, 4);
printList(list);
deleteByKey(list, 10);
printList(list);
deleteAtPosition(list, 0);
printList(list);
deleteAtPosition(list, 2);

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);
}
}

static void stack_pop(Stack<Integer> stack)


{
System.out.println("Pop :");
for(int i = 0; i < 5; i++)
{
Integer y = (Integer) stack.pop();
System.out.println(y);
}
}

static void stack_peek(Stack<Integer> stack)


{
Integer element = (Integer) stack.peek();
System.out.println("Element on stack top : " + element);
}

static void stack_search(Stack<Integer> stack, int element)


{
Integer pos = (Integer) stack.search(element);

if(pos == -1)
System.out.println("Element not found");
else
System.out.println("Element is found at position " + pos);
}

public static void main (String[] args)


{
Stack<Integer> stack = new Stack<Integer>();
stack_push(stack);
stack_pop(stack);
stack_push(stack);
5
stack_peek(stack);
stack_search(stack, 2);
stack_search(stack, 6);
}
}

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;

public class QueueExample


{
public static void main(String[] args)
{
Queue<Integer> q = new LinkedList<>();
for (int i=0; i<5; i++)
q.add(i);
System.out.println("Elements of queue-"+q);
int removedele = q.remove();
System.out.println("removed element-" + removedele);
System.out.println(q);
int head = q.peek();
System.out.println("head of queue-" + head);

int size = q.size();


System.out.println("Size of queue-" + size);
}
}

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.

Installing Hadoop in Standalone Mode

Here we will discuss the installation of Hadoop 2.4.1 in standalone mode.


There are no daemons running and everything runs in a single JVM. Standalone mode is
suitable for running MapReduce programs during development, since it is easy to test and
debug them.

Setting Up Hadoop

You can set Hadoop environment variables by appending the following commands
to /etc/profile file.

$ sudo gedit /etc/profile

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

After typing save and exit etc file

Now execute etc file by using

$ . /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

Installing Hadoop in Pseudo Distributed Mode

To set up hadoop for pseudo distributed mode:

Set up the following XML files located at /home/jmj/hadoop/etc/hadoop/


1.core-site.x ml
2.hdfs-site.xml
3.mapred-site.xml
Setup core-site.xml
 Open core _site.xml from the given path
/home/jmj/hadoop/etc/hadoop/core-site.xml

Put the following lines in the xml file


<configuration>
<property>
<name>fs.defaultFS</name>
<value>hdfs://localhost:9000</value>
</property>
<property>
<name>hadoop.tmp.dir</name>
<value>/home/jmj/hadoop/hadoop_2.4.1/tempHadoop</value>
</property>
</configuration>

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

 Open mapred_site.xml from the given path


/home/jmj/hadoop/etc/ hadoop/mapred-site.xml

If the file not present copy the mapred_site.xml.template file and rename the copied file
as mapred-site.xml

Put the following lines in the xml file

<configuration>
<property>
<name>mapred.job.tracker</name>
<value>localhost:9001</value>
</property>
</configuration>

Setup yarn-site.xml

 Open yarn_site.xml from the given path


/home/jmj/hadoop/etc/ hadoop/yarn-site.xml

Put the following lines in the xml file

12
<configuration>
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
</configuration>

Setup hadoop-env.sh

 Open hadoop-env.sh from the given path


/home/jmj/hadoop/etc/ hadoop/ hadoop-env.sh

Export the JAVA_HOME environment variable in that file

Put the following lines in that file

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

$ hadoop namenode -format

Starting the hadoop file system

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

Open the browse and goto this link

http://localhost:50070/

3. Implement File Management tasks in Hadoop

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/

4. See contents of a file


This Hadoop fs shell command displays the contents of the filename on console or stdout.

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

6. Copy a file from/To Local file system to HDFS


copyFromLocal

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>

7. Move file from source to destination.

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

8. Remove a file or directory in HDFS.

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/

9. Display last few lines of a file.


Similar to tail command in Unix.

Command :
hadoop fs -tail <path[filename]>

Example:
hadoop fs -tail /user/saurzcode/dir1/abc.txt

10. Display the aggregate length of a file.


Command :
hadoop fs -du <path>

Example:
hadoop fs -du /user/saurzcode/dir1/abc.txt

16

You might also like