windows下JAVA开发操作hadoop的方法
来源:互联网 发布:淘宝女生衣服 编辑:程序博客网 时间:2024/06/06 20:45
先说一下具体情况,
hadoop版本2.4X 本地使用的 eclipse 开发,操作虚拟机中的hadoop系统
第一次开发估计会遇到不少报错信息,如果有疑问 贴出来,可以解答
package cn.itcast.hadoop.hdfs;import java.io.IOException;import java.net.URI;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FSDataInputStream;import org.apache.hadoop.fs.FSDataOutputStream;import org.apache.hadoop.fs.FileStatus;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IOUtils;public class MyHdfsDao { static String hdfsPath="hdfs://hadoop01:9000/";public static void main(String[] args) throws IOException { Configuration conf = new Configuration();conf.set("fs.defaultFS", "hdfs://hadoop01:9000/");System.setProperty("hadoop.home.dir", "E:/hadoop");//设置本地hadoop文件位置System.setProperty("HADOOP_USER_NAME", "firefly");//设置上传用户的信息名ls("/",conf);//查看信息String javacreat="/javaCreat/haha";// mkdirs(javacreat,conf);//创建文件夹操作 //rmr(javacreat,conf);//删除操作//copyToHDFS("c:/movies.xml","/movies.xml",conf);//上传文件//cat("/movies.xml",conf);//查看文件操作//downLoad("c:/movies2.xml","/movies.xml",conf);//下载操作//createFile("Hello world!!","/movies33.xml" ,conf);//创建文件 Path oldName=new Path("/movies33.xml");//原文件 Path newName=new Path("/movies44.xml");//新文件名 rename(oldName,newName,conf); //改名 } private static void rename(Path oldName, Path newName,Configuration conf) throws IOException { FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf); fs.rename(oldName, newName); }private static void createFile(String content,String remote, Configuration conf) throws IOException { FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf); byte[] buff = content.getBytes(); FSDataOutputStream os = null; try { os = fs.create(new Path(remote)); os.write(buff, 0, buff.length); System.out.println("Create: " + remote); } finally { if (os != null) os.close(); } fs.close();}private static void downLoad(String local,String remote, Configuration conf) throws IOException { Path path = new Path(remote); FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf); fs.copyToLocalFile(path, new Path(local)); System.out.println("download: from" + remote + " to " + local); fs.close();}private static void cat(String remoteFile,Configuration conf) throws IOException { Path path = new Path(remoteFile); FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf); FSDataInputStream fsdis = null; System.out.println("cat: " + remoteFile); try { fsdis =fs.open(path); IOUtils.copyBytes(fsdis, System.out, 4096, false); } finally { IOUtils.closeStream(fsdis); fs.close(); }}private static void copyToHDFS(String local,String remote, Configuration conf) throws IOException { FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf); fs.copyFromLocalFile(new Path(local), new Path(remote)); System.out.println("copy from: " + local + " to " + remote); fs.close();}private static void rmr(String javacreat,Configuration conf) throws IOException { Path path = new Path(javacreat); FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf); fs.deleteOnExit(path); System.out.println("Delete: " + javacreat); fs.close();}private static void mkdirs(String javacreat,Configuration conf) throws IOException { Path path = new Path(javacreat); FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf); if (!fs.exists(path)) { fs.mkdirs(path); System.out.println("Create: " + javacreat); } fs.close();}public static void ls(String folder,Configuration conf) throws IOException { Path path = new Path(folder); FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf); FileStatus[] list = fs.listStatus(path); System.out.println("ls: " + folder); System.out.println("=========================================================="); for (FileStatus f : list) { System.out.printf("name: %s, folder: %s, size: %d\n b", f.getPath(), f.isDir(), f.getLen()); } System.out.println("一共有文件数量"+list.length); System.out.println("=========================================================="); fs.close(); }}
0 0
- windows下JAVA开发操作hadoop的方法
- Windows的eclipse下开发Hadoop
- windows下eclipse开发hadoop的坑
- windows下安装hadoop的简单方法
- 在Windows下开发hadoop的MapReduce的坑!!!
- windows下搭建hadoop开发环境遇到的问题
- Windows下基于Eclipse的Hadoop开发环境完全配置
- windows下基于Eclipse的Hadoop应用开发环境配置
- Windows下开发Hadoop程序遇到的坑及解决方案
- Java API操作HA方式下的Hadoop
- Hadoop Windows下的问题
- windows下hadoop的配置
- windows下的hadoop安装
- Windows下Cygwin环境的Hadoop安装(4)- 在Eclipse中建立hadoop开发环境
- Windows下Cygwin环境的Hadoop安装(4)- 在Eclipse中建立hadoop开发环境
- Hadoop-Windows下的Eclipse开发环境搭建,远程虚拟机Hadoop服务器
- windows下搭建hadoop开发环境(Eclipse)
- Windows下安装Hadoop开发环境
- opencv图像处理之图像卷积
- 在Activity或者Fragment中刷新另一个Activity或者Fragment
- IE6下的一些常见问题
- final关键字与Java编译器的小坑
- Android开发中遇到的系统组件bug
- windows下JAVA开发操作hadoop的方法
- java适配器模式(二)
- 多线程断点下载文件
- .classPath .settting .project 文件讲解
- 新手如何在window下搭建wamp(php)开发环境
- JPA中@JoinTable和@JoinColumn注解的使用
- Oracle 110个常用函数
- 轮播图片切换
- Opencv 任意比例旋转图像