java读取内存中的csv文件,跳过第一行

package ApacheCommonCSV;
import junit.framework.TestCase;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.List;

public class AppTest extends TestCase
{

    public void testApp(){
        try {
            FileReader filereader = new FileReader("/Users/student/Desktop/file.csv");
            BufferedReader bufferedReader = new BufferedReader(filereader);
            bufferedReader.readLine();// try-catch omitted
            CSVFormat format = CSVFormat.DEFAULT.withDelimiter(',');
            CSVParser parser = new CSVParser(bufferedReader, format);
            List<CSVRecord> records = parser.getRecords();//跳过第一行所有行的记录
            for(int i=0;i<records.size();i++){
                double[] temp=new double[records.get(i).size()];
                for(int j=0;j<records.get(i).size();j++){
                    temp[j]=Double.parseDouble(records.get(i).get(j));
                    //System.out.print(temp[j]+" ");
                }
                for(int ii=0;ii<temp.length;ii++){
                    System.out.print(temp[ii]+"  ");
                }
                System.out.println();
            }
        }catch (Exception e){
            System.out.print("please check your upload");
        }

    }
}

<dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-csv</artifactId>
      <version>1.5</version>
</dependency>
在 Hadoop 读取和处理 CSV 文件,可以使用 Hadoop 自带的 `TextInputFormat` 以及 `LineRecordReader` 类来实现。下面是一个简单的示例代码,可以读取 CSV 文件并提取信息: ```java import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.*; import org.apache.hadoop.mapreduce.lib.input.*; import org.apache.hadoop.mapreduce.lib.output.*; import org.apache.hadoop.util.*; public class CsvReader extends Configured implements Tool { public static class CsvMapper extends Mapper<LongWritable, Text, Text, Text> { @Override public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); String[] fields = line.split(","); String name = fields[0]; String age = fields[1]; String city = fields[2]; String outputValue = "Age: " + age + ", City: " + city; context.write(new Text(name), new Text(outputValue)); } } public int run(String[] args) throws Exception { if (args.length != 2) { System.out.println("Usage: CsvReader <input path> <output path>"); return -1; } Configuration conf = getConf(); Job job = Job.getInstance(conf, "CsvReader"); job.setJarByClass(CsvReader.class); job.setMapperClass(CsvMapper.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); job.setInputFormatClass(TextInputFormat.class); job.setOutputFormatClass(TextOutputFormat.class); FileInputFormat.setInputPaths(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); boolean success = job.waitForCompletion(true); return success ? 0 : 1; } public static void main(String[] args) throws Exception { int exitCode = ToolRunner.run(new CsvReader(), args); System.exit(exitCode); } } ``` 在上面的示例代码,首先定义了一个 `CsvMapper` 类,用来处理 CSV 文件的每一行数据。在 `map` 方法,将每一行数据按照逗号分隔符进行拆分,然后提取出需要的数据,最后将姓名作为 key,年龄和城市作为 value 写入输出。 在 `run` 方法,首先获取输入和输出路径,然后设置作业的 Mapper 类、输出 Key 和 Value 的类型、输入和输出格式等属性。接着将输入和输出路径设置到作业,并等待作业执行完成。最后,通过 `ToolRunner` 来运行 `CsvReader` 类。 需要注意的是,这里只是一个示例代码,实际情况,你需要根据自己的需求来修改代码。例如,如果你的 CSV 文件有标题行,那么需要在 Mapper 类进行特殊处理,跳过第一行标题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值