给大数据文件的每一行产生唯一的id

给大数据文件的每一行产生唯一的id

4个主要思路:

1 单线程处理

2 普通多线程

3 hive

4 Hadoop

 

搜到一些参考资料


《Hadoop实战》的笔记-2、Hadoop输入与输出

https://book.douban.com/annotation/17068812/

TextInputFormat:文件偏移量:整行数据

但是这个偏移量,貌似是在一个文件的偏移,而不是全局。

 

Generate Auto-increment Id in Map-reduceJob

http://shzhangji.com/blog/2013/10/31/generate-auto-increment-id-in-map-reduce-job/

 

Generate unique customer id / insert uniquerows in hive

http://stackoverflow.com/questions/26855003/generate-unique-customer-id-insert-unique-rows-in-hive

 

Need to add auto increment column in atable using hive

http://stackoverflow.com/questions/23082763/need-to-add-auto-increment-column-in-a-table-using-hive

 

 

https://hadooptutorial.info/writing-custom-udf-in-hive-auto-increment-column-hive/

Here make sure that addition of annotation@UDFType(stateful = true) is required otherwisecounter value will not get increment in the Hive column, it will just returnvalue 1 for all the rows but not the actual row number.

 

最后我采取了用hive写udf的方案。


package hive.udf;
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.hive.ql.udf.UDFType;

/**
 * UDFRowSequence.
 */
@Description(name = "row_sequence",
    value = "_FUNC_() - Returns a generated row sequence number starting from 1")
@UDFType(deterministic = false, stateful = true)//stateful参数是必要的
public class UDFRowSequence extends UDF
{
  private int result;

  public UDFRowSequence() {
    result=0;
  }

  public int evaluate() {
	  result++;
    return result;
  }
}

// End UDFRowSequence.java

 

本文作者:linger

本文链接:http://blog.csdn.net/lingerlanlan/article/details/46430747



  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hadoop是一个开源框架,它允许分布式存储和处理大数据集。Hadoop的数据清洗通常涉及到使用MapReduce编程模型,在Hadoop集群上运行任务以处理原始数据。以下是一个简化的Hadoop数据清洗的示例: 1. **Map阶段**: - 首先,将原始数据集输入到MapReduce程序中。 - 然后,Map函数读取输入数据,通常是从HDFS(Hadoop Distributed File System)中读取。 - 接着,Map函数对每一条记录进行分析,识别出需要清洗的数据。例如,可以去除空白行、删除不完整的记录、转换数据格式等。 - 最后,Map阶段输出键值对(key-value pairs),其中键通常是记录的标识符,值是清洗后的数据。 2. **Shuffle阶段**: - Hadoop框架自动对Map输出的键值对进行排序和分组,确保相同键的值被送到同一个Reduce任务。 3. **Reduce阶段**: - Reduce函数接收到一组具有相同键的值,然后对这些值进行合并处理,例如进行数据聚合、去重等操作。 - 最终,Reduce函数输出清洗后且可以进行后续分析的数据。 下面是一个简单的MapReduce伪代码示例,用于清洗包含日志文件的文本数据: ```java public static class TokenizerMapper extends Mapper<Object, Text, Text, NullWritable> { public void map(Object key, Text value, Context context) throws IOException, InterruptedException { // 指定分隔符,通常日志文件可以用空格、制表符或逗号等分隔 String[] tokens = value.toString().split(" "); // 假设我们关注的是日志文件中的用户ID String userID = tokens[0]; // 输出清洗后的数据,这里假设使用NullWritable作为值类型,因为只是单纯的清洗数据 context.write(new Text(userID), NullWritable.get()); } } public static class IntSumReducer extends Reducer<Text, NullWritable, Text, NullWritable> { public void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException { // 简单的去重操作,输出每个唯一的用户ID一次 context.write(key, NullWritable.get()); } } ``` 在这个示例中,Map阶段读取原始日志数据,提取用户ID,并输出清洗后的键值对。Reduce阶段接收到相同的用户ID,然后简单地输出每个唯一的用户ID一次,完成去重。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值