CSV数据文件后面添加属性的方法【大数据相关】

本文以csv文件格式的点边文件,程序在下面,使用Java8中对于时间的用法

package com.liuchj;

import java.io.*;
import java.text.DecimalFormat;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * @author liuchj
 * @create 2020-04-17 下午 10:47
 */
public class CsvAddProperties {

    public static void main(String[] args) {
        String sourcePath = "test1.csv";
        String targetPath = "result_test1.csv";

        //开始时间
        Instant beginTime = Instant.now();
        //执行添加属性方法
        addProperties(sourcePath,targetPath);
        //结束时间
        Instant endTime = Instant.now();
        
        long hours = ChronoUnit.HOURS.between(beginTime, endTime);
        long minutes = ChronoUnit.MINUTES.between(beginTime, endTime);
        long seconds = ChronoUnit.SECONDS.between(beginTime, endTime);
        System.out.println("总共消耗时间为:"+hours+"小时,"+minutes+"分钟,"+seconds+"秒" );
       }
    /**
     * 生成属性数据
     * @param intNum    int类型属性个数
     * @param doubleNum double类型属性个数
     * @param StringNum StringNum类型属性个数
     * @return
     */
    public static List<String> generateData(int intNum, int doubleNum, int StringNum) {
        List<String> list = new ArrayList<>();
        //生成随机的int类型数据
        for (int i = 0; i < intNum; i++) {
            Random random = new Random();
            int intRandom = random.nextInt(10000);
            int intProp = intRandom + 1;
            list.add(0, intProp + "");
        }

        //生成随机的double类型数据
        for (int i = 0; i < doubleNum; i++) {
            int y = 1000000;
            double v = y - Math.random() * (y - 1);
            DecimalFormat df = new DecimalFormat("0.00");
            String doubleProp = df.format(v);
            list.add(1, doubleProp);
        }

        //生成随机的String字符串,长度为10
        for (int i = 2; i < StringNum; i++) {
            UUID uuid = UUID.randomUUID();
            String insertChar = uuid.toString().substring(0, 1);
            String stringNumber = uuid.toString().substring(0, 11).replace("-", insertChar);
            list.add(i, stringNumber);
        }
        return list;
    }

/**
* 添加属性的方法
* @param sourcePath
* @param targetPath
*/
    public static void addProperties(String sourcePath, String targetPath) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedReader br = null;
        BufferedWriter bw = null;


        try {
            fis = new FileInputStream(new File(sourcePath));
            fos = new FileOutputStream(new File(targetPath));

            br = new BufferedReader(new InputStreamReader(fis));
            bw = new BufferedWriter(new OutputStreamWriter(fos));
            String oldContent = "";
            StringBuilder lineContent = null;

            while ((oldContent = br.readLine()) != null) {
                //设置1个int型,1个double型,8个String类型
                List<String> suffixList = generateData(1, 1, 10);
                lineContent = new StringBuilder(oldContent + ",");
                for (int i = 0; i < suffixList.size(); i++) {
                    lineContent = lineContent.append(suffixList.get(i)).append(",");
                }
                String resultLine = lineContent.substring(0, lineContent.length() - 1);
                bw.write(resultLine + "\n");

            }
        } catch (Exception e) {
            System.out.println("===========run exception=============");
            e.printStackTrace();
        } finally {
            try {
                bw.close();
                br.close();
                fis.close();
                fos.close();

            } catch (IOException e) {
                System.out.println("=============Stream close exception=============");
            } finally {
                System.out.println("=============Stream execute success===============");
                System.out.println("==================================================");
                System.out.println("=============Stream  close  success===============");
            }

        }
    }
    }

使用eclipse开发,可以生成jar文件,大数据量数据需要放到linux系统运行,在jar包的目录中运行命令:java -cp jar包名称 程序全类名

你可能感兴趣的:(【大数据】)