簡體   English   中英

Hadoop工作目錄

[英]Hadoop working Directory

我試圖將文件保存在Hadoop應用程序的主類中,以便以后可以由映射器讀取。 該文件是將用於加密數據的加密密鑰。 我的問題是,如果我將文件寫入工作目錄,數據將在哪里結束?

public class HadoopIndexProject {

    private static SecretKey generateKey(int size, String Algorithm) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        KeyGenerator keyGen = KeyGenerator.getInstance(Algorithm);
        keyGen.init(size);
        return keyGen.generateKey();
    }

    private static IvParameterSpec generateIV() {
        byte[] b = new byte[16];
        new Random().nextBytes(b);
        return new IvParameterSpec(b);    
    }

    public static void saveKey(SecretKey key, IvParameterSpec IV, String path) throws IOException {
        FileOutputStream stream = new FileOutputStream(path);
        //FSDataOutputStream stream = fs.create(new Path(path));
        try {
            stream.write(key.getEncoded());
            stream.write(IV.getIV());
        } finally {
            stream.close();
        }
    }

    /**
     * @param args the command line arguments
     * @throws java.lang.Exception
     */
    public static void main(String[] args) throws Exception {
        // TODO code application logic here
        Configuration conf = new Configuration();
        //FileSystem fs = FileSystem.getLocal(conf);
        String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
        SecretKey KEY;
        IvParameterSpec IV;
        if (otherArgs.length != 2) {
            System.err.println("Usage: Index <in> <out>");
            System.exit(2);
        }
        try {
            if(! new File("key.dat").exists()) {
                KEY = generateKey(128, "AES");
                IV = generateIV();
                saveKey(KEY, IV, "key.dat");
            }
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(HadoopIndexMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
        conf.set("mapred.textoutputformat.separator", ":");

        Job job = Job.getInstance(conf);
        job.setJobName("Index creator");
        job.setJarByClass(HadoopIndexProject.class);      
        job.setMapperClass(HadoopIndexMapper.class);
        job.setReducerClass(HadoopIndexReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntArrayWritable.class);

        FileInputFormat.addInputPath(job, new Path(otherArgs[0]) {});
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));

        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }

}

HDFS中沒有工作目錄的概念。 所有相對路徑都是/user/<username>路徑,因此您的文件將位於/user/<username> /user/<username>/key.dat

但是在yarn中,您具有分布式緩存的概念,因此可以使用job.addCacheFile添加紗線應用程序的其他文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM