51cto赵强HADOOP学习(二)

hdfs常用命令

image.png
#cd training/data
#start-dfs.sh
#hdfs dfs -help
#hdfs dfs -help ls
#hdfs dfs -ls /
#hdfs dfs -lsr /
#hdfs dfs -du /
#hdfs dfs -dus /
#hdfs dfs -count /input
#hdfs dfs -mv /input/data.txt /output/data.txt
#hdfs dfs -lsr /
#hdfs dfs -cp /output/data.txt /input/data.txt
#hdfs dfs -lsr /
#hdfs dfs -rmr /output
#hdfs dfs -lsr /
# hdfs dfs -put apache-tomcat-7.0.65.tar.gz /input/tomcat.tar.gz
#hdfs dfs -lsr /
# hdfs dfs -get /input/tomcat.tar.gz a.tar.gz
#ll
#hdfs dfs -cat /input/data.txt
#hdfs dfs -text /input/data.txt
#hdfs dfs -mkdir /logs
#hdfs dfs -ls /
#hdfs dfs -touchz /logs/a.log
#hdfs dfs -lsr /

java编程案例

@Test
    public void testMakeDir() throws Exception {
        Configuration conf = new Configuration();
        
        //创建一个FileSystem的对象
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.56.102:9000"),conf);
        
        //创建目录
        fs.mkdirs(new Path("/demo"));
        
        
    }

设置权限

#pwd
/root/training/hadoop-2.4.1/etc/hadoop
#vi hdfs-site.xml
<property>
    <name>dfs.permissions</name>
    <value>false</value>
</property>
#start-dfs.sh
#jps
@Test
    public void testUpload() throws Exception{
        Configuration conf = new Configuration();
        
        //创建一个FileSystem的对象
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.56.102:9000"),conf);
        
        //构造一个输入流:d:\\temp\\a.avi
        InputStream in = new FileInputStream("d:\\temp\\a.avi");
        
        //构造输出流:/demo/a.avi
        OutputStream out = fs.create(new Path("/demo/a.avi"));
        
        //定义缓冲区
        byte[] buffer = new byte[1024];
        int len = 0;
        while((len=in.read(buffer))>0) {
            //将数据写到输出流 HDFS
            out.write(buffer,0,len);
        }
        out.flush();
        
        //关闭输入和输出流
        in.close();
        out.close();
    }
#hdfs dfs -lsr /
@Test
    public void testUpload() throws Exception{
        Configuration conf = new Configuration();
        
        //创建一个FileSystem的对象
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.56.102:9000"),conf);
        
        //构造一个输入流:d:\\temp\\a.avi
        InputStream in = new FileInputStream("d:\\temp\\a.avi");
        
        //构造输出流:/demo/a.avi
        OutputStream out = fs.create(new Path("/demo/b.avi"));
        
        //利用工具类简化操作
        IOUtils.copy(in, out);
        
//       //定义缓冲区
//        byte[] buffer = new byte[1024];
//        int len = 0;
//        while((len=in.read(buffer))>0) {
//          //将数据写到输出流 HDFS
//          out.write(buffer,0,len);
//        }
//        out.flush();
        
//        //关闭输入和输出流
//        in.close();
//        out.close();
    }
#hdfs dfs -lsr /demo
@Test
    public void testDownload() throws Exception{
        Configuration conf = new Configuration();
        
        //创建一个FileSystem的对象
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.56.102:9000"),conf);
        
        //构造一个输入流:HDFS
        InputStream in = fs.open(new Path("/demo/a.avi"));
        
        //构造输出流:d:\\temp\\abc.avi
        OutputStream out = new FileOutputStream("d:\\temp\\abc.avi");
        
        byte[] buffer = new byte[1024];
        int len = 0;
        while((len=in.read(buffer))>0) {
            out.write(buffer, 0, len);
        }
        
        out.flush();
        
        //关闭输入和输出流
        in.close();
        out.close();
        
    }

@Test
    public void testDownload() throws Exception{
        Configuration conf = new Configuration();
        
        //创建一个FileSystem的对象
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.56.102:9000"),conf);
        
        //构造一个输入流:HDFS
        InputStream in = fs.open(new Path("/demo/a.avi"));
        
        //构造输出流:d:\\temp\\abc.avi
        OutputStream out = new FileOutputStream("d:\\temp\\xyz.avi");
        
        //利用工具类简化操作
        IOUtils.copy(in, out);
        
 //       byte[] buffer = new byte[1024];
//        int len = 0;
//        while((len=in.read(buffer))>0) {
//          out.write(buffer, 0, len);
//        }
        
//        out.flush();
        
//        //关闭输入和输出流
 //       in.close();
 //       out.close();
        
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 当数据量增大到超出了单个物理计算机存储容量时,有必要把它分开存储在多个不同的计算机中。那些管理存储在多个网络互连的...
    单行线的旋律阅读 1,943评论 0 7
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,924评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,845评论 18 139
  • 总体而言,大学建筑还是偏于严肃与节制,所以现代主义风格仍然是当代大学中较为主流的建筑风格;有些历史悠久的大学仍然以...
    刘小博阅读 2,318评论 0 2
  • 关于告别 毕业那天,匆匆忙碌。校园里人来人往,一如当年院庆的时候那般热闹,去年的今朝也是如此,只是去年,与我无关。...
    司徒云飞阅读 330评论 0 1