frazman
frazman

Reputation: 33273

Unable to cast string to long

So, Here is my code..

 HashMap<Long, Long> trackCount = new HashMap<Long, Long>(); 
 for (Text value: values)
            {
                String[] chunks = value.toString().split(DELIMITER);
                Long trackId = Long.parseLong(chunks[0]); // see i convert it to Long
                Long frequency = Long.parseLong(chunks[1]);
                Long curFreq = 0l;
                if(trackCount.containsKey(trackId)){
                    curFreq = trackCount.get(trackId);
                }
                trackCount.put(trackId, curFreq + frequency); // key is trackId which is long..
            }
            trackFrequency.put("track_id", key);
            trackFrequency.put("track_counts", trackCount);
            context.write(NullWritable.get(), new Text(trackFrequency.toJSONString()));

This is essentially a hadoop code.. but the final output I see is

     {"track_counts":{"2":52,"3":2,"7":32},"track_id":1}

Now why are the keys in track_counts about strings.. I thought I was casting them to Long?

Upvotes: 0

Views: 163

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280102

The name in JSON name-value pairs can only be a String. Whatever you serializer is, it converts the map key to a String before writing it in the JSON.

Upvotes: 4

Related Questions