国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

hadoop怎么合并sequcefie并在map中讀取

發(fā)布時(shí)間:2021-09-04 11:56 來(lái)源:億速云 閱讀:0 作者:chen 欄目: 網(wǎng)絡(luò )安全

本篇內容介紹了“hadoop怎么合并sequcefie并在map中讀取”的有關(guān)知識,在實(shí)際案例的操作過(guò)程中,不少人都會(huì )遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學(xué)有所成!

package hgs.sequencefile;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;
//合并小文件
public class SequenceMain {
	public static void main(String[] args) throws IOException, URISyntaxException {
		
		Configuration conf = new Configuration();
		
		FileSystem fs = FileSystem.get(new URI("hdfs://192.168.6.129:9000"),conf);
		//獲得該文件夾下的所有的文件
		FileStatus[] fstats = fs.listStatus(new Path("/words"));
		//System.out.println(fstats.length);
		Text key = new Text();
		Text value = new Text();
		
		@SuppressWarnings("deprecation")
		//創(chuàng  )建一個(gè)sequecewriter
		//merge.seq是文件名
		SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf, new Path("/sequence/merge.seq"), key.getClass(), value.getClass());
		//循環(huán)遍歷每個(gè)文件 
		for(FileStatus fis : fstats) {
			//將每個(gè)文件以key value的形式寫(xiě)入到sequencefile中
			FSDataInputStream finput = fs.open(fis.getPath());
			byte[] buffer = new byte[(int)fis.getLen()];
			IOUtils.readFully(finput, buffer, 0, buffer.length);
			//文件名為key 文件內容為value
			key.set(fis.getPath().getName());
			value.set(buffer);
			writer.append(key, value);
			finput.close();			
		}
		writer.close();
		fs.close();		
	}
}
package hgs.sequencefile;
import java.io.IOException;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class SequnceMapper extends Mapper<Text, Text, Text, Text> {
	@Override
	protected void map(Text key, Text value, Mapper<Text, Text, Text, Text>.Context context)
			throws IOException, InterruptedException {
		context.write(key, value);
	}
}
package hgs.sequencefile;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.SequenceFileOutputFormat;
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.SequenceFileAsTextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class SequenceDriver {
	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf, "read_sequence_file");
		job.setJarByClass(hgs.sequencefile.SequenceDriver.class);
		// TODO: specify a mapper
		job.setMapperClass(SequnceMapper.class);
		// TODO: specify a reducer
		//job.setReducerClass(Reducer.class);
		// TODO: specify output types
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);
		//在這個(gè)設置讀取sequencefile的inputformat,該類(lèi)讀取的是String淚習慣的key value
		//SequenceFileAsBinaryInputFormat 該類(lèi)獨處的ByteWritable的key value
		job.setInputFormatClass(SequenceFileAsTextInputFormat.class);
		// TODO: specify input and output DIRECTORIES (not files)
		FileInputFormat.setInputPaths(job, new Path("hdfs://192.168.6.129:9000/sequence"));
		FileOutputFormat.setOutputPath(job, new Path("hdfs://192.168.6.129:9000/seqresult"));
		if (!job.waitForCompletion(true))
			return;
	}
}

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。

日本肉体裸XXXXBBBB| 亚洲欧洲美色一区二区三区| 久久97精品久久久久久久不卡| 久久久久国产一区二区| 久久国产免费观看精品3| 亚洲 欧美 另类 在线|