mcs*_*vio 4 java csv hadoop apache-spark
我的Hadoop集群上有一个大的CSV文件.该文件的第一行是"标题"行,由字段名称组成.我想对这个标题行进行操作,但我不想处理整个文件.此外,我的程序是用Java编写的,并使用Spark.
在Hadoop集群上只读取大型CSV文件的第一行的有效方法是什么?
您可以使用FileSystem类和朋友访问hdfs文件:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DistributedFileSystem;
DistributedFileSystem fileSystem = new DistributedFileSystem();
Configuration conf = new Configuration();
fileSystem.initialize(new URI("hdfs://namenode-host:54310"), conf);
FSDataInputStream input = fileSystem.open(new Path("/path/to/file.csv"));
System.out.println((new BufferedReader(new InputStreamReader(input))).readLine());
Run Code Online (Sandbox Code Playgroud)
此代码不会使用MapReduce,并且将以合理的速度运行.
| 归档时间: |
|
| 查看次数: |
2274 次 |
| 最近记录: |