Wednesday, 19 November 2014

Scanner Class tutorial with examples in java



Basics Usage of  Scanner class in java. 

Scanner class is one of basic default classes provided by java language as part of JDK installation. Scanner class is located in java.util package Java.util.Scanner class is one way to take the input from keyboard in java. Scanner class read the input in the form of java primitive data types like Integer and String. Internally Scanner class in java uses regular expression to parse the input using default space delimiter.Scanner class introduce in java 1.5

Java.util.Scanner class declaration.
Scannar.java class in JDK as following declaration

public final class Scanner
extends Object
implements Iterator, Closeable

Scanner class implements Iterator and closeable interfaces.

Iterator class used to iterate scanned primititve types, Closeable is used to release the resources once scanner read the source.

Usage of Scanner class in java.
import java.util.Scanner;

public class ScannerExample {
      public static void main(String args[]) {
            Scanner readInput = new Scanner(System.in);
            String line = readInput.nextLine();
            System.out.println("Scanner Console Example= " + line);
      }
}
 And out put  is Scanner Console Example= Scanner Usage example if Scanner Usage example is provided as intput

Various Scanner examples in java 1.5

We can use java.util.scanner class is used to read data from various source
  
How to read the input data from console using Scanner class


Scanner API in java provides various constructor for reading various srouces like File, InputStream, java.nio.file.Path, java.nio.channels.ReadableByteChannel

System.in provides the reading capabilities to read from console. This option provides capabilities to read data from console based on input data

How to read the input data from File stream using Scanner class in java


Scanner constructor has as file reader reference to read input data  from files.
Scanner fileScannerRead = new Scanner (new FileReader(new File("FILE_PATH")));


How to Fix NoSuchElementException using Scanner class 

NoSuchElementException exception is RuntimeException occurred when there is no element to scan while using Scaner class in java

NoSuchElementException occurs when code has Scanner.next() method

To fix this, code needs to check whether next element is available or not.
            Scanner readInput = new Scanner(System.in);
if (readInput.hasNext()) {
                  Object obj = readInput.next();
            }

nextInt(),nextLong() methods also throws NoSuchElementException . To fix, Code has to use corresponding hasNextInt(),hasNextLong() methods.

Fixing Exception in thread "main" java.util.InputMismatchException

The following example expects the input as number from console
Public static void main(String args[]) {
            Scanner readInput = new Scanner(System.in);
        System.out.print("Enter input number : ");
        int inputNo = readInput.nextInt();


Above program prints read the integer number when integer number is provided.

InputMismatchException  exception throws when float number is provided as input.  Please make sure that you enter the correct input data and handle the correct method to read data.

java.util.Scanner class advantages:-

  • Simplifies the text reading capabilities using scanner class
  • Scanner has default regular expression for primitive data types and String. So Regular expression compile is not required and hence improved the performance


java.util.Scanner class Disadvantages

  • Scanner is not good for multithread applications for thread safety. Need to handle synchronization
  • Java Compilter encountered Scanner class during execution, compilter wait until d ata is entered by user.

Sunday, 16 November 2014

Install,setup cloudera hadoop on linux

Install cloudera hadoop on linux


This article talks about installing hadoop on single host machine

hadoop is framework for large amount of data processing paralleled

Hadoop implementation provided by different vendors like hortionworks and cloudera.

This article talks about install cloudera hadoop on single machine.

To setup cloudera hadoop, java is required.
if java is not already installed, install JDK 1.6, at least update 8

Please donwnload cloudera-testing.repo from http://archive.cloudera.com/redhat/cdh/ and copy it to /etc/yum.repos.d/ and make sure you update yum command


Please run below commands to install hadoop,hive and pig
$ yum install hadoop-0.20 -y
$ yum install hadoop-hive -y
$ yum install hadoop-pig -y

The above commands installs hadoop to /usr/lib/hadoop folder, hive installs to /usr/lib/hive, pig to /usr/lib/pig

please setup the environment variables as described below in .bash_rc file

$ \vi ~/.bashrc
export HADOOP_HOME=/usr/lib/hadoop
export HIVE_HOME=/usr/lib/hive
export PIG_HOME=/usr/lib/pig
export PATH=$HADOOP_HOME/bin:$PATH:$PIG_HOME/bin:$HIVE_HOME/bin
save it to .bashrc file

$ source ~/.bashrc

Open $HADOOP_HOME/conf/hadoop-env.sh. Add JAVA_HOME path. Ex:
export JAVA_HOME=/usr/java/jdk1.6.0_18

* Open $HADOOP_HOME/conf/core-site.xml. Add the Namenode server name or localhost and port for fs.default.name. Ex:

fs.default.name
hdfs://localhost:9000