pig tutorial - apache pig tutorial - Apache Pig - UCFIRST() - pig latin - apache pig - pig hadoop



What is UCFIRST()?

    • This function accepts a string, converts the first letter of it into uppercase, and returns the result.
    • ucfirst is a function which exists natively in PHP and other languages. It is used to capitalize the first character of a string.

    Syntax:

      Here is the syntax of the function UCFIRST() function.

      grunt> UCFIRST(expression) 
      

      Example:

        Assume that there is a file named wikitechy_emp.txt in the HDFS directory /pig_data/as shown below. This file contains the employee details such as id, name, age, and city.

        wikitechy_emp.txt

        
        001,Aadav,32,Tokyo
        002,Aadhi,33,Kolkata
        003,Charu,23, London
        004,Daya,35,London 
        005, Hansa,22,Bhuwaneshwar 
        006, Hena,21,Chennai
        007,Robert,24, Bhuwaneshwar
        008,Kali,20,Kolkata
        009,Leena,22, Chennai
        010,Mahi,22, newyork
        011,Priya,23, Tokyo
        012,Rahul,20, newyork
        

        And, we have loaded this file into Pig with a relation named wikitechy_emp_data as shown below.

        grunt> wikitechy_emp_data = LOAD 'hdfs://localhost:9000/pig_data/wikitechy_emp.txt' USING PigStorage(',')
           as (id:int, name:chararray, age:int, city:chararray);
        

        Following is an example of the UCFIRST() function. In this example, we are trying to convert the first letters of the names of the cities, to which the employees belong to, to uppercase.

        grunt> ucfirst_data = FOREACH wikitechy_emp_data GENERATE (id,city), UCFIRST(city);
        
        • The result of the statement will be stored in the relation named ucfirst_data. Verify the content of the relation ucfirst_data, using the Dump operator as shown below.
        • In our example, the first letter of the name of the city “tokyo” is in lowercase. After applying UCFIRST() function, it turns into “TOKYO”
        grunt>Dump ucfirst_data;
        
        ((1,Tokyo),Tokyo) 
        ((2,Kolkata),Kolkata)
        ((3,London),London) 
        ((4,London),London) 
        ((5,Bhuwaneshwar),Bhuwaneshwar) 
        ((6,Chennai),Chennai)
        ((7,Bhuwaneshwar),Bhuwaneshwar) 
        ((8,Kolkata),Kolkata)
        ((9,Chennai),Chennai)
        ((10,newyork),Newyork) 
        ((11,Tokyo),Tokyo)
        ((12,newyork),Newyork)  
        
        

        Related Searches to Apache Pig - UCFIRST()