使用Hadoop的java api 针对HDFS进行文件上传、创建、重命名、删除操作:
package hadoop.hdfs; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.net.URISyntaxException; 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; /** * 使用FileSystem类 * @author yehao * */ public class App2 { private final static String HDFS_PATH = "hdfs://hadoop:9000/hello"; private final static String DIR_PATH="/d100"; private final static String FILE_PATH = "/d100/f100"; /** * * @param args * @throws URISyntaxException * @throws IOException */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub final FileSystem fileSystem = FileSystem.get(new URI(HDFS_PATH), new Configuration()); //创建文件夹 makeDir(fileSystem); //上传文件 uploadFile(fileSystem); //下载文件 downFile(fileSystem); //删除文件夹 deleteFile(fileSystem); } private static void deleteFile(final FileSystem fileSystem) throws IOException { fileSystem.delete(new Path(DIR_PATH), true); } private static void downFile(final FileSystem fileSystem) throws IOException { final FSDataInputStream in = fileSystem.open(new Path(FILE_PATH)); IOUtils.copyBytes(in, System.out, 1024, true); } private static void makeDir(final FileSystem fileSystem) throws IOException { fileSystem.mkdirs(new Path(DIR_PATH)); } private static void uploadFile(FileSystem fileSystem) throws Exception{ final FSDataOutputStream out = fileSystem.create(new Path(FILE_PATH)); final InputStream in = new FileInputStream("E:/test"); IOUtils.copyBytes(in, out, 1024, true); } }