簡體   English   中英

如何在HDFS上讀取文件以在Hadoop上進行分布式緩存

[英]How to read a file on HDFS for distributed cache on Hadoop

我正在嘗試從HDFS加載hadoop的分布式緩存中的文件,但是它不起作用。 我正在使用hadoop 2.5.1版本。 這是關於我如何在映射器中使用緩存文件的代碼:

@Override
    protected void setup(Context context) throws IOException, InterruptedException {
        URI[] uris = context.getCacheFiles();

        for (URI uri : uris) {
            File usersFile = new File(uri);
            BufferedReader reader = null;
            reader = new BufferedReader(new FileReader(usersFile));
            String line = reader.readLine();

            ...

            reader.close();
        }
    }

以下是我嘗試在驅動程序中加載緩存的三種不同方式:1)如果我將文件放在這樣的緩存中,它可以工作,但是它將從本地FS加載文件(我在Mac上運行代碼) 。

 job.addCacheFile(new URI("file:///input/users.txt"));

2)如果我按照以下方式使用hdfs(文件存在於hdfs的“ / input /”下):

job.addCacheFile(new URI("hdfs:///input/users.txt"));

我得到這個例外:

java.lang.Exception: java.lang.IllegalArgumentException: URI scheme is not "file"
    at org.apache.hadoop.mapred.LocalJobRunner$Job.runTasks(LocalJobRunner.java:462)
    at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:522)
Caused by: java.lang.IllegalArgumentException: URI scheme is not "file"
    at java.io.File.<init>(File.java:395)

3)這是我嘗試加載文件的第三種方式:

job.addCacheFile(new URI("hdfs://localhost:9000/input/users.txt"));

我得到以下異常:

java.lang.Exception: java.lang.IllegalArgumentException: URI scheme is not "file"
    at org.apache.hadoop.mapred.LocalJobRunner$Job.runTasks(LocalJobRunner.java:462)
    at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:522)
Caused by: java.lang.IllegalArgumentException: URI scheme is not "file"
    at java.io.File.<init>(File.java:395)

如果有人能闡明為什么發生這些異常,我將不勝感激。

我有同樣的問題。 如果文件位於Pro Apache Hadoop 2nd Edition中的hdfs中,則引用了另一種方法。 您只能通過下載本書的源代碼來訪問示例代碼。 我將在第6章文件夾(MapSideJoinMRJob3.java)中發布不同的代碼段。 希望這可以幫助::

    private FileSystem hdfs = null;

    public List<String> readLinesFromJobFS(Path p) throws Exception {
        List<String> ls = new ArrayList<String>();

        BufferedReader br = new BufferedReader(new InputStreamReader(
                this.hdfs.open(p)));
        String line;
        line = br.readLine();
        while (line != null) {
            line = br.readLine();
            if(line!=null)
                ls.add(line);
        }
        return ls;
     }

   public void setup(Context context) {

        try {
            this.hdfs = FileSystem.get(context.getConfiguration());
            URI[] uris = context.getCacheFiles();
            for (URI uri : uris) {
                if (uri.toString().endsWith("airports.csv")) {
                    this.readAirports(uri);
                }
                if (uri.toString().endsWith("carriers.csv")) {
                    this.readCarriers(uri);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex);
        }

    }

暫無
暫無

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

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