Pages

Monday 17 March 2014

Insert CSV data to Database using Kettle


Here i would like to share that how to insert csv file's data to database by the help of Kettle .Refer

Configure the environment

1 - Download kettle .click  here

2 - Start Spoon.bat

3 - Select  File ->New->Transformation

4 - Select Input option from left pane

5 - Chose CSV File Input

6 - Double click on CSV File Input and configure as follows


7 - Select Output option from left pane

8 Chose Table Output

9 - Double click on   Table Outputand configure as follows

* Add a Connection by clicking 'New'





        *Browse Table name
*Browse database feild option and Enter feild mapping

10 - Click OK to complete configuration

Now you can save and start the transformation


Friday 7 March 2014

convert csv file to java object

Here i would like to share that how to convert a csv file to java object.

Records in  sample  csv


NPI
Fisrt Name
Middle Name
Last Name
Designation
1000
Ajmal
A
Abdullah
Software Engineer
2000
Jouhar
O
Oravingal
Software Engineer
3000
Eldho
J
John
Software Engineer

Professional.java


/**
 * 
 * @author Jamsheer T
 * 
 */

public class Professional {

String npi;
String name;
String mname;
String lname;
String desig;

public String getNpi() {
return npi;
}

public void setNpi(String npi) {
this.npi = npi;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getMname() {
return mname;
}

public void setMname(String mname) {
this.mname = mname;
}

public String getLname() {
return lname;
}

public void setLname(String lname) {
this.lname = lname;
}

public String getDesig() {
return desig;
}

public void setDesig(String desig) {
this.desig = desig;
}

}

CSVtoJava.java


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 *
 * @author Jamsheer T
 *
 */
public class CSVtoJava {

public void convertCsvToJava() {
Professional profObject = new Professional();
String csvFileToRead = "C:\\Users\\Jamsheer T\\Desktop\\csv\\prof.csv";
BufferedReader br = null;
String line = "";
String splitBy = ",";
boolean initial =true;

try {

br = new BufferedReader(new FileReader(csvFileToRead));
while ((line = br.readLine()) != null) {



// split on comma(',')
String[] details = line.split(splitBy);

// create car object to store values

// add values from csv to car object
profObject.setNpi(details[0]);
profObject.setName(details[1]);
profObject.setMname(details[2]);
profObject.setLname(details[3]);
profObject.setDesig(details[4]);

if(initial==true){
initial=false;
continue;
}
System.out.println("NPI :" + profObject.getNpi());
System.out.println("NAME :" + profObject.getName());
System.out.println("Designation :" + profObject.getDesig());
System.out.println("----------------------------------");

}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

}


Main.java


/**
 * 
 * @author Jamsheer T
 * 
 */

public class Main {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

CSVtoJava csvToJavaObject = new CSVtoJava();
csvToJavaObject.convertCsvToJava();
}

}

OUTPUT

NPI :1000
NAME :Ajmal
Designation :Software Engineer
----------------------------------
NPI :2000
NAME :Jouhar
Designation :Software Engineer

----------------------------------
NPI :3000
NAME :Eldho
Designation :Software Engineer

----------------------------------






Convert CSV File to JSON Object in JAVA


Here i would like to share that how to convert a csv file contents to JSON object  in JAVA

Included Libraries

<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.3.4</version>
</dependency>

Sample Code


package com.test.csvtojson;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 *
 * @author Jamsheer T +91 9846716175
 */
public class CSVtoJson {

public static void main(String[] args) { int i; String line = ""; String fileToParse = "C:\\Users\\Jamsheer T\\Desktop\\csv\\ex.csv"; BufferedReader fileReader = null; boolean initial = true; ArrayList<String> header = new ArrayList<String>(); JSONObject record = new JSONObject(); try { fileReader = new BufferedReader(new FileReader(fileToParse)); while ((line = fileReader.readLine()) != null) { i = 0; String[] tokens = line.split(","); if (initial) { for (String token : tokens) { if (token.startsWith("\"")) { token = token.substring(1); } if (token.endsWith("\"")) { token = token.substring(0, token.length() - 1); } header.add(token); } initial = false; continue; } // Get all tokens available in line for (String token : tokens) { if (token.startsWith("\"")) { token = token.substring(1); } if (token.endsWith("\"")) { token = token.substring(0, token.length() - 1); } record.put(header.get(i).toString(), token); i++; if (i == header.size()) break; } System.out.println(record); } } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } } }