最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解HDFS多文件Join操作的實例

 更新時間:2017年10月18日 10:30:53   作者:回首凡塵不做仙  
這篇文章主要介紹了詳解HDFS多文件Join操作的實例的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下

詳解HDFS多文件Join操作的實例

最近在做HDFS文件處理之時,遇到了多文件Join操作,其中包括:All Join以及常用的Left Join操作,

下面是個簡單的例子;采用兩個表來做left join其中數(shù)據(jù)結(jié)構(gòu)如下:

A 文件:

a|1b|2|c

B文件:

a|b|1|2|c

即:A文件中的第一、二列與B文件中的第一、三列對應(yīng);類似數(shù)據(jù)庫中Table的主鍵/外鍵

代碼如下:

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.contrib.utils.join.DataJoinMapperBase;
import org.apache.hadoop.contrib.utils.join.DataJoinReducerBase;
import org.apache.hadoop.contrib.utils.join.TaggedMapOutput;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapred.TextOutputFormat;
import org.apache.hadoop.util.ReflectionUtils;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;


import cn.eshore.traffic.hadoop.util.CommUtil;
import cn.eshore.traffic.hadoop.util.StringUtil;




/**
 * @ClassName: DataJoin
 * @Description: HDFS JOIN操作
 * @author hadoop
 * @date 2012-12-18 下午5:51:32
 */
public class InstallJoin extends Configured implements Tool {
private String static enSplitCode = "\\|";
private String static splitCode = "|";


// 自定義Reducer
public static class ReduceClass extends DataJoinReducerBase {


@Override
protected TaggedMapOutput combine(Object[] tags, Object[] values) {
String joinedStr = "";
//該段判斷用戶生成Left join限制【其中tags表示文件的路徑,install表示文件名稱前綴】
//去掉則為All Join
if (tags.length == 1 && tags[0].toString().contains("install")) {
return null;
}

Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i < values.length; i++) {
TaggedWritable tw = (TaggedWritable) values[i];
String line = ((Text) tw.getData()).toString();


String[] tokens = line.split(enSplitCode, 8);
String groupValue = tokens[6];

String type = tokens[7];

map.put(type, groupValue);
}

joinedStr += StringUtil.getCount(map.get("7"))+"|"+StringUtil.getCount(map.get("30"));
TaggedWritable retv = new TaggedWritable(new Text(joinedStr));
retv.setTag((Text) tags[0]);
return retv;
}
}


// 自定義Mapper
public static class MapClass extends DataJoinMapperBase {


//自定義Key【類似數(shù)據(jù)庫中的主鍵/外鍵】
@Override
protected Text generateGroupKey(TaggedMapOutput aRecord) {
String line = ((Text) aRecord.getData()).toString();
String[] tokens = line.split(CommUtil.enSplitCode);


String key = "";
String type = tokens[7];
//由于不同文件中的Key所在列有可能不同,所以需要動態(tài)生成Key,其中type為不同文件中的數(shù)據(jù)標識;如:A文件最后一列為a用于表示此數(shù)據(jù)為A文件數(shù)據(jù)
if ("7".equals(type)) {
key = tokens[0]+"|"+tokens[1];
}else if ("30".equals(type)) {
key = tokens[0]+"|"+tokens[2];
}
return new Text(key);
}


@Override
protected Text generateInputTag(String inputFile) {
return new Text(inputFile);
}


@Override
protected TaggedMapOutput generateTaggedMapOutput(Object value) {
TaggedWritable retv = new TaggedWritable((Text) value);
retv.setTag(this.inputTag);
return retv;
}


}


