第1关:HDFS基础命令实战
start-dfs.sh
vi stu01.txt
#vi#按i键进入编辑模式,左下角出现--INSERT--字符后,输入以下内容
234
5678
Hadoop
#vi#按ESC 键,左下角--INSERT--字符消失后,在英文输入状态下输入 :wq ,回车保存退出文件。
hdfs dfs -mkdir /user/stu02
hdfs dfs -put stu01.txt /user/stu02
第2关:HDFS高级命令实战
start-dfs.sh
vi stu01_2.txt
#vi#按 i 进入编辑模式,输入内容
hadoop
hive
#vi#然后按 esc 键,再同时按下 shift 和冒号键,输入 wq,保存退出
hdfs dfs -mkdir /user/stu01/
hdfs dfs -moveFromLocal stu01_2.txt /user/stu01/
vi stu01.txt
#vi#按i键进入编辑模式,左下角出现--INSERT--字符后,输入以下内容
234
5678
Hadoop
#vi#按ESC 键,左下角--INSERT--字符消失后,在英文输入状态下输入 :wq ,回车保存退出文件。
hdfs dfs -appendToFile stu01.txt /user/stu01/stu01_2.txt
hdfs dfs -get /user/stu01/stu01_2.txt .
第3关:使用JAVA API读取HDFS的数据
URI uri=URI.create("hdfs://localhost:9000/user/hadoop/task.txt");
Configuration config=new Configuration();
FileSystem fs =FileSystem get(uri, config);
InputStream in=null;
try{
in=fs.open(new Path(uri));
IOUtils. copyBytes (in, System. out, 2048, false);
} catch(Exception e){
IOUtils. closeStream(in);
}
第4关:使用JAVA API将数据写入HDFS
代码文件:
File localPath=new File("/develop/input/hello.txt");
String hdfsPath="hdfs://localhost:9000/user/tmp/hello.txt";
InputStream in=new BufferedInputStream(new FileInputStream (localPath));
Configuration config=new Configuration();
FileSystem fs=FileSystem.get(URI.create(hdfsPath),config);
long fileSize =localPath.length()>65536 ? localPath.length() / 65536 : 1;
FSDataOutputStream out =fs.create(new Path(hdfsPath),new Progressable(){
long fileCount=0;
public void progress){
System.out.println("总进度"+(fileCount/fileSize)*100+"%");
fileCount++;
}
});
IOUtils.copyBytes(in,out,2048,true);
命令行:
首先将资源文件(在文章顶部)拖入虚拟系统
之后在命令行运行代码
start-dfs.sh
mkdir -p /develop/input/
mv /data/workspace/userfiles/hello.txt /develop/input/