0% found this document useful (0 votes)
0 views7 pages

Java Code

The document contains Java code snippets for creating and managing an HBase table, inserting data into it, and performing character counting and even-odd summation using Hadoop MapReduce. It includes classes for creating a table with column families, inserting data, and implementing MapReduce jobs for counting characters and summing even and odd numbers. Each section of code is structured with appropriate imports, class definitions, and methods for functionality.

Uploaded by

LET'S LEARN
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views7 pages

Java Code

The document contains Java code snippets for creating and managing an HBase table, inserting data into it, and performing character counting and even-odd summation using Hadoop MapReduce. It includes classes for creating a table with column families, inserting data, and implementing MapReduce jobs for counting characters and summing even and odd numbers. Each section of code is structured with appropriate imports, class definitions, and methods for functionality.

Uploaded by

LET'S LEARN
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Code for Creating table:

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link].*;

import [Link];

public class createTable{

public static void main(String[] args) throws IOException{

Configuration conf = [Link]();

Connection connection = [Link](conf);

Admin admin = [Link]();

HTableDescriptor tableName = new


HTableDescriptor([Link]("secondtable"));

[Link](new HColumnDescriptor("colfam1"));

[Link](new HColumnDescriptor("colfam2"));

if(![Link]([Link]()))

[Link]("Creating table");

[Link](tableName);

[Link]("Done");

}
Java Code for inserting data:

import [Link];

import [Link];

import [Link];

import [Link].*;

import [Link];

import [Link];

public class tab2{

public static void main(String[] args) throws IOException {

Configuration conf=[Link]();

Connection connection = [Link](conf);

Table table = [Link]([Link]("secondtable"));

Put put = new Put([Link]("1"));

[Link]([Link]("colfam1"),
[Link]("Channel"),[Link]("ABC"));

[Link]([Link]("colfam1"),
[Link]("Creator"),[Link]("Sam"));

[Link]([Link]("colfam1"),
[Link]("Country"),[Link]("USA"));

[Link](put);

}
Word Count:

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

public class CharCount {

// Mapper Class

public static class CharCountMapper extends Mapper<Object, Text, Text, IntWritable> {

private final static IntWritable one = new IntWritable(1);

private Text character = new Text();

public void map(Object key, Text value, Context context) throws IOException,
InterruptedException {

String line = [Link]().toLowerCase();

for (char c : [Link]()) {

if ([Link](c)) {

[Link]([Link](c));
[Link](character, one);

// Reducer Class

public static class CharCountReducer extends Reducer<Text, IntWritable, Text,


IntWritable> {

public void reduce(Text key, Iterable<IntWritable> values, Context context) throws


IOException, InterruptedException {

int sum = 0;

for (IntWritable val : values) {

sum += [Link]();

[Link](key, new IntWritable(sum));

// Driver Class

public static void main(String[] args) throws Exception {

Configuration conf = new Configuration();

Job job = [Link](conf, "Character Count");

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

EVEN ODD SUM:

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

public class EvenOddSum {

public static class EvenOddMapper extends Mapper<Object, Text, Text, IntWritable> {

private final static IntWritable number = new IntWritable();

private final static Text EVEN = new Text("Even");

private final static Text ODD = new Text("Odd");


public void map(Object key, Text value, Context context) throws IOException,
InterruptedException {

String line = [Link]();

for (String numStr : [Link](" ")) {

try {

int num = [Link](numStr);

[Link](num);

if (num % 2 == 0) {

[Link](EVEN, number);

} else {

[Link](ODD, number);

} catch (NumberFormatException e) {

// Ignore non-integer values

public static class EvenOddReducer extends Reducer<Text, IntWritable, Text,


IntWritable> {

public void reduce(Text 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, "sum even odd");

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

You might also like