Box classes in Hadoop

Introduction:

In this article we learn what is Box classes in hadoop. Hadoop framework is developed in Java programming. It mainly uses collection framework of Java. Collection framework works only on objects and hence wrapper classes are invented. Before Java 1.5 we required utility classes(parseInt, valueOf-Methods) to convert primitive type to objective type and vice-versa. Primitive type to Objective type
conversion is called AutoBoxing, and Objective type to Primitive type is called UnBoxing. Autoboxing and Unboxing features were added in Java5.

Box Classes:

In Hadoop there is no direct support for automatic conversion from primitive type to Box classes and vice-versa. So it requires specific methods of conversion.
Following are data types in Primitive, Wrapper and Box class:

Wrapper Class Primitive Type Box Class
 Integer  int  IntWritable
 Long  long  LongWritable
 Float  float  FloatWritable
 Double  double  DoubleWritable
 String  string  Text
 Character  char  Text

Methods for conversion:

  1. int to IntWritable conversion new IntWritable(int) method.
  2. IntWritable to int conversion get() method.
  3. long to LongWritable conversion new LongWritable(long) method.
  4. LongWritable to long conversion get() method.
  5. float to FloatWritable conversion new FloatWritable(float) method.
  6. FloatWritable to float conversion get() method. 
  7. double to DoubleWritable conversion new DoubleWritable(double) method.
  8. DoubleWritable to double conversion get() method.
  9. String to Text conversion new Text(string) method.
  10. Text to String conversion toString() method.
Hopes you are clear with box classes in Hadoop. That's all in this article. This article will help you for writing MapReduce programs. To learn how to use this box classes for writing code refer this link WordCount in Hadoop.

0 comments:

Post a Comment