package hadoopTest;

import java.io.IOException;
import java.util.Scanner;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;

public class MKdir {

    /**
     * @描述:创建文件
     * @作者:曹碧刚
     * @时间:2015年10月15日 上午11:32:24
     * @throws Exception
     */
    private static void mkdir() throws Exception{
        Configuration conf = new Configuration();
        FileSystem fileSystem =FileSystem.get(conf);
        Path path = new Path("hadoop_test");
        fileSystem.mkdirs(path);
    }
    
    /**
     * @描述:删除文件
     * @作者:曹碧刚
     * @时间:2015年10月15日 上午11:33:05
     * @throws IOException
     */
    private static void deletedir() throws IOException{
        Configuration conf = new Configuration();
        FileSystem fileSystem =FileSystem.get(conf);
        Path path = new Path("hadoop_test");
        fileSystem.delete(path,true);
    }
    
    /**
     * @描述:写文件
     * @作者:曹碧刚
     * @时间:2015年10月15日 上午11:37:46
     * @param args
     * @throws IOException
     * @throws Exception
     */
    private static void writerFile() throws IOException{
        Configuration conf = new Configuration();
        FileSystem fileSystem =FileSystem.get(conf);
        Path path = new Path("hadoop_test.txt");
        FSDataOutputStream dataOutputStream = fileSystem.create(path);
        dataOutputStream.writeUTF(" hello hadoop test");
    }
    
    /**
     * @throws IOException
     * @描述:读文件
     * @作者:曹碧刚
     * @时间:2015年10月15日 上午11:42:06
     */
    private static void readFile() throws IOException{
        Configuration conf = new Configuration();
        FileSystem fileSystem =FileSystem.get(conf);
        Path path = new Path("hadoop_test.txt");
        if(fileSystem.exists(path)){
            FSDataInputStream dataInputStream = fileSystem.open(path);
            FileStatus fileStatus = fileSystem.getFileStatus(path);
            byte b[] = new byte[Integer.parseInt(String.valueOf(fileStatus.getLen()))];
            dataInputStream.readFully(b);
        }
    }
    
    /**
     * @描述:遍历目录文件
     * @作者:曹碧刚
     * @时间:2015年10月15日 下午1:21:32
     * @throws IOException
     */
    private static void listDir() throws IOException{
        Configuration conf = new Configuration();
        FileSystem fileSystem =FileSystem.get(conf);
        Path path = new Path("/");
        
        FileStatus []fileStatus =  fileSystem.listStatus(path);
        print(fileStatus);
    }
    
    private static void print(FileStatus []fileStatus){
        for (int i = 0; i < fileStatus.length; i++) {
            FileStatus status = fileStatus[i];
            if(status.isDir()){
                
            }else{
                System.out.println(status.getPath().getName());
            }
        }
    }
    
    /**
     * @throws Exception
     * @描述:文件位置
     * @作者:曹碧刚
     * @时间:2015年10月15日 下午1:26:13
     */
    private static void fileLocal() throws Exception{
        Configuration conf = new Configuration();
        FileSystem fileSystem =FileSystem.get(conf);
        Path path = new Path("/user/centos");
        FileStatus fileStatus = fileSystem.getFileStatus(path);
        
        BlockLocation[] blockLocations = fileSystem.getFileBlockLocations(fileStatus, 0,fileStatus.getLen());
        
        for (int i = 0; i < blockLocations.length; i++) {
            String hosts[] = blockLocations[i].getHosts();
            for (int j = 0; j < hosts.length; j++) {
                System.out.println(hosts[j]+""+blockLocations[i].getLength());
            }
        }
    }
    
    /**
     * @throws IOException
     * @描述:节点信息
     * @作者:曹碧刚
     * @时间:2015年10月15日 下午1:31:30
     */
    private static void nodeDesc() throws IOException{
        Configuration conf = new Configuration();
        FileSystem fileSystem =FileSystem.get(conf);
        
        DistributedFileSystem distributedFileSystem =(DistributedFileSystem)fileSystem;
        DatanodeInfo datanodeInfo [] = distributedFileSystem.getDataNodeStats();
        for (int i = 0; i < datanodeInfo.length; i++) {
            System.out.println(datanodeInfo[i].getHostName() +"  "+datanodeInfo[i].getNonDfsUsed());
        }
    }
    
    /**
     * @描述:注意,不能这么运行
     * @作者:曹碧刚
     * @时间:2015年10月15日 下午2:01:54
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        String arg ="";
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("1、创建文件");
        System.out.println("2、删除文件");
        System.out.println("3、文件位置");
        System.out.println("4、遍历目录文件");
        System.out.println("5、节点信息");
        System.out.println("6、读文件");
        System.out.println("7、写文件");
        
        boolean isconture = true;
        while(isconture){
            arg = scanner.next();
            if("1".equals(args)){
                mkdir();
            }else if("2".equals(args)){
                deletedir();
            }else if("3".equals(args)){
                fileLocal();
            }else if("4".equals(args)){
                listDir();
            }else if("5".equals(args)){
                nodeDesc();
            }else if("6".equals(args)){
                readFile();
            }else if("7".equals(args)){
                writerFile();
            }
            
            System.out.println("是否继续 y/n");
            if(scanner.next().equals("n")){
                isconture = false;
            }
        }
        scanner.close();
    }
}