Attachment 'HadoopDFSFileReadWrite.java'

Download

   1 /**
   2  * Licensed to the Apache Software Foundation (ASF) under one
   3  * or more contributor license agreements.  See the NOTICE file
   4  * distributed with this work for additional information
   5  * regarding copyright ownership.  The ASF licenses this file
   6  * to you under the Apache License, Version 2.0 (the
   7  * "License"); you may not use this file except in compliance
   8  * with the License.  You may obtain a copy of the License at
   9  *
  10  *     http://www.apache.org/licenses/LICENSE-2.0
  11  *
  12  * Unless required by applicable law or agreed to in writing, software
  13  * distributed under the License is distributed on an "AS IS" BASIS,
  14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15  * See the License for the specific language governing permissions and
  16  * limitations under the License.
  17  */
  18 
  19 import java.io.File;
  20 import java.io.IOException;
  21 
  22 import org.apache.hadoop.conf.Configuration;
  23 import org.apache.hadoop.fs.FileSystem;
  24 import org.apache.hadoop.fs.FSDataInputStream;
  25 import org.apache.hadoop.fs.FSDataOutputStream;
  26 import org.apache.hadoop.fs.Path;
  27 
  28 public class HadoopDFSFileReadWrite {
  29   
  30   static void usage () {
  31     System.out.println("Usage : HadoopDFSFileReadWrite <inputfile> <output file>");
  32     System.exit(1);
  33   }
  34   
  35   static void printAndExit(String str) {
  36     System.err.println(str);
  37     System.exit(1);
  38   }
  39 
  40   public static void main (String[] argv) throws IOException {
  41     Configuration conf = new Configuration();
  42     FileSystem fs = FileSystem.get(conf);
  43 
  44     if (argv.length != 2)
  45       usage();
  46     
  47     // Hadoop DFS deals with Path
  48     Path inFile = new Path(argv[0]);
  49     Path outFile = new Path(argv[1]);
  50    
  51     // Check if input/output are valid
  52     if (!fs.exists(inFile))
  53       printAndExit("Input file not found");
  54     if (!fs.isFile(inFile))
  55       printAndExit("Input should be a file");
  56     if (fs.exists(outFile))
  57       printAndExit("Output already exists");
  58 
  59     // Read from and write to new file
  60     FSDataInputStream in = fs.open(inFile);
  61     FSDataOutputStream out = fs.create(outFile);
  62     byte buffer[] = new byte[256];
  63     try {
  64       int bytesRead = 0;
  65       while ((bytesRead = in.read(buffer)) > 0) {
  66         out.write(buffer, 0, bytesRead);
  67       }
  68     } catch (IOException e) {
  69       System.out.println("Error while copying file");
  70     } finally {
  71       in.close();
  72       out.close();
  73     }
  74   }
  75 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.