hadop 역 열 색인
                    ,             。           (   ),                   ,                  ,                   ,          ,         。   inversed.files  file1,file2,file3  ,       :file1:dog cat
dog rabbit
tiger mice
goose chicken
rabbit foxfile2:tiger donkey
lion fish
duck wolf
dog bird
cat bearfile3:snake pig
lion
cat
elephant
         ,    : 
 bear file2:1 ,
 
 bird file2:1 ,
 
 cat file2:1 ,file1:1 ,file3:1 ,
 
 chicken file1:1 ,
dog 
  file1:2 ,file2:1 ,duck 
  file2:1 ,dungkey 
  file2:1 ,elephant 
  file3:1 ,fish 
  file2:1 ,fox 
  file1:1 ,goose 
  file1:1 ,lion 
  file2:1 ,file3:1 ,mice 
  file1:1 ,pig 
  file3:1 ,rabbit 
  file1:2 ,snake 
  file3:1 ,tiger 
  file2:1 ,file1:1 ,wolf 
  file2:1 , 
  
 import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class InversedIndex {
	
	/**
	 *        ,
	 *                 map key  ,
	 *         value  
	 * */
	
	public static class InversedIndexMapper extends Mapper<Object, Text, Text, Text> {
		
		private Text outKey = new Text();
		private Text outVal = new Text();
		
		@Override
		public void map (Object key,Text value,Context context) {
			StringTokenizer tokens = new StringTokenizer(value.toString());
			FileSplit split = (FileSplit) context.getInputSplit();
			while(tokens.hasMoreTokens()) {
				String token = tokens.nextToken();
				try {
					outKey.set(token + ":" + split.getPath());
					outVal.set("1");
					context.write(outKey, outVal);
				} catch (IOException e) {
					e.printStackTrace();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * map      combiner  ,                   reduce  ,
	 *     key          ,         key,
	 *          ,    value,
	 *            ,             key--value  
	 * */
	public static class InversedIndexCombiner extends Reducer<Text, Text, Text, Text> {
		
		private Text outKey = new Text();
		private Text outVal = new Text();
		
		@Override
		public void reduce(Text key,Iterable<Text> values,Context context) {
			String[] keys = key.toString().split(":");
			int sum = 0;
			for(Text val : values) {
				sum += Integer.parseInt(val.toString());
			}
			try {
				outKey.set(keys[0]);
				int index = keys[keys.length-1].lastIndexOf('/');
				outVal.set(keys[keys.length-1].substring(index+1) + ":" + sum);
				context.write(outKey, outVal);
			} catch (IOException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	/**
	 *  combiner  key value   reduce,
	 *   combiner  ,            value,      value      
	 * */
	
	public static class InversedIndexReducer extends Reducer<Text, Text, Text, Text> {
		
		@Override
		public void reduce (Text key,Iterable<Text> values,Context context) {
			StringBuffer sb = new StringBuffer();
			for(Text text : values) {
				sb.append(text.toString() + " ,");
			}
			try {
				context.write(key, new Text(sb.toString()));
			} catch (IOException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
		Configuration conf = new Configuration();
		Job job = new Job(conf,"index inversed");
		
		job.setJarByClass(InversedIndex.class);
		job.setMapperClass(InversedIndexMapper.class);
		job.setCombinerClass(InversedIndexCombiner.class);
		job.setReducerClass(InversedIndexReducer.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);
		
		FileInputFormat.addInputPath(job, new Path("inversed.files"));
		FileOutputFormat.setOutputPath(job, new Path("inversed.result"));
		
		System.exit(job.waitForCompletion(true)?0:1);
		
	}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Azure HDInsight + Microsoft R Server에서 연산 처리 분산Microsoft Azure HDInsight는 Microsoft가 제공하는 Hadoop의 PaaS 서비스로 인프라 주변의 구축 노하우를 몰라도 훌륭한 Hadoop 클러스터를 구축할 수 있는 훌륭한 서비스입니다. 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.