Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8989897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:22:28+00:00 2026-06-15T22:22:28+00:00

I have a csv file with following format TRAABRX12903CC4816,1548880,2:19,4:7,… . . . My problem

  • 0

I have a csv file with following format

TRAABRX12903CC4816,1548880,2:19,4:7,...
.
.
.

My problem is that I want to interpret as

{(key:chararray,key2:int,{(id:int,cnt:int)})}

So far my code is

data = LOAD 'mxm_dataset_test_3.txt' using PigStorage(',');
data0 = foreach data generate $0 as key:chararray, {$2 ..} as bow;

For data0 this the schema data0: {key: chararray,bow: {(NULL)}}
When I try to explicit cast it to (bag{tuple(chararray)}) with

data0 = foreach data generate $0 as key:chararray, {$2 ..} as bow;

this gives the error Cannot cast bag with schema :bag{:tuple(:NULL)} to bag with schema :bag{:tuple(:chararray)}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:22:29+00:00Added an answer on June 15, 2026 at 10:22 pm

    Use the TOBAG built-in function to build your bag:

    $ cat input
    TRAABRX12903CC4816,1548880,2:19,4:7
    TRAABRX12903CC4816,1548881,2:19,4:7,5:23,7:3
    TRAABRX12903CC4816,1548882
    TRAABRX12903CC4816,1548883,17:33
    $ cat test.pig
    data = LOAD 'input' USING PigStorage(',');
    data0 = FOREACH data GENERATE $0 AS key:chararray, $1 AS key2:chararray, TOBAG($2 ..) AS bow:{(pair)};
    DESCRIBE data0;
    DUMP data0;
    $ pig -x local test.pig
    ...
    data0: {key: chararray,key2: chararray,bow: {(pair: NULL)}}
    ...
    (TRAABRX12903CC4816,1548880,{(2:19),(4:7)})
    (TRAABRX12903CC4816,1548881,{(2:19),(4:7),(5:23),(7:3)})
    (TRAABRX12903CC4816,1548882,{})
    (TRAABRX12903CC4816,1548883,{(17:33)})
    

    If you want to split up the id:cnt pairs, however, this gets trickier. Because there is no way to assign a schema to an arbitrary number of elements, and TOBAG is a UDF, Pig can’t cast the bytearray to a chararray or anything else later.

    I would recommend loading the entire line as a string (USING PigStorage('\n')), using STRSPLIT with a limit of 3 to get your key, key2, and comma-delimited list of strings, then iterate with STRSPLIT on comma and then on colon to get the pairs you want, using FLATTEN and TOBAG as needed. I would demonstrate this for you, but I am stuck on Pig 0.9, and judging by PIG-2311, this isn’t possible until Pig 0.10.

    The simplest solution may just be to write your own UDF to interpret a string like 2:13,9:4,5:4:

    data = LOAD 'input' USING PigStorage('\n') AS (line:chararray);
    data0 = FOREACH data GENERATE FLATTEN(STRSPLIT(line, ',', 3)) AS (key:chararray, key2:chararray, pairs:chararray);
    data1 = FOREACH data0 GENERATE key, key2, myudfs.PARSE_PAIRS(pairs);
    

    where myudfs.PARSE_PAIRS returns a bag with the tuples you want. Good luck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If i have a CSV file that is in the following format fd!,sdf,dsfds,dsfd fd!,asdf,dsfds,dsfd
I have a CSV file which has the following format: id,case1,case2,case3 Here is a
I want the following functionality using php I have a csv file. Each file
I have the following code that returns a .CSV file when a link is
I have a file that contains the following in a csv file; country,region,city,postalCode,metroCode,areaCode I
I have the following piece of Java code that reads strings from CSV file.
I have a CSV file with the following format: ID | STUFF | Custom
I have data in a csv file in the following format ,BC1,BC10,BC11 1,2432,420,18 2,276,405,56
I have data in a csv file in the following format 0,q20,q25,q30 0,32821,2270,68 1,105586,11366,38
I have a csv file in a format resembling the following. There are no

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.