• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Java操作Hdfs

武飞扬头像
CJunZero
帮助1

java代码

  1.  
    package cn.itcast.hadoop.hdfs;
  2.  
     
  3.  
    import java.io.FileInputStream;
  4.  
    import java.io.FileNotFoundException;
  5.  
    import java.io.FileOutputStream;
  6.  
    import java.io.IOException;
  7.  
    import java.io.InputStream;
  8.  
    import java.io.OutputStream;
  9.  
    import java.net.URI;
  10.  
    import java.net.URISyntaxException;
  11.  
     
  12.  
    import org.apache.hadoop.conf.Configuration;
  13.  
    import org.apache.hadoop.fs.FileSystem;
  14.  
    import org.apache.hadoop.fs.Path;
  15.  
    import org.apache.hadoop.io.IOUtils;
  16.  
    import org.junit.Before;
  17.  
    import org.junit.Test;
  18.  
     
  19.  
    public class HDFSDemo {
  20.  
     
  21.  
    FileSystem fs = null;
  22.  
     
  23.  
    @Before
  24.  
    public void init() throws Exception{
  25.  
    fs = FileSystem.get(new URI("hdfs://itcast01:9000"), new Configuration(), "root");
  26.  
    }
  27.  
     
  28.  
    @Test
  29.  
    public void testUpload() throws Exception{
  30.  
    InputStream in = new FileInputStream("/root/install.log");
  31.  
    OutputStream out = fs.create(new Path("/log123.log"));
  32.  
    IOUtils.copyBytes(in, out, 1024, true);
  33.  
    }
  34.  
     
  35.  
    @Test
  36.  
    public void testMkdir() throws IllegalArgumentException, IOException{
  37.  
    boolean flag = fs.mkdirs(new Path("/a/aa"));
  38.  
    System.out.println(flag);
  39.  
    }
  40.  
     
  41.  
    @Test
  42.  
    public void testDel() throws IllegalArgumentException, IOException{
  43.  
    boolean flag = fs.delete(new Path("/a"), true);
  44.  
    System.out.println(flag);
  45.  
    }
  46.  
     
  47.  
     
  48.  
     
  49.  
    public static void main(String[] args) throws Exception {
  50.  
    FileSystem fs = FileSystem.get(new URI("hdfs://itcast01:9000"), new Configuration());
  51.  
    InputStream in = fs.open(new Path("/jdk"));
  52.  
    OutputStream out = new FileOutputStream("/home/jdk1.7.tar.gz");
  53.  
    IOUtils.copyBytes(in, out, 4096, true);
  54.  
     
  55.  
    }
  56.  
    }
学新通

处理连不上hdfs的情况

1、删除tmp

学新通

 2、修改配置

修改namenode的地址为具体IP (之前写的localhost)

学新通

修改slaves文件,为具体IP

学新通

3、 格式化hdfs

hadoop namenode –format

4、启动hdfs

start-dfs.sh

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhibfcga
系列文章
更多 icon
同类精品
更多 icon
继续加载