rm -rf /opt/linuxsir/hadoop/logs/*.*
ssh root@192.168.31.132 rm -rf /opt/linuxsir/hadoop/logs/*.*
ssh root@192.168.31.133 rm -rf /opt/linuxsir/hadoop/logs/*.*
clear
cd /opt/linuxsir/hadoop/sbin
./start-dfs.sh
./start-yarn.sh
clear
jps
ssh root@192.168.31.132 jps
ssh root@192.168.31.133 jps
在eclipse里面操作如下:
New
-Java Project
,名称自定义即可,如 java-prj
New
-Package
,名称自定义为com.pai.hdfs_demo
New
-Class
,名称自定义为ReadWriteHDFSExample
package com.pai.hdfs_demo;
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.*;
import java.nio.charset.StandardCharsets;
public class ReadWriteHDFSExample {
// main 新建一个类ReadWriteHDFSExample,编写main函数如下。main函数调用其它函数,创建目录,写入数据,添加数据,然后再读取数据
public static void main(String[] args) throws IOException {
// ReadWriteHDFSExample.checkExists();
ReadWriteHDFSExample.createDirectory();
ReadWriteHDFSExample.writeFileToHDFS();
ReadWriteHDFSExample.appendToHDFSFile();
ReadWriteHDFSExample.readFileFromHDFS();
}
// readFileFromHDFS 该函数读取文件内容,以字符串形式显示出来
public static void readFileFromHDFS() throws IOException {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS", "hdfs://192.168.31.131:9000");
FileSystem fileSystem = FileSystem.get(configuration);
// Create a path
String fileName = "read_write_hdfs_example.txt";
Path hdfsReadPath = new Path("/javareadwriteexample/" + fileName);
// initialize input stream
FSDataInputStream inputStream = fileSystem.open(hdfsReadPath);
// Classical input stream usage
String out = IOUtils.toString(inputStream, "UTF-8");
System.out.println(out);
// BufferedReader bufferedReader = new BufferedReader(
// new InputStreamReader(inputStream, StandardCharsets.UTF_8));
// String line = null;
// while ((line=bufferedReader.readLine())!=null){
// System.out.println(line);
// }
inputStream.close();
fileSystem.close();
}
// writeFileToHDFS writeFileToHDFS函数打开文件,写入一行文本
public static void writeFileToHDFS() throws IOException {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS", "hdfs://192.168.31.131:9000");
FileSystem fileSystem = FileSystem.get(configuration);
// Create a path
String fileName = "read_write_hdfs_example.txt";
Path hdfsWritePath = new Path("/javareadwriteexample/" + fileName);
FSDataOutputStream fsDataOutputStream = fileSystem.create(hdfsWritePath, true);
BufferedWriter bufferedWriter = new BufferedWriter(
new OutputStreamWriter(fsDataOutputStream, StandardCharsets.UTF_8));
bufferedWriter.write("Java API to write data in HDFS");
bufferedWriter.newLine();
bufferedWriter.close();
fileSystem.close();
}
// appendToHDFSFile 函数打开文件,添加一行文本。需要注意的是,需要对Configuration类的对象configuration进行适当设置,否则出错
public static void appendToHDFSFile() throws IOException {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS", "hdfs://192.168.31.131:9000");
//configuration.setBoolean("dfs.client.block.write.replace-datanode-on-failure.enabled", true);
configuration.set("dfs.client.block.write.replace-datanode-on-failure.policy","NEVER");
configuration.set("dfs.client.block.write.replace-datanode-on-failure.enable","true");
FileSystem fileSystem = FileSystem.get(configuration);
// Create a path
String fileName = "read_write_hdfs_example.txt";
Path hdfsWritePath = new Path("/javareadwriteexample/" + fileName);
FSDataOutputStream fsDataOutputStream = fileSystem.append(hdfsWritePath);
BufferedWriter bufferedWriter = new BufferedWriter(
new OutputStreamWriter(fsDataOutputStream, StandardCharsets.UTF_8));
bufferedWriter.write("Java API to append data in HDFS file");
bufferedWriter.newLine();
bufferedWriter.close();
fileSystem.close();
}
// createDirectory 函数创建一个目录
public static void createDirectory() throws IOException {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS", "hdfs://192.168.31.131:9000");
FileSystem fileSystem = FileSystem.get(configuration);
String directoryName = "/javareadwriteexample";
Path path = new Path(directoryName);
fileSystem.mkdirs(path);
}
// checkExists checkExists检查目录或者文件是否存在。注意如下代码的最后一个括号是ReadWriteHDFSExample类的结束括号
public static void checkExists() throws IOException {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS", "hdfs://192.168.31.131:9000");
FileSystem fileSystem = FileSystem.get(configuration);
String directoryName = "/javareadwriteexample";
Path path = new Path(directoryName);
if (fileSystem.exists(path)) {
System.out.println("File/Folder Exists : " + path.getName());
} else {
System.out.println("File/Folder does not Exists : " + path.getName());
}
}
}
为了编译通过上述Java代码,需要把如下目录下的jar包导入Eclipse项目的Build Path
操作序列为 右键点击Eclipse里的Java项目→Properties
→Java Build Path
→Libraries
→Add External Jars
# 添加如下路径的包
D:hadoop-2.7.3sharehadoopcommonlib
D:hadoop-2.7.3sharehadoopcommon
D:hadoop-2.7.3sharehadoophdfs
D:hadoop-2.7.3sharehadoophdfslib
D:hadoop-2.7.3sharehadoopmapreducelib
D:hadoop-2.7.3sharehadoopmapreduce
D:hadoop-2.7.3sharehadoopyarnlib
D:hadoop-2.7.3sharehadoopyarn
就可以愉快地执行了,执行完毕上述代码后,在hd-master主机上可以通过如下命令,检查已经写入的文件
[root@hd-master bin]# cd /opt/linuxsir/hadoop/bin
[root@hd-master bin]# ./hdfs dfs -ls /javareadwriteexample/read_write_hdfs_example.txt
-rw-r--r-- 3 root supergroup 70 2024-10-10 04:47 /javareadwriteexample/read_write_hdfs_example.txt
[root@hd-master bin]# ./hdfs dfs -cat /javareadwriteexample/read_write_hdfs_example.txt
Java API to write data in HDFS
Java API to append data in HDFS file
为了多次进行实验(或者为了调试代码),可以把HDFS文件删除,然后再执行或者调试Java代码,否则一经存在该目录,执行创建目录的代码就会出错
cd /opt/linuxsir/hadoop/bin
./hdfs dfs -rm /javareadwriteexample/*
./hdfs dfs -rmdir /javareadwriteexample
cd /opt/linuxsir/hadoop/sbin
./stop-yarn.sh
./stop-dfs.sh
jps
ssh root@192.168.31.132 jps
ssh root@192.168.31.133 jps
package mywordcount;
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.IntWritable;
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.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class WordCount {
//定义WordCount类的内部类TokenizerMapper 该类实现了map函数,把从文件读取的每个word变成一个形式为<word,1>的Key Value对,输出到map函数的参数context对象,由执行引擎完成Shuffle
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 {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
}
}
}
//定义WordCount类的内部类IntSumReducer 该类实现了reduce函数,它收拢所有相同key的、形式为<word,1>的Key-Value对,对Value部分进行累加,输出一个计数
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);
String thekey = key.toString();
int thevalue = sum;
}
}
// WordCount类的main函数,负责配置Job的若干关键的参数,并且启动这个Job。在main函数中,conf对象包含了一个属性即“fs.defaultFS”,它的值为“hdfs://192.168.31.131:9000”,使得WordCount程序知道如何存取HDFS
public static void main(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);
}
conf.set("fs.defaultFS", "hdfs://192.168.31.131:9000");
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.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
[root@hd-master bin]# ./hdfs dfs -ls /output1
Found 2 items
-rw-r--r-- 3 root supergroup 0 2024-10-10 05:17 /output1/_SUCCESS
-rw-r--r-- 3 root supergroup 89 2024-10-10 05:17 /output1/part-r-00000
[root@hd-master bin]# ./hdfs dfs -cat /output1/part-r-00000
I 1
apache 1
cloudera 1
google 1
hadoop 8
hortonworks 1
ibm 1
intel 1
like 1
microsoft 1
数据分析咨询请扫描二维码
若不方便扫码,搜微信号:CDAshujufenxi
CDA持证人简介:居瑜 ,CDA一级持证人,国企财务经理,13年财务管理运营经验,在数据分析实践方面积累了丰富的行业经验。 一、 ...
2025-04-16持证人简介: CDA持证人刘凌峰,CDA L1持证人,微软认证讲师(MCT)金山办公最有价值专家(KVP),工信部高级项目管理师,拥有 ...
2025-04-15持证人简介:CDA持证人黄葛英,ICF国际教练联盟认证教练,前字节跳动销售主管,拥有丰富的行业经验。在实际生活中,我们可能会 ...
2025-04-14在 Python 编程学习与实践中,Anaconda 是一款极为重要的工具。它作为一个开源的 Python 发行版本,集成了众多常用的科学计算库 ...
2025-04-14随着大数据时代的深入发展,数据运营成为企业不可或缺的岗位之一。这个职位的核心是通过收集、整理和分析数据,帮助企业做出科 ...
2025-04-11持证人简介:CDA持证人黄葛英,ICF国际教练联盟认证教练,前字节跳动销售主管,拥有丰富的行业经验。 本次分享我将以教培行业为 ...
2025-04-11近日《2025中国城市长租市场发展蓝皮书》(下称《蓝皮书》)正式发布。《蓝皮书》指出,当前我国城市住房正经历从“增量扩张”向 ...
2025-04-10在数字化时代的浪潮中,数据已经成为企业决策和运营的核心。每一位客户,每一次交易,都承载着丰富的信息和价值。 如何在海量客 ...
2025-04-09数据是数字化的基础。随着工业4.0的推进,企业生产运作过程中的在线数据变得更加丰富;而互联网、新零售等C端应用的丰富多彩,产 ...
2025-04-094月7日,美国关税政策对全球金融市场的冲击仍在肆虐,周一亚市早盘,美股股指、原油期货、加密货币、贵金属等资产齐齐重挫,市场 ...
2025-04-08背景 3月26日,科技圈迎来一则重磅消息,苹果公司宣布向浙江大学捐赠 3000 万元人民币,用于支持编程教育。 这一举措并非偶然, ...
2025-04-07在当今数据驱动的时代,数据分析能力备受青睐,数据分析能力频繁出现在岗位需求的描述中,不分岗位的任职要求中,会特意标出“熟 ...
2025-04-03在当今数字化时代,数据分析师的重要性与日俱增。但许多人在踏上这条职业道路时,往往充满疑惑: 如何成为一名数据分析师?成为 ...
2025-04-02最近我发现一个绝招,用DeepSeek AI处理Excel数据简直太爽了!处理速度嘎嘎快! 平常一整天的表格处理工作,现在只要三步就能搞 ...
2025-04-01你是否被统计学复杂的理论和晦涩的公式劝退过?别担心,“山有木兮:统计学极简入门(Python)” 将为你一一化解这些难题。课程 ...
2025-03-31在电商、零售、甚至内容付费业务中,你真的了解你的客户吗? 有些客户下了一两次单就消失了,有些人每个月都回购,有些人曾经是 ...
2025-03-31在数字化浪潮中,数据驱动决策已成为企业发展的核心竞争力,数据分析人才的需求持续飙升。世界经济论坛发布的《未来就业报告》, ...
2025-03-28你有没有遇到过这样的情况?流量进来了,转化率却不高,辛辛苦苦拉来的用户,最后大部分都悄无声息地离开了,这时候漏斗分析就非 ...
2025-03-27TensorFlow Datasets(TFDS)是一个用于下载、管理和预处理机器学习数据集的库。它提供了易于使用的API,允许用户从现有集合中 ...
2025-03-26"不谋全局者,不足谋一域。"在数据驱动的商业时代,战略级数据分析能力已成为职场核心竞争力。《CDA二级教材:商业策略数据分析 ...
2025-03-26