代码实例
package oa.epoint.com.hdfs
import java.io.FileOutputStream
import java.io.OutputStream
import java.net.URI
import java.util.Scanner
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.FSDataInputStream
import org.apache.hadoop.fs.FileSystem
import org.apache.hadoop.fs.Path
import org.apache.hadoop.io.IOUtils
public class HdfsTest {
public static void upload() throws Exception {
Configuration conf = new Configuration()
URI uri = new URI("hdfs://100.2.5.1:8020")
FileSystem fs = FileSystem.get(uri, conf, "HDFS")
Path resP = new Path("D://files/file1.txt")
Path destP = new Path("/tmp")
if (!fs.exists(destP)) {
fs.mkdirs(destP)
}
fs.copyFromLocalFile(resP, destP)
fs.close()
System.out.println("***********************")
System.out.println("上传成功!")
}
public static void download() throws Exception {
Configuration conf = new Configuration()
String dest = "hdfs://100.2.5.1/tmp/file1.txt"
String local = "D://files/downloads/file1.txt"
FileSystem fs = FileSystem.get(URI.create(dest), conf, "hdfs")
FSDataInputStream fsdi = fs.open(new Path(dest))
OutputStream output = new FileOutputStream(local)
IOUtils.copyBytes(fsdi, output, 4096, true)
System.out.println("***********************")
System.out.println("下载成功!")
}
public static void main(String[] args) throws Exception {
String arg = ""
Scanner scanner = new Scanner(System.in)
System.out.println("1、上传文件")
System.out.println("2、下载文件")
boolean isconture = true
while (isconture) {
arg = scanner.next()
if ("1".equals(arg)) {
upload()
} else if ("2".equals(arg)) {
download()
}
System.out.println("是否继续 y/n")
if (scanner.next().equals("n")) {
isconture = false
}
}
scanner.close()
}
}