这篇来介绍一下往HDFS写数据与查看文件信息的操作.
1.读取本地文件,并写入至HDFS.
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import java.io.*;
import java.net.URI;
public class CopyToHdfs {
public static void main(String[] args) throws Exception{
String hdfsPath = "hdfs://192.168.133.153:9000/test/write/hdfs-site.xml";//hdfs位置
String localPath = "E:\\Hadoop\\hdfs-site.xml";//本地磁盘位置
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(URI.create(hdfsPath), configuration);
FSDataOutputStream out = fs.create(new Path(hdfsPath));//创建一个输出流
InputStream in = new FileInputStream(new File(localPath));//从本地读取文件
IOUtils.copyBytes(in, out, 100, true);
System.out.println("上传完毕");
}
}
之后使用在虚拟机使用 hdfs dfs -ls /test/write 命