在两个文件上设置操作Diff

时间:2014-11-20 08:55:22

标签: algorithm hadoop set hive

我希望获得两个平面/ CSV文件源和目标的差异,它们具有相同的架构。

让我们说,

的Source.txt:

  

EmpId | RegionId |销售
001 | R01 | $ 10000
002 | R02 | $ 20000
  003 | R03 | $ 30000

target.txt:

  

EmpId | RegionId |销售
001 | R01 | $ 10000
002 | R02 | $ 10000
  004 | R04 | $ 40000

结果应该是:

  

EmpId1 | RegionId1 | Sales1 | EmpId2 | RegionId2 | Sales2 | Result_Status
  001 | R01 | $ 10000 | 001 | R01 | $ 10000 |匹配
  002 | R02 | $ 20000 | 002 | R02 | $ 10000 |无与伦比的
  003 | R03 | $ 30000 | NULL | NULL | NULL |不匹配的
  NULL | NULL | NULL | 004 | R04 | $ 40000 |无与伦比的

任何帮助都会受到欢迎!!

编辑:

如果给定2个文件的大小很大,这个问题可能看起来更简单,但我试图找到最好的方法,性能是这里的主要标准,技术可以是任何东西,甚至hadoop地图减少,我试过使用Hive但它有点慢。

1 个答案:

答案 0 :(得分:1)

这是一种解决它的map-reduce方法(在高级伪代码中):

map(source):
   for each line x|y|z:
     emitIntermediate(x,(1,y|z))
map(target):
   for each line x|y|z:
     emitIntermediate(x,(2,y|z))

//make sure each list is sorted/ sort it yourself 1 is before 2 if both exists.
reduce(x, list):
   if list.size() == 1:
      (idx,y|z) <- list.first() //this is the configuration of the element in the list
      if idx == 1:
            emit(x|y|z|NULL|NULL|NULL|unmatched)
      else:
            emit(NULL|NULL|NULL|x|y|z|unmatched)
   else:
       (1,y1|z1) <- list.first()
       (2,y2|z2) <- list.last()
       m = (y1|z1 matches y2|z2 ? "matched" : "unmatched")
       emit(x|y1|z2|x|y2|z2|m)

我们的想法是将reduce部分中的数据拆分为map阶段中的不同ID,并让reducers检查区域和销售是否匹配。

通过大型集群(以及分布式文件格式)实现它可以显着提高性能,因为map-reduce框架将工作分布在集群中。

例如,您可以将Hadoop用作实施框架。

相关问题