---
title: "mapreduce中用户自定义数据类型"
author: "sunasheng"
date: "2013-09-22T09:30:51Z"
tags: ["Hadoop", "Mapreduce"]
categories: ["互联网"]
slug: "mapreduce"
draft: false
lastmod: "2014-03-11T16:39:15Z"
id: 1944600
url: "https://oaksun.iteye.com/blog/1944600"
---

该博客已经完全转移到[http://sunhs.me](http://sunhs.me/ "http://sunhs.me")

 

中并增加更多新的技术内容（hadoop为

 

 

主），欢迎访问！

 

mapreduce中用户自定义数据类型

hadoop内置的数据类型：  
BooleanWritable  
ByteWritable  
DoubleWritable  
FloatWritable  
IntWritable  
LongWritable  
Text  
NullWritable  
自定义数据类型时第一个基本的要求是实现Writable接口，如果该数据要被作为主键key使用的话还必须实现WritableComparable接口

```
public class Point3D implemants WritableComparable<Point3D>{
	private float x,y,z;
	public float getX(){return x;}
	public float getY(){return y;}
	public float getZ(){return z;}

	public void readFields(DataInput in)throws IOException{
		x = in.readFloat();
		y = in.readFloat();
		z = in.readFloat();
	}
	public void write(DataOutput out)throws IOException{
		out.writeFloat(x);
		out.writeFloat(y);
		out.writeFloat(z);
	}
	public int compareTo(Point3D p){
		//具体实现比较当前的空间坐标点this(x,y,z)与指定的点p(x,y,z)的大小
		//并输出-1（小于），0（等于），1（大于）
	}
}
```