Read a Tab Separated Values File
Updated: Oct 29, 2023
This example shows how to read TSV (Tab-Separated Values) files, allowing you to extract data from such files with ease. It can be used for a range of applications, including log analysis, data conversion, and data import tasks.
Input TSV File
Account LastName FirstName Balance CreditLimit AccountCreated Rating 101 Reeves Keanu 9315.45 10000.00 1/17/1998 A 312 Butler Gerard 90.00 1000.00 8/6/2003 B 868 Hewitt Jennifer Love 0 17000.00 5/25/1985 B 761 Pinkett-Smith Jada 49654.87 100000.00 12/5/2006 A 317 Murray Bill 789.65 5000.00 2/5/2007 C 101 Reeves Keanu 9315.45 10000.00 1998-1-17 A 312 Butler Gerard 90.00 1000.00 2003-8-6 B 101 Hewitt Jennifer Love 0 17000.00 1985-5-25 B 312 Pinkett-Smith Jada 49654.87 100000.00 2006-12-5 A 317 Murray Bill 789.65 5000.00 2007-2-5 C 317 Murray Bill 1 5000.00 2007-2-5 A
Java Code Listing
package com.northconcepts.datapipeline.examples.cookbook;
import com.northconcepts.datapipeline.core.DataReader;
import com.northconcepts.datapipeline.core.StreamWriter;
import com.northconcepts.datapipeline.csv.CSVReader;
import com.northconcepts.datapipeline.job.Job;
import java.io.File;
public class ReadATsvFile {
public static void main(String[] args) {
DataReader reader = new CSVReader(new File("example/data/input/input-file.tsv"))
.setFieldSeparator("\t")
.setFieldNamesInFirstRow(true);
Job.run(reader, new StreamWriter(System.out));
}
}
Code Walkthrough
- CSVReader is created corresponding to the input file
input-file.tsv. - In tab-separated values (TSV) files,
\t(tab) is used as a field separator between values. So this is declared using.setFieldSeparator("\t")method. - The
setFieldNamesInFirstRow(true)method is invoked to specify that the names specified in the first row should be used as field names. - Job.run() is used to transfer the data from
readertoStreamWriter(System.out). See how to compile and run data pipeline jobs.
Console Output
-----------------------------------------------
0 - Record {
0:[Account]:STRING=[101]:String
1:[LastName]:STRING=[Reeves]:String
2:[FirstName]:STRING=[Keanu]:String
3:[Balance]:STRING=[9315.45]:String
4:[CreditLimit]:STRING=[10000.00]:String
5:[AccountCreated]:STRING=[1/17/1998]:String
6:[Rating]:STRING=[A]:String
}
-----------------------------------------------
1 - Record {
0:[Account]:STRING=[312]:String
1:[LastName]:STRING=[Butler]:String
2:[FirstName]:STRING=[Gerard]:String
3:[Balance]:STRING=[90.00]:String
4:[CreditLimit]:STRING=[1000.00]:String
5:[AccountCreated]:STRING=[8/6/2003]:String
6:[Rating]:STRING=[B]:String
}
-----------------------------------------------
2 - Record {
0:[Account]:STRING=[868]:String
1:[LastName]:STRING=[Hewitt]:String
2:[FirstName]:STRING=[Jennifer Love]:String
3:[Balance]:STRING=[0]:String
4:[CreditLimit]:STRING=[17000.00]:String
5:[AccountCreated]:STRING=[5/25/1985]:String
6:[Rating]:STRING=[B]:String
}
-----------------------------------------------
3 - Record {
0:[Account]:STRING=[761]:String
1:[LastName]:STRING=[Pinkett-Smith]:String
2:[FirstName]:STRING=[Jada]:String
3:[Balance]:STRING=[49654.87]:String
4:[CreditLimit]:STRING=[100000.00]:String
5:[AccountCreated]:STRING=[12/5/2006]:String
6:[Rating]:STRING=[A]:String
}
-----------------------------------------------
4 - Record {
0:[Account]:STRING=[317]:String
1:[LastName]:STRING=[Murray]:String
2:[FirstName]:STRING=[Bill]:String
3:[Balance]:STRING=[789.65]:String
4:[CreditLimit]:STRING=[5000.00]:String
5:[AccountCreated]:STRING=[2/5/2007]:String
6:[Rating]:STRING=[C]:String
}
-----------------------------------------------
5 - Record {
0:[Account]:STRING=[101]:String
1:[LastName]:STRING=[Reeves]:String
2:[FirstName]:STRING=[Keanu]:String
3:[Balance]:STRING=[9315.45]:String
4:[CreditLimit]:STRING=[10000.00]:String
5:[AccountCreated]:STRING=[1998-1-17]:String
6:[Rating]:STRING=[A]:String
}
-----------------------------------------------
6 - Record {
0:[Account]:STRING=[312]:String
1:[LastName]:STRING=[Butler]:String
2:[FirstName]:STRING=[Gerard]:String
3:[Balance]:STRING=[90.00]:String
4:[CreditLimit]:STRING=[1000.00]:String
5:[AccountCreated]:STRING=[2003-8-6]:String
6:[Rating]:STRING=[B]:String
}
-----------------------------------------------
7 - Record {
0:[Account]:STRING=[101]:String
1:[LastName]:STRING=[Hewitt]:String
2:[FirstName]:STRING=[Jennifer Love]:String
3:[Balance]:STRING=[0]:String
4:[CreditLimit]:STRING=[17000.00]:String
5:[AccountCreated]:STRING=[1985-5-25]:String
6:[Rating]:STRING=[B]:String
}
-----------------------------------------------
8 - Record {
0:[Account]:STRING=[312]:String
1:[LastName]:STRING=[Pinkett-Smith]:String
2:[FirstName]:STRING=[Jada]:String
3:[Balance]:STRING=[49654.87]:String
4:[CreditLimit]:STRING=[100000.00]:String
5:[AccountCreated]:STRING=[2006-12-5]:String
6:[Rating]:STRING=[A]:String
}
-----------------------------------------------
9 - Record {
0:[Account]:STRING=[317]:String
1:[LastName]:STRING=[Murray]:String
2:[FirstName]:STRING=[Bill]:String
3:[Balance]:STRING=[789.65]:String
4:[CreditLimit]:STRING=[5000.00]:String
5:[AccountCreated]:STRING=[2007-2-5]:String
6:[Rating]:STRING=[C]:String
}
-----------------------------------------------
10 - Record {
0:[Account]:STRING=[317]:String
1:[LastName]:STRING=[Murray]:String
2:[FirstName]:STRING=[Bill]:String
3:[Balance]:STRING=[1]:String
4:[CreditLimit]:STRING=[5000.00]:String
5:[AccountCreated]:STRING=[2007-2-5]:String
6:[Rating]:STRING=[A]:String
}
-----------------------------------------------
11 records
