Java執(zhí)行hadoop的基本操作實例代碼
更新時間:2017年04月25日 09:28:37 投稿:lqh
這篇文章主要介紹了Java執(zhí)行hadoop的基本操作實例代碼的相關資料,需要的朋友可以參考下
Java執(zhí)行hadoop的基本操作實例代碼
向HDFS上傳本地文件
public static void uploadInputFile(String localFile) throws IOException{
Configuration conf = new Configuration();
String hdfsPath = "hdfs://localhost:9000/";
String hdfsInput = "hdfs://localhost:9000/user/hadoop/input";
FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
fs.copyFromLocalFile(new Path(localFile), new Path(hdfsInput));
fs.close();
System.out.println("已經(jīng)上傳文件到input文件夾啦");
}
將output文件下載到本地
public static void getOutput(String outputfile) throws IOException{
String remoteFile = "hdfs://localhost:9000/user/hadoop/output/part-r-00000";
Path path = new Path(remoteFile);
Configuration conf = new Configuration();
String hdfsPath = "hdfs://localhost:9000/";
FileSystem fs = FileSystem.get(URI.create(hdfsPath),conf);
fs.copyToLocalFile(path, new Path(outputfile));
System.out.println("已經(jīng)將輸出文件保留到本地文件");
fs.close();
}
刪除hdfs中的文件
public static void deleteOutput() throws IOException{
Configuration conf = new Configuration();
String hdfsOutput = "hdfs://localhost:9000/user/hadoop/output";
String hdfsPath = "hdfs://localhost:9000/";
Path path = new Path(hdfsOutput);
FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
fs.deleteOnExit(path);
fs.close();
System.out.println("output文件已經(jīng)刪除");
}
執(zhí)行mapReduce程序
創(chuàng)建Mapper類和Reducer類
public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(Object key, Text value, Context context) throws IOException, InterruptedException{
String line = value.toString();
line = line.replace("\\", "");
String regex = "性別:</span><span class=\"pt_detail\">(.*?)</span>";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(line);
while(matcher.find()){
String term = matcher.group(1);
word.set(term);
context.write(word, one);
}
}
}
public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException{
int sum = 0;
for(IntWritable val :values){
sum+= val.get();
}
result.set(sum);
context.write(key, result);
}
}
執(zhí)行mapReduce程序
public static void runMapReduce(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if(otherArgs.length != 2){
System.err.println("Usage: wordcount<in> <out>");
System.exit(2);
}
Job job = new Job(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.out.println("mapReduce 執(zhí)行完畢!");
System.exit(job.waitForCompletion(true)?0:1);
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
您可能感興趣的文章:
- java結合HADOOP集群文件上傳下載
- Java訪問Hadoop分布式文件系統(tǒng)HDFS的配置說明
- java使用hadoop實現(xiàn)關聯(lián)商品統(tǒng)計
- 深入淺析Java Object Serialization與 Hadoop 序列化
- hadoop中實現(xiàn)java網(wǎng)絡爬蟲(示例講解)
- Java/Web調用Hadoop進行MapReduce示例代碼
- Hadoop運行時遇到java.io.FileNotFoundException錯誤的解決方法
- hadoop運行java程序(jar包)并運行時動態(tài)指定參數(shù)
- java實現(xiàn)對Hadoop的操作
- 利用Java連接Hadoop進行編程
相關文章
詳解如何使用MyBatis實現(xiàn)數(shù)據(jù)庫的CRUD
這篇文章主要為大家詳細介紹了如何使用MyBatis實現(xiàn)數(shù)據(jù)庫的CRUD操作,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2024-12-12
Java 圖解Spring啟動時的后置處理器工作流程是怎樣的
spring的后置處理器有兩類,bean后置處理器,bf(BeanFactory)后置處理器。bean后置處理器作用于bean的生命周期,bf的后置處理器作用于bean工廠的生命周期2021-10-10
基于Java將Excel科學計數(shù)法解析成數(shù)字
這篇文章主要介紹了基于Java將Excel科學計數(shù)法解析成數(shù)字,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-09-09

