Saturday, 17 May 2014

Count Frequency Of Values In A Column Using Apache Pig


There may be situations to count the occurence of a value in a field.
Let this be the sample input bag.


user_id   course_name user_name
1           Social      Anju
2           Maths       Malu
1           English     Anju
1           Maths       Anju

Say we need to calculate no of occurence of each user_name.
Anju 3
Malu 1

Inorder to achieve this - COUNT Built In Function can be used.


COUNT Function in Apache Pig


COUNT function  compute the number of elements in a bag.
To group count a preceding GROUP BY statement and for global counts GROUP ALL statement is required.

The basic idea to do the above example is to group by user_name and count the tuples in the bag.


--count.pig

 userAlias = LOAD '/home/sreeveni/myfiles/pig/count.txt' as 
             (user_id:long,course_name:chararray,user_name:chararray);
 groupedByUser = group userAlias by user_name;
 counted = FOREACH groupedByUser GENERATE group as user_name,COUNT(userAlias) as cnt;
 result = FOREACH counted GENERATE user_name, cnt;
 store result into '/home/sreeveni/myfiles/pig/OUT/count';

The COUNT function ignores NULLs, that is tuple in the bag will not be counted if the first field in this tuple is NULL.
COUNT_STAR can be used to count fields including NULL values.




14 comments:


  1. Worthful Hadoop tutorial. Appreciate a lot for taking up the pain to write such a quality content on Hadoop tutorial. Just now I watched this similar Hadoop tutorial and I think this will enhance the knowledge of other visitors for sure. Thanks anyway.:https://www.youtube.com/watch?v=1jMR4cHBwZE

    ReplyDelete
  2. Top Trending Technologies of 2019. Watch here:https://www.youtube.com/watch?v=-y5Z2fmnp-o

    ReplyDelete
  3. Very Impressive Big Data Hadoop tutorial. The content seems to be pretty exhaustive and excellent and will definitely help in learning Big Data Hadoop course. I'm also a learner taken up Big Data Hadoop Tutorial and I think your content has cleared some concepts of mine. While browsing for Hadoop tutorials on YouTube i found this fantastic video on Big Data Hadoop Tutorial.Do check it out if you are interested to know more.https://www.youtube.com/watch?v=nuPp-TiEeeQ&

    ReplyDelete
  4. Trade Stocks, Forex, And Bitcoin Anywhere In The World:tradeatf Is The Leading Provider Of Software That Allows You To Trade On Your Own Terms. Whether You Are Operating In The Forex, Stock, Or Cryptocurrency Markets, Use tradeatf Software And Anonymous Digital Wallet To Connect With The Financial World.: tradeatf Is A Currency Trading Company That Allows You To Trade Stocks, Forex, And Cryptocurrency.

    ReplyDelete
  5. AximTrade Review Is A Forex And Cfd Broker. It Offers Trading In Currency Pairs, Commodities, Indices, And Shares. It Also Provides A Range Of Tools, And 24/7 Customer Service. Sign Up For Aximtrade Login Account Today!

    ReplyDelete
  6. Search engine optimization is a highly effective digital marketing strategy for Forex Trading Seo . It is the process of optimizing a website so that it will rank higher in search engine results.

    ReplyDelete
  7. Discover professional seo services for online business. Before online trading is booming, Seo Service Provider For Forex plays an important role in the marketing strategy. Forex trading still has its own charm and attraction. People who are deeply interested in it will find their way to the right direction.

    ReplyDelete
  8. Whether you are a new trader or an experienced trader, Online Stock Broker provides all of the information you need to make an educated decision about which online broker to use. We have years of experience in this industry working alongside brokers, traders and market makers and we bring our knowledge and expertise to you with this guide.

    ReplyDelete
  9. You'll understand the market a whole lot better with the data and graphic tools provided by our stock quote system. You'll understand how certain economic events can affect your investments, and be able to answer questions from stock market friends concerning when to buy or sell Acam Stock . You will have access to live charts, and you can receive instant notifications as stocks change in cost.

    ReplyDelete
  10. Never miss a stock price change from Acam Stock with our Live, Real Time Stock Market Overview! Most of the data is updated every 5 minutes to ensure that you are getting all of the latest Acam Stock change alerts as soon as they happen.

    ReplyDelete
  11. We have analyzed Sos Stocktwits and other stock information for you so that you can stay up-to-date on the latest Sos Stocktwits market trends.

    ReplyDelete
  12. Check The Sos Stocktwits Overview To Monitor A Stock And View Live Price Changes As They Occur, Including Volume And Share Changes. With Our Live, Real Time Stock Market Overview App, You No Longer Need To Refresh Your Browser Or Watch Multiple Computer Screens To View Real Time Stock Prices.

    ReplyDelete
  13. Ati Testing Login will help you to choose the bestAti Testing Login online account for you. We are committed to providing excellent online recommendation for home buyers and sellers of personal information.

    ReplyDelete
  14. Awesome article! I want people to know just how good this information is in your article. It’s interesting, compelling content. Your views are much like my own concerning this subject. note to frequency

    ReplyDelete