java上传本地文件到HDFS简单demo

本文整理了上传文件到hdfs的三种java代码,均已测试通过

1、创建maven项目 


2、pom依赖


    
        junit
        junit
        3.8.1
        test
    
    
        org.apache.hadoop
        hadoop-client
        2.7.3
    
    
        org.apache.hadoop
        hadoop-common
        2.7.3
    
    
        org.apache.hadoop
        hadoop-hdfs
        2.7.3
    
    
        org.apache.hbase
        hbase-client
        0.96.1-hadoop2
    



    
        
            src/main/resources
            true
        
        
            src/main/java
            true
            
                **/*.java
            
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.6
                    1.6
                    UTF-8
                
            
        
    

    
        
            org.apache.maven.plugins
            maven-shade-plugin
            
                
                    package
                    
                        shade
                    
                    
                        
                            
                                *:*
                            
                                META-INF/*.SF
                                META-INF/*.DSA
                                META-INF/*.RSA
                            
                            
                        
                        
                            
                                ${mainClass}
                            
                            
                                META-INF/spring.handlers
                            
                            
                                META-INF/spring.schemas
                            
                            
                                META-INF/spring.tooling
                            
                        
                    
                
            
        
    


3、java代码

代码一:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class CopyFile {
public static void main(String[] args) {
    Configuration conf = new Configuration();
    String localDir = "/home/hdfs/files/test.txt";
    String hdfsDir = "hdfs://server:8020/bbbb";
    try{
            Path localPath = new Path(localDir);
            Path hdfsPath = new Path(hdfsDir);
            FileSystem hdfs = FileSystem.get(conf);
            if(!hdfs.exists(hdfsPath)){
                 hdfs.mkdirs(hdfsPath);
             }
             hdfs.copyFromLocalFile(localPath, hdfsPath);
         }catch(Exception e){
         e.printStackTrace();
         }
    }
}
代码二:

package my.test;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class UploadSingle {
 public static void main(String[] args) throws URISyntaxException, IOException{
     Configuration conf = new Configuration();
     URI uri = new URI("hdfs://server:8020");
     FileSystem fs = FileSystem.get(uri,conf);
     Path resP = new Path("/home/hdfs/files/test.txt");
     Path destP = new Path("/aaaaaa");
     if(!fs.exists(destP)){
          fs.mkdirs(destP);
     }
    fs.copyFromLocalFile(resP, destP);
    fs.close();
  }
}

代码三:

import java.io.IOException;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
//上传本地文件到HDFS
public class CopyFile {
  public static void main(String[] args) throws IOException {
      //获取配置文件信息
      Configuration conf = new Configuration();
      conf.addResource(new Path("conf/core-site.xml"));
      //获取文件系统
      FileSystem hdfs = FileSystem.get(conf);
      //文件名称
      Path src = new Path("/home/hdfs/files/test.txt");
      Path dst = new Path("hdfs://server:8020/cccc");
      if(!hdfs.exists(dst)){
          hdfs.mkdirs(dst);
      }
      hdfs.copyFromLocalFile(src, dst);
      System.out.println("Upload to " + conf.get("fs.default.name"));
      FileStatus files[] = hdfs.listStatus(dst);
      for(FileStatus file:files){
          System.out.println(file.getPath());
      }
  }
}
以上各段代码,其中,

if(!hdfs.exists(dst)){
hdfs.mkdirs(dst);
}

这段代码必须存在,否则通过 hdfs dfs -ls /cccc命令查看cccc等指定路径下的文件列表时显示不出text.txt文件,

而是将txt中的文字内容直接存入了此路径下,通过 hdfs dfs -cat /cccc命令可以查看文字内容。


4、在eclipse项目上右击,选择run as–》maven install,生成jar包,将jar包上传到服务器上。

5、运行jar包 

unix代码: 

su hdfs 
$ hadoop jar path/name.jar classname 
其中,path是jar包的路径,name是项目名称,classname是类名。

你可能感兴趣的:(学习笔记)