- Apache Pig Environment
- Apache Pig - Installation
- Apache Pig - Execution
- Apache Pig - Grunt Shell
- Pig Latin
- Pig Latin - Basics
- Load & Store Operators
- Apache Pig - Reading Data
- Apache Pig - Storing Data
- Diagnostic Operators
- Apache Pig - Diagnostic Operator
- Apache Pig - Describe Operator
- Apache Pig - Explain Operator
- Apache Pig - Illustrate Operator
- Grouping & Joining
- Apache Pig - Group Operator
- Apache Pig - Cogroup Operator
- Apache Pig - Join Operator
- Apache Pig - Cross Operator
- Combining & Splitting
- Apache Pig - Union Operator
- Apache Pig - Split Operator
- Pig Latin Built-In Functions
- Apache Pig - Eval Functions
- Load & Store Functions
- Apache Pig - Bag & Tuple Functions
- Apache Pig - String Functions
- Apache Pig - date-time Functions
- Apache Pig - Math Functions
- Other Modes Of Execution
- Apache Pig - User-Defined Functions
- Apache Pig - Running Scripts
- Apache Pig Useful Resources
- Apache Pig - Quick Guide
- Apache Pig - Useful Resources
- Apache Pig - Discussion
Apache Pig - ToString()
This method is used to convert the date-time object to a customized string.
Syntax
Here is the syntax of the ToString() function.
grunt> ToString(datetime [, format string])
Example
Assume that there is a file named date.txt in the HDFS directory /pig_data/. This file contains the date-of-birth details of a particular person, id, date, and time.
date.txt
001,1989/09/26 09:00:00 002,1980/06/20 10:22:00 003,1990/12/19 03:11:44
And, we have loaded this file into Pig with a relation named date_data as shown below.
grunt> date_data = LOAD 'hdfs://localhost:9000/pig_data/date.txt' USING PigStorage(',')
as (id:int,date:chararray);
Following is an example of the ToString() function. The ToString() function converts the given date-time objects in to String format. Therefore, let us generate the date-time objects of all employees using todate() function.
grunt> todate_data = foreach date_data generate ToDate(date,'yyyy/MM/dd HH:mm:ss') as (date_time:DateTime ); grunt> Dump todate_data; (1989-09-26T09:00:00.000+05:30) (1980-06-20T10:22:00.000+05:30) (1990-12-19T03:11:44.000+05:30)
Let us get the string format of the date-time objects of all the employees using ToString() method and store it in a relation named tostring_data.
grunt> tostring_data = foreach todate_data generate (date_time), ToString(date_time,Text);
Verify the tostring_data relation using the Dump command as shown below.
grunt> Dump tostring_data; (1989-09-26T09:00:00.000+05:30,39) (1980-06-20T10:22:00.000+05:30,25) (1990-12-19T03:11:44.000+05:30,51)