key的自定义类型

根据例【5-13】,设计一个可用于key的自定义类型。要求以时间戳为参照进行比较。

在这里插入图片描述
原理

除了Writable接口的readFields()和write()方法之外,WritableComparable接口还引入了compareTo()方法。compareTo()方法的返回值有三种类型:负整数,0或正整数,分别表示当前对象小于,等于或大于被比较对象。在compareTo实现中,是将title、 timestamp和content三者封装在一起,通过比较当前timestamp和MyKeyWritableComparable中的timestamp,来输出值。

1) Package hadoop.pojo;

Package hadoop.pojo;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException
import org.apache.hadoop.io.WritableComparable;

 

public class MyKeyWritableComparable implements WritableComparable<MyKeyWritableComparable> {
private String title;
private long timestamp;
private String content;

@Override
public void write(DataOutput out) throws IOException{
                   out.writeUTF(title);
                   out.writeLong(timestamp);
                   out.writeUTF(content);
         }

Override
public void readFields(DataInput in) throws IOException{
                   title= in.readUTF();
                   timestamp= in.readLong();
                   content= in.readUTF();
         }

@Override
public int compareTo(MyKeyWritableComparable mk) {
                   long thisValue = this.timestamp;
                   long thatValue = mk.timestamp;
                   return (thatValue < thatValue ? -1 : (thisValue == thatValue ? 0 : 1));
         }         
         
public String getTitle() {
                   returntitle;
         }

public void setTitle(String title) {
                   this.title= title;
         }

public long getTimestamp() {
                   return timestamp;
         }

public void setTimestamp(long timestamp) {
                   this.timestamp= timestamp;
         }

 public String getContent() {
                   return content;
         }

public void setContent(String content) {
                   this.content= content;
         }

Override
public String toString() {
                   return"MyKeyWritableComparable [title=" + title + ", 	  timestamp="+ timestamp + ", content=" + content + "]";
         }

(2) Package hadoop.services

Package hadoop.services

import 


java.io.DataOutput;   
import java.io.DataInput;
import java.io.IOException;
import org.apache.hadoop.classification.InterfaceAudience;   
import org.apache.hadoop.classification.InterfaceStability;

@InterfaceAudience.Public 
@InterfaceStability.Stable
 
public interface Writable {   
	void write(DataOutput out) throws IOException;    
	void readFields(DataInput in) throws IOException;
  }


package hadoop.services;
   
import java.util.*;

public interface Comparable<T>{
  	public int compareTo(T mk);
  }

(3)Package hadoop.Test

package hadoop.test;

import hadoop.pojo.MyKeyWritableComparable;

public class ComparaTest { 
         /**
          * @param args
public static void main(String[] args) {
                   MyKeyWritableComparable w = new MyKeyWritableComparable();
                   w.setTitle("dashuju");
                   w.setTimestamp(2020-04-13);
                   w.setContent("2017764439");
                   System.out.println(w);
         }
}

(4)结果截图
在这里插入图片描述

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读