Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

Java interfaces to HDFS

Java interfaces to HDFS


 Computer Interface is known as a boundary which separates two
or more systems.
 It exchanges data between the components in a system which
could signals, commands or protocols.
 Java Abstraction provides the functionality of a particular method
by hiding the implementation logic written inside the method.
Similarly, Java Interface is also an Abstract Class which includes
the Method Declaration but not its definition.
 A class implements an Interface to inherit the abstract methods.
Along with abstract methods, an interface can also
include constants, static methods, nested interfaces and default
methods.
Conti…
 An interface is completely similar to a normal class in Java. An interface
includes the abstract methods and these methods are designed to be
implemented. This process is merely equal to inheritance, which is
normal when it comes to classes. We will discuss more on similarities.
 Same as a class, an interface can also contain as many numbers of
methods as required.
 Similar to classes, the interface is also written with a .java extension file.
 Surprisingly, the bytecode for an interface will have appeared in
a .class file.
 An interface is shown in the form of a package and their respective
bytecode is in a directory that matches with the package name.
Conti… (Need an Interface)
Java does not support Multiple Inheritances, due to
which, It will not allow classes to extend more than one
class at an instance.
Child classes could not inherit the properties of multiple
parent classes at a single instance, as it results in Diamond
Problem.
To overcome this issue, Interface is introduced.
Conti… (Need an Interface)
Let us assume we have two planes, one can carry
only passengers, the other one only cargo.
Now, we need to inherit the properties of both the cargo
plane and the passenger plane.
Java would not support this solution as it ends up
in ambiguity between the two planes.
Conti… (Need an Interface)
But, if you can make it possible by making Java feel that it
is inheriting one plane and implementing the methods
present in the other plane.
It is like a commercial Airplane which takes both
passengers and cargo luggage.
The Interface is like making a bigger plane which could
do both the tasks without interfering with the
components of one another, instead just borrowing the
methods from the Interface Class.
Conti… (Need an Interface)
Conti… (Need an Interface: Example)
//Class A Code
1package multiple;
2class A{
3 void msg(){System.out.println("Hello");}
4}

//Class B Code
1package multiple;
2class B{
3 void msg(){System.out.println("Welcome");}
4}
// Class C Code

package multiple;
class C extends A,B{ // This will not be accepted by Java, It will throw an error and
1
code will not be executed.
2
public static void main(String args[]){
3
C obj=new C();
4
}
}
Conti…
Output:
Error. This particular approach throws an exception.
Java do not support Multiple inheritances. This error is
known as Diamond Problem

Let us try a solution by using an interface, Child


Classes can access the methods from Multiple
Parent classes at a single instance.
Conti…
//Interface Code
1package MIS;
2public interface solution {
3public void Hello();
4public void Welcome();
5}

//Class Code
1package MIS;
2public class classA implements solution {
3 public void Hello() {
4 java.lang.System.out.println("Hello world");
5 }
6 public void Welcome() {
7 java.lang.System.out.println("Welcome to ABES");
8}
9public static void main(String[] args) {
10 classA Edureka = new classA();
11 Edureka.Hello();
12 Edureka.Welcome();
13 }
14}
Conti…
Output:
Hello World
Welcome to ABES

Declaring a Java Interface: Syntax


1interface interface_name {
2// declare constant fields;
3// declare methods();
4// default methods;
5}
Conti…
Nesting the Java Interface
Interface Nesting is a process of declaring an Interface
inside another Existing Interface or declaring an
Interface inside a Class. The Nested Interface is also
known as an Inner Interface.
The Nested Interface cannot be accessed directly.
Conti…(Java code for writing file in HDFS)
 FileSystem fileSystem = FileSystem.get(conf);

// Check if the file already exists


Path path = new Path("/path/to/file.ext");
if (fileSystem.exists(path)) {
System.out.println("File " + dest + " already exists");
return;
}

// Create a new file and write data to it.


FSDataOutputStream out = fileSystem.create(path);
InputStream in = new BufferedInputStream(new FileInputStream(new File(source)));
byte[] b = new byte[1024];
int numBytes = 0;
while ((numBytes = in.read(b)) > 0) {
out.write(b, 0, numBytes);
}

// Close all the file descripters


in.close();
out.close();
Conti…(Java code for reading file in HDFS)
FileSystem fileSystem = FileSystem.get(conf);
Path path = new Path("/path/to/file.ext");
if (!fileSystem.exists(path)) {
System.out.println("File does not exists");
return;
}
FSDataInputStream in = fileSystem.open(path);
int numBytes = 0;
while ((numBytes = in.read(b))> 0) {
System.out.prinln((char)numBytes));
// code to manipulate the data which is read
}
in.close();
out.close();
fileSystem.close();
THANK
YOU

You might also like