package org.ts.ddcs.worker;

import com.alibaba.fastjson.JSONObject;
import org.ts.ddcs.hadoop.HadoopUtils;
import org.ts.ddcs.log.LogHelper;


import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;

@SuppressWarnings("all")
public class ReadFdfsFileWorker implements Callable<Integer> {


    private String tm;

    ReadFdfsFileWorker(String tm) {
        this.tm = tm;
    }


    @Override
    public Integer call() throws Exception {
        try {
//            JSONObject data = new JSONObject();
//            Configuration cnf = HadoopUtils.hadoopConf();
//            FileSystem fs = FileSystem.get(new URI("hdfs://172.9.0.190:9000"), cnf);
//            String dir = "/flume/logs/" + tm.substring(2, 2 + "yy-mm-dd".length());
//            FileStatus[] statusArr = fs.listStatus(new Path(dir));        //listStatus方法用于列出目录下的所有文件
//            Path[] listPaths = FileUtil.stat2Paths(statusArr);            //用FileUtil工具类将FileStatus类型的数组转换为Path类型的数组
//            for (Path p : listPaths) {
//                System.out.println(p);
//
//                HdfsDataInputStream hdis = (HdfsDataInputStream) fs.open(p);
//                List<LocatedBlock> allBlocks = hdis.getAllBlocks();
//                for (LocatedBlock block : allBlocks) {
//                    ExtendedBlock eBlock = block.getBlock();
//                    System.out.println("------------------------");
//
//                    System.out.println(eBlock.getBlockId());
//                    System.out.println(eBlock.getBlockName());
//                    System.out.println(block.getBlockSize());
//                    System.out.println(block.getStartOffset());
//                    // 获取当前的数据块所在的DataNode的信息
//                    DatanodeInfo[] locations = block.getLocations();
//                    for (DatanodeInfo info : locations) {
//                        System.out.println(info.getIpAddr());
//                        System.out.println(info.getHostName());
//                    }
//                }
//
//                // 获取文件状态
//                FileStatus status = fs.getFileStatus(p);
//                long tm = status.getModificationTime();
//                short replicationFiles = status.getReplication();
//                SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                LogHelper.info(timeFormat.format(new Date(tm)));
//                SequenceFile.Reader reader = new SequenceFile.Reader(fs, p, cnf);
//                Writable key = (Writable) ReflectionUtils.newInstance(reader.getKeyClass(), cnf);
//                Writable value = (Writable) ReflectionUtils.newInstance(reader.getValueClass(), cnf);
//                while (reader.next(key, value)) {
//                    BytesWritable bw = (BytesWritable) value;
//                    String s = new String(bw.getBytes(), "utf-8");
//                    System.out.println(s);
//                }
//                IOUtils.closeStream(reader);
//            }
//            // 关闭文件操作对象
//            fs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
