Hadoop基本API运用:
根据Hadoop提供的API我们可以完成基本的文件上传、删除、重命名等操作。在操作之前我们需要将所安装的Hadoop包加入到自己的工程当中,此外如果使用Eclipse来开发Hadoop,必须将其开发包包含进来。
一,将文件上传到hdfs中
public static void main(String[]args)throws Exception{
Configuration conf = new Configuration();//用来配置参数的类对象
conf.set("mapred.job.tracker", "192.168.233.129:9001");//设置mapreduce端口号,指定datanode所在的位置
conf.set("fs.default.name", "hdfs://192.168.233.129:9000");//设置hdfs的地址,也就是namenode的安装位置
FileSystem hdfs = FileSystem.get(conf);//创建hdfs对象
Path src = new Path("C:\\Users\\FFL\\Desktop\\cl.txt");//设置上传文件所在的路径
Path dst = new Path("/hbase");//指定上传文件在hdfs中所处的位置
hdfs.copyFromLocalFile(src, dst);//用copyfromlocalfile函数将文件上传到指定位置。
System.out.println("Upload to"+" "+conf.get("fs.default.name"));//显示文件上传到的hdfs的名字
FileStatus files[] = hdfs.listStatus(dst);//获得刚上传文件所处的hdfs位置
for(FileStatus file:files){//将刚获得的位置处的所有文件便利出来
System.out.println(file.getPath());
}
二,创建hdfs文件
//创建HDFS文件的代码
/* public static void main(String[] args)throws Exception
{
Configuration conf = new Configuration(); //hdfs配置类对象
conf.set("mapred.job.tracker", "192.168.233.129:9001");//设置mapreduce端口号,指定datanode所在的位置
conf.set("fs.default.name", "hdfs://192.168.233.129:9000");//设置hdfs的地址,也就是namenode的安装位置
byte[] buff="hello world!".getBytes();//定义字节对象
FileSystem hdfs = FileSystem.get(conf);//创建hdfs对象
Path dfs = new Path("/usr/lin");//设定路径
FSDataOutputStream outputStream = hdfs.create(dfs);//
outputStream.write(buff,0,buff.length);
}
三,重命名hdfs文件名字
//重新命名hdfs文件名字
/* public static void main(String[]args)throws Exception
{
Configuration conf = new Configuration();
conf.set("mapred.job.tracker", "192.168.233.129:9001");//设置mapreduce端口号,指定datanode所在的位置
conf.set("fs.default.name", "hdfs://192.168.233.129:9000");//设置hdfs的地址,也就是namenode的安装位置
FileSystem hdfs = FileSystem.get(conf);
Path frpath = new Path("/usr/local/cl.txt");
Path topath = new Path("/usr/local/lin.txt");
hdfs.rename(frpath,topath);
}*/
四,删除hdfs上的文件
//删除hdfs上的文件
/* public static void main(String[]args)throws Exception
{
Configuration conf = new Configuration();
conf.set("mapred.job.tracker", "192.168.233.129:9001");
conf.set("fs.default.name", "hdfs://192.168.233.129:9000");
FileSystem hdfs = FileSystem.get(conf);
Path f = new Path("/usr/local/lin.txt");
boolean isDeleted = hdfs.delete(f,false);
System.out.println("delete?"+isDeleted);
//hdfs.delete(f);
}*/
五,查看hdfs上文件的修改时间
//查看hdfs文件的最后修改时间代码
/* public static void main(String[]args)throws Exception
{
Configuration conf = new Configuration();
conf.set("mapred.jpb.tracker", "192.168.233.129:9001");
conf.set("fs.default.name", "hdfs://192.168.233.129:9000");
FileSystem hdfs = FileSystem.get(conf);
Path fpath = new Path("/user/root/input/1.gml");
FileStatus filestatus = hdfs.getFileStatus(fpath);
long modificationTime = filestatus.getModificationTime();
System.out.println("Modification time is: "+modificationTime);
//cl.txt 的 Modification time is: 1416320388525
//l.gml 的 Modification time is: 1415000732734
}
六,查看hdfs中文件是否存在
//查看某个hdfs文件是否存在
public static void main(String[]args)throws Exception
{
Configuration conf = new Configuration();
conf.set("mapred.job.tracker","192.168.233.129:9001");
conf.set("fs.default.name","hdfs://192.168.233.129:9000");
FileSystem hdfs = FileSystem.get(conf);
Path findf = new Path("/user/root/input/fanga.txt");
boolean isExists = hdfs.exists(findf);
System.out.println("Exits?"+isExists);
}
七,查看文件在hdfs集群中位置
//查找某个文件在hdfs集群的位置
public static void main(String[]args)throws Exception
{
Configuration conf = new Configuration();
conf.set("mapred.job.tracker", "192.168.233.129:9001");
conf.set("fs.default.name", "hdfs://192.168.233.129:9000");
FileSystem hdfs = FileSystem.get(conf);
Path fpath = new Path("/user/root/input/cl.txt");
FileStatus filestatus = hdfs.getFileStatus(fpath);
BlockLocation[] blkLocation = hdfs.getFileBlockLocations(filestatus, 0, filestatus.getLen());
int blockLen = blkLocation.length;
for(int i=0;i<blockLen;i++)
{
String[]hosts = blkLocation[i].getHosts();
System.out.println("block "+i+" location: "+hosts[i]);
}
}*/