头歌|HDFS实战

第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);

命令行:

        首先将资源文件(在文章顶部)拖入虚拟系统

58f05f72f6fa4b9ebd6b7e41d812dce0.png

16604c4dbe17447797dcb9a2f4e9af51.png

2f87f68d95d641a89063aefb08923241.png

之后在命令行运行代码

start-dfs.sh
mkdir -p /develop/input/
mv /data/workspace/userfiles/hello.txt /develop/input/

 

  • 16
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

跑得动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值