public static class TaggedWritable extends TaggedMapOutput {


private Writable data;


// 自定義
public TaggedWritable() {
this.tag = new Text("");
}


public TaggedWritable(Writable data) {
this.tag = new Text("");
this.data = data;
}


@Override
public Writable getData() {
return data;
}


@Override
public void write(DataOutput out) throws IOException {
this.tag.write(out);
out.writeUTF(this.data.getClass().getName());
this.data.write(out);
}


@Override
public void readFields(DataInput in) throws IOException {
this.tag.readFields(in);
String dataClz = in.readUTF();
if (this.data == null
|| !this.data.getClass().getName().equals(dataClz)) {
try {
this.data = (Writable) ReflectionUtils.newInstance(
Class.forName(dataClz), null);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
this.data.readFields(in);
}


}


/**
* job運行
*/
@Override
public int run(String[] paths) throws Exception {
int no = 0;
try {
Configuration conf = getConf();
JobConf job = new JobConf(conf, InstallJoin.class);
FileInputFormat.setInputPaths(job, new Path(paths[0]));
FileOutputFormat.setOutputPath(job, new Path(paths[1]));
job.setJobName("join_data_test");
job.setMapperClass(MapClass.class);
job.setReducerClass(ReduceClass.class);
job.setInputFormat(TextInputFormat.class);
job.setOutputFormat(TextOutputFormat.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(TaggedWritable.class);
job.set("mapred.textoutputformat.separator", CommUtil.splitCode);
JobClient.runJob(job);
no = 1;
} catch (Exception e) {
throw new Exception();
}
return no;
}


//測試
public static void main(String[] args) {
String[] paths = {
"hdfs://master...:9000/home/hadoop/traffic/join/newtype",
"hdfs://master...:9000/home/hadoop/traffic/join/newtype/output" }

int res = 0;
try {
res = ToolRunner.run(new Configuration(), new InstallJoin(), paths);
} catch (Exception e) {
e.printStackTrace();
}
System.exit(res);
}
}

如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • 最全LocalDateTime、LocalDate、Date、String相互轉(zhuǎn)化的方法

    最全LocalDateTime、LocalDate、Date、String相互轉(zhuǎn)化的方法

    大家在開發(fā)過程中必不可少的和日期打交道,對接別的系統(tǒng)時,時間日期格式不一致,每次都要轉(zhuǎn)化,本文為大家準備了最全的LocalDateTime、LocalDate、Date、String相互轉(zhuǎn)化方法,需要的可以參考一下
    2023-06-06
  • SpringBoot Redis批量存取數(shù)據(jù)的操作

    SpringBoot Redis批量存取數(shù)據(jù)的操作

    這篇文章主要介紹了SpringBoot Redis批量存取數(shù)據(jù)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 詳解Spring事務(wù)回滾和事務(wù)提交

    詳解Spring事務(wù)回滾和事務(wù)提交

    這篇文章主要介紹了詳解Spring事務(wù)回滾和事務(wù)提交的相關(guān)資料,幫助大家更好的理解和學習使用spring框架,感興趣的朋友可以了解下
    2021-03-03
  • Java Web程序中利用Spring框架返回JSON格式的日期

    Java Web程序中利用Spring框架返回JSON格式的日期

    這里我們來介紹一下Java Web程序中利用Spring框架返回JSON格式的日期的方法,前提注意使用@DatetimeFormat時要引入一個類庫joda-time-版本.jar,否則會無法訪問相應(yīng)路徑
    2016-05-05
  • MyBatisPlus批量添加的優(yōu)化與報錯解決

    MyBatisPlus批量添加的優(yōu)化與報錯解決

    MybatisPlus是一個高效的java持久層框架,它在Mybatis的基礎(chǔ)上增加了一些便捷的功能,提供了更加易用的API,可以大幅度提高開發(fā)效率,這篇文章主要給大家介紹了關(guān)于MyBatisPlus批量添加的優(yōu)化與報錯解決的相關(guān)資料,需要的朋友可以參考下
    2023-05-05
  • maven中snapshot相關(guān)jar無法拉取問題及解決方案(常用方案)

    maven中snapshot相關(guān)jar無法拉取問題及解決方案(常用方案)

    Maven中的SNAPSHOT版本是指正在開發(fā)中的版本,這些版本可能會頻繁地更新,在使用Maven構(gòu)建項目時,有時會遇到無法拉取SNAPSHOT相關(guān)jar的問題,下面給大家分享maven中snapshot相關(guān)jar無法拉取問題及解決方案,感興趣的朋友一起看看吧
    2024-06-06
  • SpringBoot3讀取配置文件application.properties屬性值的幾種方式

    SpringBoot3讀取配置文件application.properties屬性值的幾種方式

    這篇文章主要介紹了SpringBoot3讀取配置文件application.properties屬性值的幾種方式,文中通過代碼示例給大家講解的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2024-11-11
  • Mybatis之解決collection一對多問題(顯示的結(jié)果沒有整合到一起)

    Mybatis之解決collection一對多問題(顯示的結(jié)果沒有整合到一起)

    這篇文章主要介紹了Mybatis之解決collection一對多問題(顯示的結(jié)果沒有整合到一起),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • 淺談Java之終止繼承:Final類和Fianl方法

    淺談Java之終止繼承:Final類和Fianl方法

    這篇文章主要介紹了Java之終止繼承:Final類和Fianl方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-03-03
  • Java 配置log 將日志信息輸出到指定日志文件中

    Java 配置log 將日志信息輸出到指定日志文件中

    這篇文章主要介紹了Java 配置log 將日志信息輸出到指定日志文件中,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-09-09

最新評論

山东省| 久治县| 千阳县| 日照市| 苏尼特左旗| 兴仁县| 泾阳县| 沐川县| 抚州市| 屏东县| 弥勒县| 扎赉特旗| 眉山市| 乃东县| 屏山县| 杭锦后旗| 山阳县| 和田市| 门源| 左云县| 七台河市| 沂水县| 香河县| 张家川| 谢通门县| 新丰县| 岳阳县| 成武县| 商河县| 海晏县| 芒康县| 琼海市| 盘山县| 延庆县| 开江县| 新源县| 任丘市| 施甸县| 寿宁县| 曲水县| 台前县|