(1)pom.xml
(2)创建类 编写内容
1)复制配置文件到项目中
cp /opt/modules/hadoop-2.5.0-cdh5.3.6/etc/hadoop/core-site.xml
/opt/modules/hadoop-2.5.0-cdh5.3.6/etc/hadoop/hdfs-site.xml /opt/tool/workspace/hadoophdfs/src/main/resources/
2)配置log4j 直接复制hahoop已存在的到现有项目就行了
cp /opt/modules/hadoop-2.5.0-cdh5.3.6/etc/hadoop/log4j.properties
/opt/tool/workspace/hadoophdfs/src/main/resources/
3)添加类Hdfs
package com.my.hadoop.hadoophdfs; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IOUtils; public class Hdfs { /** * 得到文件系统 * @return */ public static FileSystem getFileSystem(){ //core-site.xml core-default.xml hdfs-site.xml hdfs-default.xml //读取上边列出的文件内容 Configuration conf = new Configuration(); FileSystem fs = null; try { fs = FileSystem.get(conf); } catch (IOException e) { e.printStackTrace(); } return fs; } /** * 读取指定路径下的文件 * @param fileName */ public static void read(String fileName){ FileSystem fs = getFileSystem(); //read path Path readPath=new Path(fileName); FSDataInputStream inStream =null; try { inStream = fs.open(readPath); //read 输出到控制台System.out IOUtils.copyBytes(inStream, System.out, 4096,false); } catch (IOException e) { e.printStackTrace(); }finally{ IOUtils.closeStream(inStream); } } /** * 将本地文件 上传到HDFS * @param sPath 本地文件地址 * @param dPath 目标文件地址 */ public static void Write(String sPath,String dPath){ FileSystem fs =getFileSystem(); //目标文件 String putFileName=dPath; Path writePath = new Path(putFileName); FSDataOutputStream outStream = null; FileInputStream inStream = null; try { //输出流 outStream= fs.create(writePath); //输入流 inStream = new FileInputStream(new File(sPath)); //流操作 IOUtils.copyBytes(inStream, outStream, 4096,false); } catch (IOException e) { e.printStackTrace(); }finally{ IOUtils.closeStream(inStream); IOUtils.closeStream(outStream); } } public static void main(String[] args) { //read("/user/liming/mapreduce/wordcount/input/test.input"); } }