ITechShree-Data-Analytics-Technologies

CSV File reading using Java and Scala

CSV file reading is one of the important technique used frequently in Data analytics Programming.

Here I am sharing my work which may help you.


Here we have one csv file stored in the following path where first column is date field when employee was registered.I am going to change the date format by reading this file.


    /home/hadoop_local/employee.csv

    eid, ename, phone_no, loc

    "Mon, 26 Nov 2019 09:34:11 +0000","1", surya, 9373365498, DEL

    "Tue, 26 Dec 2018 09:34:11 +0000",2, animesh, 9365845214, KOL

    "Mon, 20 May 2018 09:34:11 +0000 ",3, sameer, SC, 2010569823,BANG

    "Wed, 18 Jun 2018 09:34:11 +0000",4, karthik, TP, 9010528694,KOL


Using Java Programming:


import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;



public class CSVreadClass {

public static void main(String args[]){

String csvFileStored = "\home\hadoop_local\employee.csv";

BufferedReader br_var = null;

String line = "";

String cvsSplitByOperator = ",";

Date t=new Date();


try {


br = new BufferedReader(new FileReader(csvFileStored));

while ((line = br_var.readLine()) != null) {


// use comma as separator

String[] st= line.split(cvsSplitByOperator);

st[0]=st[0].replaceAll("\"","");

System.out.println(st[0]);

SimpleDateFormat format_time = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");

try {

time = format_time.parse(st[0]);

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

ft.applyPattern("mm-dd-yyyy hh:mm:ss a");

System.out.println(format_time .format(time));

}


} catch (FileNotFoundException er) {

er.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (br_var != null) {

try {

br_var.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}


System.out.println("Completed");

}


}




Using Scala Programming:


import java.time.LocalDate

import java.time.LocalDateTime

import java.time.format.DateTimeFormatter

import java.time.format.DateTimeFormatter

object CSVDemo extends App {

val buffer = io.Source.fromFile("\home\hadoop_local\employee.csv")

for (line <- buffer.getLines) {

val columns = line.split(",").map(_.trim)

val dt=columns(0);

// do whatever you want with the columns attributes

try {

val date= LocalDateTime.parse(dt, DateTimeFormatter.ofPattern("E, dd MMM yyyy HH:mm:ss Z"))

println(date.format(DateTimeFormatter.ofPattern("mm-dd-yyyy hh:mm:ss a")))

}

}

print("Completed")

buffer.close

}


Happy Coding!!

See you in next blog!

Post a Comment

0 Comments