17.
MapReduce for Addition of Two Numbers
While this might seem trivial for a MapReduce job, here's a basic implementation:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class AddTwoNumbers {
public static class Map extends Mapper<Object, Text, IntWritable,
IntWritable> {
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
String[] numbers = [Link]().split(",");
int num1 = [Link](numbers[0]);
int num2 = [Link](numbers[1]);
[Link](new IntWritable(1), new IntWritable(num1 +
num2));
}
}
public static class Reduce extends Reducer<IntWritable,
IntWritable, IntWritable, IntWritable> {
public void reduce(IntWritable key, Iterable<IntWritable>
values, Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += [Link]();
}
[Link](key, new IntWritable(sum));
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = [Link](conf, "add two numbers");
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link](job, new Path(args[0]));
[Link](job, new Path(args[1]));
[Link]([Link](true) ? 0 : 1);
}
}
18. Thrift and HBase
1. Install Thrift: Follow the instructions for your specific operating system and programming
language.
2. Generate HBase Thrift Bindings: Use the thrift command-line tool to generate the
necessary code.
3. Interact with HBase: Use the generated code to create HBase clients and perform
operations like creating tables, putting and getting data.
19. HQL Commands
-- Create a table
CREATE TABLE bankaccount (
cid INT,
cname STRING,
cage INT,
acctype STRING
);
-- Load data (assuming a CSV file)
LOAD DATA LOCAL INPATH '/path/to/your/[Link]' INTO TABLE
bankaccount;
-- Display table content
SELECT * FROM bankaccount;
20. Hadoop Command for Deleting Files
hadoop fs -rm -r <hdfs_directory>
Note:
● The MapReduce job for addition is a simplified example. In real-world scenarios, you
would likely use a more efficient approach.
● For more complex HBase interactions, consider using the HBase Java API or other
higher-level libraries.
● Always handle exceptions and error conditions in your MapReduce and HBase code.
● Consider using HBase's built-in mechanisms for data loading and querying, as they can
be more efficient than using MapReduce.
● [Link]
● [Link]
● [Link]
● [Link]
● [Link]
● [Link]