1)、打开终端,切换为hadoop账户
su - hadoop
2)、进入hadoop环境
cd /usr/local/hadoop/
3)、运行hadoop
./sbin/start-dfs.sh
jps
注意:上面一定要有NameNode和DataNode节点哦!
1)、创建file文件夹:
./bin/hdfs dfs -mkdir /user/hadoop/file
2)、查看文件夹是否创建成功
./bin/hdfs dfs -ls /user/hadoop/
1)、创建file1.txt学生信息文件
./bin/hdfs dfs -touchz /user/hadoop/file/file1.txt
2)、对该文件写入学习信息内容
echo "张三-10001-物联1701班-物联网工程" | ./bin/hdfs dfs -appendToFile - /user/hadoop/file/file1.txt
3)、通过cat命令查看文件内容是否成功写入
./bin/hdfs dfs -cat /user/hadoop/file/file1.txt
4)、其他两个文件和上面步骤一样哦,大家也可以自己多建立几个文件,方便看效果哦!
1.第二个学生基本信息的创建步骤如下所示:
./bin/hdfs dfs -touchz /user/hadoop/file/file2.txt
echo "李四-10002-物联1702班-物联网工程" | ./bin/hdfs dfs -appendToFile - /user/hadoop/file/file2.txt
./bin/hdfs dfs -touchz /user/hadoop/file/file3.txt
echo "王五-10003-物联1703班-物联网工程" | ./bin/hdfs dfs -appendToFile - /user/hadoop/file/file3.txt
5)、创建过滤类型的文件file4.abc
创建file4.abc文件:
./bin/hdfs dfs -touchz /user/hadoop/file/file4.abc
写入文件内容:
echo "刘备-10004-物联1704班-物联网工程" | ./bin/hdfs dfs -appendToFile - /user/hadoop/file/file4.abc
查看文件是否成功写入:
./bin/hdfs dfs -cat /user/hadoop/file/file4.abc
1)、在路径为[ /user/hadoop/input/]下创建Merge.txt文件
./bin/hdfs dfs -touchz /user/hadoop/input/Merge.txt
2)、查看文件是否创建成功
./bin/hdfs dfs -ls /user/hadoop/input/
1)、在项目中右击->New->class
类名大家可以自己命名哦!
import java.io.IOException;
import java.io.PrintStream;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.PathFilter;
public class FilterMergeFile {
Path inputPath = null; //待合并的文件所在的目录的路径
Path outputPath = null; //输出文件的路径
public FilterMergeFile(String input, String output){
this.inputPath = new Path(input);
this.outputPath = new Path(output);
}
public void doMerge() throws IOException{
Configuration conf = new Configuration();
conf.set("fs.defaultFS","hdfs://localhost:9000" );
conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
FileSystem fsSource = FileSystem.get(URI.create(inputPath.toString()),conf);
FileSystem fsDst = FileSystem.get(URI.create(outputPath.toString()),conf);
FileStatus[] sourceStatus = fsSource.listStatus(inputPath, new myPathFilter(".*\\.abc")); //过滤掉目录中后缀为.abc的文件
FSDataOutputStream fsdos = fsDst.create(outputPath);
//下面分别读取过滤之后的每个文件的内容,并输出到同一个文件中
for(int i=0;i<sourceStatus.length;i++){
System.out.println("路径: " + sourceStatus[i].getPath()+ " 文件大小: " + sourceStatus[i].getLen() + " 权限: " + sourceStatus[i].getPermission() + " 内容: ");
FSDataInputStream fsdis = fsSource.open(sourceStatus[i].getPath());
byte[] data = new byte[1024];
int read = -1;
PrintStream ps = new PrintStream(System.out);
while((read = fsdis.read(data)) > 0){
ps.write(data, 0, read);
fsdos.write(data, 0, read);
}
}
fsdos.close();
}
public static void main(String args[]) throws IOException{
FilterMergeFile merge = new FilterMergeFile("/user/hadoop/file", "/user/hadoop/input/Merge.txt");
merge.doMerge();
}
}
class myPathFilter implements PathFilter{ //过滤掉文件名满足特定条件的文件
String reg = null;
myPathFilter(String reg){
this.reg = reg;
}
public boolean accept(Path path) {
if(!(path.toString().matches(reg)))
return true;
return false;
}
}
1)、在eclipse的终端结果如下所示:
2)、在终端利用cat命令查看Merge.txt文件内容
./bin/hdfs dfs -cat /user/hadoop/input/Merge.txt
通过上面可以看出,该java程序将==/user/hadoop/file==路径下的格式为.abc的文件过滤掉,然后将剩下的txt格式的学生信息文件在路径[/user/hadoop/input/]下的Merge.txt文件中合并,将三个分开的文件内容,合并到一个文件内容之中,实现成功!
以上就是本次博客的全部内容啦,希望通过对本次博客的阅读,能够帮助你解决HDFS文件系统的文件的过滤和合并哦!遇到问题的小伙伴请在评论区进行留言,林君学长看到会大家解答的,这个学长不太冷!
陈一月的又一天编程岁月^ _ ^