In this tutorial i will list basic HDFS needed comamnds like:
Connectin to the filse system,creating directory, copy /delete/create files etc.
1. Connecting to HDFS file system:
Configuration config = new Configuration();config.set("fs.default.name","hdfs://127.0.0.1:9000/");
FileSystem dfs = FileSystem.get(config);
2. Creating directory
Path src = new Path(dfs.getWorkingDirectory()+"/"+"rami");dfs.mkdirs(src);
3. Delete directory or file:
Path src = new Path(dfs.getWorkingDirectory()+"/"+"rami");Dfs.delete(src);
4. Copy files from local FS o HDFS and back:
Path src = new Path("E://HDFS/file1.txt");Path dst = new Path(dfs.getWorkingDirectory()+"/directory/");
dfs.copyFromLocalFile(src, dst);
Or Back :
dfs.copyToLocalFile(src, dst);
Note : destination should be a path object that contains the directory to copy the source fiel to.
Source should be a path object that contains path to the file including thefile itself.
5.Create file:
Path src = new Path(dfs.getWorkingDirectory()+"/rami.txt");dfs.createNewFile(src);
6. Reading file:
Path src = new Path(dfs.getWorkingDirectory()+"/rami.txt");FSDataInputStream fs = dfs.open(src);
String str = null;
while ((str = fs.readline())!= null)
{
System.out.println(str);
}
7.Writing file:
Path src = new Path(dfs.getWorkingDirectory()+"/rami.txt");FSDataOutputStream fs = dfs.create(src);
byte[] btr = new byte[]{1,2,3,4,5,6,7,8,9};
fs.write(btr);
fs.close();
No comments:
Post a Comment