Load tsv file in pig

Load tsv file in pig

Requirement

Assume that you want to load TSV(tab separated values) file in pig and output of pig should be pipe delimited and should be stored in one directory.

Solution

Please follow the below steps:-

Step 1: Sample TSV file

Create a sample TSV file named as sample_1.tsv file.

Put content in that file, delimited by a tab (\t). If you have created a file in windows then transfer it to your Linux machine via WinSCP.

You can get the file from here sample_1

I have a local directory as /root/bigdataprogrammers/input_files, so I have placed a sample_1.tsv file in that directory.

You can see content of that file using below command in shell

 
 
cat /root/bigdataprogrammers/input_files/sample_1.tsv

It will show the file content:-

Step 2: Load Data in Pig

Now I will load the file in pig, because the file is present in local, so here I prefer pig in local mode. In real time pig MapReduce mode works.

Enter a below-mentioned command in putty

 
 
 pig -x local

It will take you to the grunt shell.Type the below-mentioned command in grunt shell.

 
 
  1. MyTSVData = LOAD '/root/bigdataprogrammers/input_files/sample_1.tsv' using PigStorage('\t') AS (id:chararray,code:chararray);

You can see whether the file is loaded or not using below command:-

 
 
  1.  dump MyTSVData

It will show the content of relation MyTSVData.

Step 3: Store output  data in Pig

You can store this relation using STORE command in pig, assume that you want output file into pipe delimited format.

Enter below command to store the relation into pipe delimited file. It will be stored in a tsv_to_pipe directory which will be created dynamically, make sure that you do not have the same directory already present in your output directory.

 

 
 
  1. STORE MyTSVData INTO '/root/bigdataprogrammers/output_files/tsv_to_pipe' using PigStorage('|');

Step 4: Show Output

Come out of grunt shell by pressing ctrl+z

Use below command to see the output

 
 
 cat root/bigdataprogrammers/output_files/tsv_to_pipe/*

keep learning.

14
0

Join in hive with example

Requirement You have two table named as A and B. and you want to perform all types of join in ...
Read More

Join in pyspark with example

Requirement You have two table named as A and B. and you want to perform all types of join in ...
Read More

Join in spark using scala with example

Requirement You have two table named as A and B. and you want to perform all types of join in ...
Read More

Java UDF to convert String to date in PIG

About Code Many times it happens like you have received data from many systems and each system operates on a ...
Read More
/ java udf, Pig, pig, pig udf, string to date, udf

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.