Hadoop HelloWorld Examples - 单表连接

2019-03-28 12:55|来源: 网络

应该是那本"Hadoop 实战"的第4个demo了,单表连接。给出一对对的children和parents的名字,然后输出所有的grandchildren和grandparents对。

相关阅读:

《Hadoop实战》中文版+英文文字版+源码【PDF】 http://www.linuxidc.com/Linux/2012-10/71901.htm

Hadoop: The Definitive Guide【PDF版】 http://www.linuxidc.com/Linux/2012-01/51182.htm

输入数据(第一列child,第二列 parent)

Tom Lucy
Tom Jack
Jone Lucy
Jone Jack
Lucy Mary
Lucy Ben
Jack Alice
Jack Jesse
Terry Alice
Terry Jesse
Philip Terry
Philip Alma
Mark Terry
Mark Alma

输出数据(第一列grandchild,第二列grandparents)

Tom    Jesse
Tom    Alice
Jone    Jesse
Jone    Alice
Jone    Ben
Jone    Mary
Tom    Ben
Tom    Mary
Philip    Alice
Philip    Jesse
Mark    Alice
Mark    Jesse

  不知到是"Hadoop 实战" 还是那个虾皮工作室写的解法,把这个问题说成是单表联接。个人觉得其实没有这么深奥,说白了非常的简单,map输入:如上所说,key是child's name, value是parent's name; map的输出:key是名字,value是“标识+名字”,其中标识'0'后面接他的child's name,'1'接他的parent's name。比如第一个记录Tom Lucy,那么key是"Tom", value是"1Lucy",还有key是lucy,value是"0Tom" 。 reducer的输入:对于每一个人名(key),它的一系列values是他的children或者parents name(通过标识来识别是children还是parents),这样子得到key的children集合和parents集合(说白了就是知道了key的所有children和parents的名字)。然后求这两个集合的乘积(或者称为笛卡尔积)即可。。

具体代码如下:

import java.util.*;
import java.io.*;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;


public class oneTableConnect {
 
 public static class tableMapper extends Mapper<Text, Text, Text, Text>
 {
  @Override
  public void map(Text key, Text value, Context context) throws IOException, InterruptedException
  {
   context.write(key, new Text( "1" + value.toString()));
   context.write(value, new Text("0" + key.toString()));
  }
 }
 
 
 public static class tableReducer extends Reducer<Text, Text, Text, Text>
 {
  @Override
  public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException
  {
   String[] gChildren = new String[10];
   String[] gParents = new String[10];
   int cNum = 0;
   int pNum = 0;
   for(Text val : values)
   {
    if(val.toString().charAt(0)  == '0')// the key's child.
    {
     gChildren[cNum++] = val.toString().substring(1);
    }
    else//the key's parent.
    {
     gParents[pNum++] = val.toString().substring(1);
    }
   }
   
   for(int i=0; i<cNum; i++)
    for(int j=0;j<pNum;j++)
    {
     context.write(new Text(gChildren[i]), new Text(gParents[j]));
    }
  } 
 }
 
 public static void main(String[] args) throws Exception
 {
  Configuration conf = new Configuration();
  conf.set("mapreduce.input.keyvaluelinerecordreader.key.value.separator", " ");
 
  Job job = new Job(conf, "tableConnect");
  job.setJarByClass(oneTableConnect.class);
 
  job.setMapperClass(tableMapper.class);
  job.setReducerClass(tableReducer.class);
 
  job.setOutputKeyClass(Text.class);
  job.setOutputValueClass(Text.class);
 
  job.setInputFormatClass(KeyValueTextInputFormat.class);
  job.setOutputFormatClass(TextOutputFormat.class);
 
  FileInputFormat.addInputPath(job, new Path(args[0]));
  FileOutputFormat.setOutputPath(job, new Path(args[1]));
 
  System.exit( job.waitForCompletion(true) ? 0 : 1);
 }
}

更多Hadoop相关信息见Hadoop 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=13

相关问答

更多
  • 我刚检查过teravalidate只适用于terasort。 没有标准作业来验证Sort的输出。 这就是TeraSort用于大多数基准测试工作的原因,因为它拥有TeraGen,TeraSort,TeraValidate。 I just checked teravalidate only works for terasort. There is no standard job to verify the output of Sort. Thats the reason TeraSort is used for ...
  • 我认为,问题是,54.235.101.85被认为是一个公共IP地址。 在所有节点中使用ifconfig获取IP地址列表并检查以10.xxx/172.xxx/192.xxx开头的IP如果找到,请相应地修改所有节点中的配置文件。 I think, the problem is, 54.235.101.85 is suppose to be a public IP address. Use ifconfig in all the nodes to get a list of IP address and chec ...
  • 最简单的答案是将项目转换为Maven并在POM中包含gson依赖项。 现在, mvn package获取所有必需的依赖项,并创建一个JAR文件,其中包含完成集群中作业所需的所有内容。 The easiest answer was to convert the project to Maven and include a gson dependency in the POM. Now mvn package picks up all the necessary dependencies and creates ...
  • 删除已存在的输出文件,或输出到不同的文件。 (我有点好奇你对错误信息的其他解释。) Delete the output file that already exists, or output to a different file. (I'm a little curious what other interpretations of the error message you considered.)
  • 这取决于很多因素,包括配置,机器,内存配置,JVM设置等。还需要减去JVM启动时间。 它对我来说运行速度要快得多。 也就是说,小数据集的速度当然比专门的C程序要慢 - 请考虑它在“幕后”做了什么。 尝试使用数千个数据分布在几千个文件中,并查看会发生什么。 This depends on a large number of factors, including your configuration, your machine, memory config, JVM settings, etc. You als ...
  • 在Hadoop中,您的数据绝对很小。 最新的电脑有16+ GB的RAM,因此您的数据集可以完全适合单台机器的内存。 但是,这并不意味着您至少可以尝试将数据加载到HDFS并对其执行一些操作。 Sqoop&Hive将成为您用来加载和处理SQL的工具。 但是,由于我提出了关于内存的观点,因此完全可行,您不需要Hadoop(HDFS和YARN),而是可以使用Apache Spark w / SparkSQL直接从分布式JDBC连接访问MySQL。 In Hadoop terms, your data is defi ...
  • 罐子的路径有错误。 我纠正了它。 There was a mistake in the path for the jar. I corrected it.
  • 错误消息表示虽然运行时能够找到类ProgramDriver ,但函数run()不存在。 最可能的原因是您正在运行旧版本的Hadoop,它暴露了ProgramDriver的差异界面。 大约一年前,这个方法在被调用driver()之后被重命名为run() driver() 。 解决方法是确保您运行的是最新版本的Hadoop。 The error message means that while the runtime was able to find the class ProgramDriver, the f ...
  • 这是3.10版本中已知的错误/回归: https : //bugzilla.xamarin.com/show_bug.cgi? id = 23553 这个Xamarin论坛帖子提供了一个修复版本: http : //forums.xamarin.com/discussion/27011/fix-for-gdipcreatefromcontext-macosx-and-other-macios-gdi-issues It's a known bug/regression in the 3.10 release ...
  • 问题是HelloWorld.j文件是假的。 请参阅此主题: http : //sourceforge.net/projects/jasmin/forums/forum/349052/topic/3330588 The problem is the HelloWorld.j file is bogus. See this thread: http://sourceforge.net/projects/jasmin/forums/forum/349052/topic/3330588