hadoop fs -mkdir /mingzhu
    vim xiyouji.txt (创建这个文件)
    (在文件里面改)
    tangsanzang
    ls

    // moveFromLocal (以剪贴的方式上传,传完后本地文件会删除)
    hadoop fs -moveFromLocal ./xiyouji.txt /mingzhu (把当前目录下的xiyouji.txt文件上传到mingzhu这个文件夹)
    ls

    vim sanguo.txt
    // copyFromLocal (拷贝上传,传完后本地还存在)
    hadoop fs -copyFromLocal ./sanguo.txt /mingzhu
    vim hongloumeng.txt
    jiabaoyu
    ls

    //put
    hadoop fs -put ./hongloumeng.txt /mingzu (上传文件)

    // cat (查看文件中的内容)
    hadoop fs -cat /mingzu/xiyouji.txt

    // appendToFile
    vim tudi.txt
    hadoop fs -appendToFile ./tudi.txt /mingzhu/xiyouji.txt (把tudi.txt里面的文件内容加到mingzhu这个文件夹里面的xiyouji.txt文件里面)

    // copyToLocal (把集群上指定的文件下载到本地)
    hadoop fs -copyToLocal /mingzhu/xiyouji.txt ./xiyouji.txt (把集群上的这个位置的这个文件/mingzhu/xiyouji.txt下载到当前目录下,文件名为xiyouji.txt)

    //get (和copyToLocal一样)
    hadoop fs -get /mingzhu/xiyouji.txt ./xiyouji2.txt

    // du (显示占用空间的大小,以byte显示)
    -s (显示总的大小)
    -h (转换成可读性较好的结果显示出来)
    hadoop fs -du /minghzu (会显示里面每个文件的大小,以byte形式显示)

    log4j

    1. log4j.rootLogger=info,stdout,R
    2. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    3. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    4. log4j.appender.stdout.layout.ConversionPattern=%5p - %m%n
    5. log4j.appender.R=org.apache.log4j.RollingFileAppender
    6. log4j.appender.R.File=HdfsDemo.log
    7. log4j.appender.R.MaxFileSize=1MB
    8. log4j.appender.R.MaxBackupIndex=1
    9. log4j.appender.R.layout=org.apache.log4j.PatternLayout
    10. log4j.appender.R.layout.ConversionPattern=%p %t %c -%m%n

    main

    1. public static void main(String[] args) throws IOException, URISyntaxException {
    2. Configuration conf=new Configuration();
    3. conf.set("dfs.replication","2");
    4. URI uri = new URI("hdfs://192.168.139.145:8020");
    5. FileSystem fs = FileSystem.get(uri, conf);
    6. //读取文件
    7. /*String content=readContent(fs,"/mingzhu/xiyouji.txt");
    8. if(content==null)
    9. {
    10. System.out.println("content is Null");
    11. }else {
    12. System.out.println(content);
    13. }*/
    14. //上传文件
    15. //upload(fs,"D:\\eclipse\\shuihuzhuan.txt","/mingzhu");
    16. //upload(fs,"D:\\eclipse\\HdfsDemo\\input","/wordcount");
    17. //创建文件
    18. /*if(createFile(fs, "/mingzhu/a.txt")==true){
    19. System.out.println("创建成功");
    20. }*/
    21. //创建文件夹
    22. /*if(mkdir(fs, "/new")==true) {
    23. System.out.println("创建成功");
    24. }*/
    25. //删除文件
    26. /*if(deleteFile(fs,"/wordcount",true)) {
    27. System.out.println("删除成功");
    28. }*/
    29. //删除文件夹
    30. /*if(deleteFile(fs,"/new",true)){
    31. System.out.println("删除成功");
    32. }*/
    33. //获取文件最后更新时间
    34. //getLastTime(fs,"/mingzhu/xiyouji.txt");
    35. //查看block分布主机
    36. //fileLoc(fs,"/mingzhu/xiyouji.txt");
    37. //获取所有的DataNode结点信息
    38. //getDataNodeList(fs);
    39. //追加内容
    40. /*if(appendStringToFile(fs,"/mingzhu/xiyouji.txt")==true) {
    41. System.out.println("追加成功");
    42. }*/
    43. }

    读取文件

    1. public static String readContent(FileSystem fs, String path) throws IOException {
    2. Path f = new Path(path);
    3. if (!fs.exists(f)) {
    4. return null;
    5. } else {
    6. String str = null;
    7. StringBuilder sb = new StringBuilder(1024);
    8. try {
    9. FSDataInputStream in=fs.open(f);
    10. BufferedReader bf = new BufferedReader(new InputStreamReader(in));
    11. long time = System.currentTimeMillis();
    12. while ((str = bf.readLine()) != null) {
    13. sb.append(str + "\n");
    14. }
    15. System.out.println(System.currentTimeMillis()-time);
    16. } catch (IOException e) {
    17. } finally {
    18. fs.close();
    19. }
    20. return sb.toString();
    21. }
    22. }

    image.png

    创建文件

    1. public static boolean createFile(FileSystem fs,String path) throws IOException, URISyntaxException {
    2. Path f = new Path(path);// 目的路径
    3. fs.create(f).close();
    4. return true;
    5. }

    image.pngimage.png

    创建文件夹

    1. public static boolean mkdir(FileSystem fs,String path) throws IOException, URISyntaxException {
    2. Path f = new Path(path);// 目的路径
    3. boolean ok = fs.mkdirs(f);
    4. return ok;
    5. }

    image.png
    image.png

    删除文件及文件夹

    1. public static boolean deleteFile(FileSystem fs,String path,boolean recursive) throws IOException, URISyntaxException {
    2. Path f = new Path(path);// 目的路径
    3. boolean ok = fs.delete(f, recursive);
    4. return ok;
    5. }

    image.png
    image.png

    image.png
    image.png
    上传文件

    1. public static void upload(FileSystem fs, String localFile, String hdfsPath) throws IOException {
    2. Path src = new Path(localFile);// 要上传的
    3. Path dst = new Path(hdfsPath);// 目的路径
    4. fs.copyFromLocalFile(src, dst);
    5. System.out.println("upload to " + fs.getConf().get("fs.defaultFS"));// 默认配置文件的名称
    6. FileStatus files[] = fs.listStatus(dst);// 目的地址的所有文件状态
    7. for (FileStatus file : files) {
    8. System.out.println(file.getPath());
    9. }
    10. }

    image.png

    获取文件最后更新时间

    1. public static void getLastTime(FileSystem fs,String filepath) throws URISyntaxException, IOException {
    2. Path path = new Path(filepath);// 目的路径
    3. FileStatus status = fs.getFileStatus(path);
    4. System.out.println("Modification time is:"+status.getModificationTime());
    5. }

    image.png

    查看block分布主机

    1. public static void fileLoc(FileSystem fs,String filepath) throws URISyntaxException, IOException {
    2. Path path = new Path(filepath);// 目的路径
    3. FileStatus status = fs.getFileStatus(path);
    4. BlockLocation blocks[] = fs.getFileBlockLocations(status, 0,status.getLen());
    5. for(int i = 0 ; i <blocks.length;i++) {
    6. String hosts[] = blocks[i].getHosts();
    7. for(String host:hosts) {
    8. System.out.println("block:"+i+" location:"+host);
    9. }
    10. }
    11. }

    image.png

    获取所有的DataNode结点信息

    1. public static void getDataNodeList(FileSystem fs) throws URISyntaxException, IOException {
    2. DatanodeInfo dataNodeInfo[] = ((DistributedFileSystem)fs).getDataNodeStats();
    3. String names[] = new String[dataNodeInfo.length];
    4. for(int i = 0;i<dataNodeInfo.length;i++) {
    5. names[i] = dataNodeInfo[i].getHostName();
    6. System.out.println("node"+i+"hostname:"+names[i]);
    7. }
    8. }

    image.png

    追加文件

    1. public static boolean appendStringToFile(FileSystem fs,String filepath) throws URISyntaxException, IOException {
    2. Path f = new Path(filepath);// 目的路径
    3. Configuration conf = new Configuration();
    4. conf.set("dfs.client.block.write.replace-datanode-on-failure.policy", "NEVER");
    5. conf.set("dfs.client.block.write.replace-datanode-on-failure.enable", "true");
    6. FSDataOutputStream fos = fs.append(f);
    7. fos.write("\nhello world".getBytes());
    8. fos.close();
    9. return true;
    10. }

    image.pngimage.png

    There are 2 missing blocks. The following files may be corrupted:
    image.png
    //HDFS丢失了文件

    1. hdfs fsck /
    1. hdfs dfs -rm -r -f