
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
BI 大数据分析师:连接数据与业务的价值转化者 在大数据与商业智能(Business Intelligence,简称 BI)深度融合的时代,BI ...
2025-07-10SQL 在预测分析中的应用:从数据查询到趋势预判 在数据驱动决策的时代,预测分析作为挖掘数据潜在价值的核心手段,正被广泛 ...
2025-07-10数据查询结束后:分析师的收尾工作与价值深化 在数据分析的全流程中,“query end”(查询结束)并非工作的终点,而是将数 ...
2025-07-10尊敬的考生: 您好! 我们诚挚通知您,CDA Level I和 Level II考试大纲将于 2025年7月25日 实施重大更新。 此次更新旨在确保认 ...
2025-07-09CDA 数据分析师考试:从报考到取证的全攻略 在数字经济蓬勃发展的今天,数据分析师已成为各行业争抢的核心人才,而 CDA(Certi ...
2025-07-09【CDA干货】单样本趋势性检验:捕捉数据背后的时间轨迹 在数据分析的版图中,单样本趋势性检验如同一位耐心的侦探,专注于从单 ...
2025-07-09year_month数据类型:时间维度的精准切片 在数据的世界里,时间是最不可或缺的维度之一,而year_month数据类型就像一把精准 ...
2025-07-09CDA 备考干货:Python 在数据分析中的核心应用与实战技巧 在 CDA 数据分析师认证考试中,Python 作为数据处理与分析的核心 ...
2025-07-08SPSS 中的 Mann-Kendall 检验:数据趋势与突变分析的有力工具 在数据分析的广袤领域中,准确捕捉数据的趋势变化以及识别 ...
2025-07-08备战 CDA 数据分析师考试:需要多久?如何规划? CDA(Certified Data Analyst)数据分析师认证作为国内权威的数据分析能力认证 ...
2025-07-08LSTM 输出不确定的成因、影响与应对策略 长短期记忆网络(LSTM)作为循环神经网络(RNN)的一种变体,凭借独特的门控机制,在 ...
2025-07-07统计学方法在市场调研数据中的深度应用 市场调研是企业洞察市场动态、了解消费者需求的重要途径,而统计学方法则是市场调研数 ...
2025-07-07CDA数据分析师证书考试全攻略 在数字化浪潮席卷全球的当下,数据已成为企业决策、行业发展的核心驱动力,数据分析师也因此成为 ...
2025-07-07剖析 CDA 数据分析师考试题型:解锁高效备考与答题策略 CDA(Certified Data Analyst)数据分析师考试作为衡量数据专业能力的 ...
2025-07-04SQL Server 字符串截取转日期:解锁数据处理的关键技能 在数据处理与分析工作中,数据格式的规范性是保证后续分析准确性的基础 ...
2025-07-04CDA 数据分析师视角:从数据迷雾中探寻商业真相 在数字化浪潮席卷全球的今天,数据已成为企业决策的核心驱动力,CDA(Certifie ...
2025-07-04CDA 数据分析师:开启数据职业发展新征程 在数据成为核心生产要素的今天,数据分析师的职业价值愈发凸显。CDA(Certified D ...
2025-07-03从招聘要求看数据分析师的能力素养与职业发展 在数字化浪潮席卷全球的当下,数据已成为企业的核心资产,数据分析师岗位也随 ...
2025-07-03Power BI 中如何控制过滤器选择项目数并在超限时报错 引言 在使用 Power BI 进行数据可视化和分析的过程中,对过滤器的有 ...
2025-07-03把握 CDA 考试时间,开启数据分析职业之路 在数字化转型的时代浪潮下,数据已成为企业决策的核心驱动力。CDA(Certified Da ...
2025-07-02