fork download
  1. import org.apache.hadoop.conf.Configuration;
  2. import org.apache.hadoop.fs.FileSystem;
  3. import org.apache.hadoop.fs.Path;
  4.  
  5. public class HDFSOperations {
  6. public static void main(String[] args) throws Exception {
  7. Configuration conf = new Configuration();
  8. FileSystem fs = FileSystem.get(conf);
  9.  
  10. // Create a file
  11. fs.create(new Path("/user/yourusername/newfile.txt"));
  12.  
  13. // Read a file
  14. FSDataInputStream in = fs.open(new Path("/user/yourusername/existingfile.txt"));
  15. byte[] buffer = new byte[1024];
  16. int bytesRead = 0;
  17. while ((bytesRead = in.read(buffer)) != -1) {
  18. System.out.write(buffer, 0, bytesRead);
  19. }
  20. in.close();
  21.  
  22. // Delete a file
  23. fs.delete(new Path("/user/yourusername/filetodelete.txt"), true);
  24.  
  25. fs.close();
  26. }
  27. }-- your code goes here
Success #stdin #stdout #stderr 0.01s 5284KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: near line 1: near "import": syntax error
Error: near line 2: near "import": syntax error
Error: near line 3: near "import": syntax error
Error: near line 5: near "public": syntax error
Error: near line 8: near "FileSystem": syntax error
Error: near line 10: near "/": syntax error
Error: near line 13: near "/": syntax error
Error: near line 15: near "byte": syntax error
Error: near line 16: near "int": syntax error
Error: near line 17: near "while": syntax error
Error: near line 19: unrecognized token: "}"
Error: near line 22: near "/": syntax error
Error: near line 25: near "fs": syntax error
Error: near line 26: unrecognized token: "}"