hadoop读取文本格式的问题(读取缓存文件)

定义缓存文件

01    小米
02    华为
03    格力

文件名pd.txt

driver 里面添加缓存文件夹路径  

job.addCacheFile(new URI("file:///c:/gao/pd.txt"));

在Mapper中setup()方法中读取文件  并存到一个Map中

  Map<String,String> pdMap = new HashMap<>();

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        URI[] cacheFiles = context.getCacheFiles();
        for (URI cacheFile : cacheFiles) {
            BufferedReader br = new BufferedReader(
                                new InputStreamReader(
                                        new FileInputStream(cacheFile.getPath()),"UTF-8"));
            String line;
            while(StringUtils.isNotEmpty((line=br.readLine()))){
                String[] split = line.split("\t");
                pdMap.put(split[0],split[1]);

            }
            System.out.println(pdMap.get("01"));
            IOUtils.closeStream(br);
        }


    }

map里面存储了 3个键值对

但是map.get("01")确实null

放第一排,获取都是这样子

改格式即可  改为 UTF-8 无BOM 格式编码,然后就正常了

猜你喜欢

转载自blog.csdn.net/qq_42506914/article/details/86162904