Example usage for org.apache.hadoop.io Text Text

List of usage examples for org.apache.hadoop.io Text Text

Introduction

In this page you can find the example usage for org.apache.hadoop.io Text Text.

Prototype

public Text(byte[] utf8) 

Source Link

Document

Construct from a byte array.

Usage

From source file:Assignment3_P4_DateStock.DateStock_Reducer.java

public void reduce(Text key, Iterable<DateStock_CompositeValueWritable> values, Context context)
        throws IOException, InterruptedException {
    int minStockVol = 0;
    int maxStockVol = 0;
    Float maxStockPriceAdj = 0f;//from w  w w .  j a v a2s  .  c  om
    String minDate = "";
    String maxDate = "";
    int count = 0;

    for (DateStock_CompositeValueWritable value : values) {
        if (count < 1) {
            minStockVol = Integer.parseInt(value.getVolume());
            minDate = value.getDate();
            //System.out.println("Min date is " + minDate);
        }

        if (Integer.parseInt(value.getVolume()) < minStockVol) {
            minStockVol = Integer.parseInt(value.getVolume());
            minDate = value.getDate();
            //System.out.println("Min date is " + minDate);
        }

        if (Integer.parseInt(value.getVolume()) > maxStockVol) {
            maxStockVol = Integer.parseInt(value.getVolume());
            maxDate = value.getDate();
            //System.out.println("Max date is " + maxDate);
        }

        if (Float.parseFloat(value.getMaxStockPrice()) > maxStockPriceAdj) {
            maxStockPriceAdj = Float.parseFloat(value.getMaxStockPrice());
        }

        count++;
    }

    String op = minDate + "\t" + maxDate + "\t" + String.valueOf(maxStockPriceAdj);
    Text out = new Text(op);
    context.write(key, out);

}

From source file:Assignment4_P2_StockAverageWithCombiner.StockAverage_Mapper.java

public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
    String[] stockInfo = value.toString().split(",");

    if (!stockInfo[1].trim().equals("stock_symbol")) {
        String[] dateParts = stockInfo[2].split("-");
        Text year = new Text(dateParts[0]);

        stockPriceAdjCompositeObj.setCount(1);
        stockPriceAdjCompositeObj.setAverage(stockInfo[8]);

        context.write(year, stockPriceAdjCompositeObj);
    }//w  w w . j a va2s  .  c om
}

From source file:Assignment4_P3_InMemoryStdDeviation.MovieRatingStdDev_Reducer.java

public void reduce(IntWritable key, Iterable<FloatWritable> values, Context context)
        throws IOException, InterruptedException {
    int running_sum = 0;
    int running_count = 0;
    float median = 0;

    for (FloatWritable val : values) {
        list.add((float) val.get());
        running_sum += val.get();
        running_count++;/*from   w  w  w  .  j  a va2  s .c o  m*/
    }

    Collections.sort(list);

    // calculating median
    if (list.size() % 2 == 0) {
        median = (list.get((running_count / 2) - 1) + list.get((running_count / 2))) / 2;
    } else {
        median = list.get((running_count / 2));
    }

    // calculating mean
    float mean = running_sum / running_count;

    // calculating standard deviation
    float sumSquare = 0;
    float stdDev = 0;
    for (Float oneValue : list) {
        sumSquare += (oneValue - mean) * (oneValue - mean);
    }

    // finally, std dev
    stdDev = (float) Math.sqrt(sumSquare / (running_count - 1));

    //.append(median)
    String outcome = new StringBuilder().append("\t").append(stdDev).append("\t").append(running_sum)
            .append("\t").append(running_count).toString();
    result = new Text(outcome);
    context.write(key, result);
}

From source file:Assignment4_P4_MemoryConscious.MovieRatingMemConscious_Reducer.java

public void reduce(IntWritable key, Iterable<SortedMapWritable> values, Context context)
        throws IOException, InterruptedException {
    int running_count = 0;
    long running_sum = 0;
    float median = 0;
    list = new TreeMap<>();

    // loop through all ratings in received hashmap for this movieID
    for (SortedMapWritable val : values) {

        // iterate through every entry consisting of movieRating, countOfRating for ex (4.5, 10) i.e 10 people rated it 4.5
        for (Map.Entry<WritableComparable, Writable> entry : val.entrySet()) {

            // extract movieRating for ex : 4.5
            FloatWritable number = (FloatWritable) entry.getKey();
            float movieRating = number.get();

            //extract countOfRating for ex : 10
            LongWritable counter = (LongWritable) entry.getValue();
            long count = counter.get();

            // calculate running sum by multiplying movieRating and countRating i.e (4.5 * 10 = 45)
            running_sum += (movieRating * count);

            // increment running count for getting average later i.e 10
            running_count += count;//from  w w  w .ja v  a  2s.  c o m

            // make <count> entries for <movieRating> in new hashmap i.e  (4.5, 10)
            if (list.containsKey(movieRating)) {
                list.put(movieRating, list.get(movieRating) + count);
            } else {
                list.put(movieRating, count);
            }

        }
    }

    System.out.println("Running count for movieID " + key + " is :- " + running_count);
    System.out.println("Rating List size for movieID " + key + " is :- " + list.size());

    // calculating mean
    float mean = running_sum / running_count;
    System.out.println("Mean for movieID " + key + " is :- " + mean);

    // calculating standard deviation
    float sumSquare = 0;
    float stdDev = 0;
    for (Map.Entry<Float, Long> entry : list.entrySet()) {
        sumSquare += (entry.getKey() - mean) * (entry.getKey() - mean) * (entry.getValue());
    }

    // finally, std dev
    stdDev = (float) Math.sqrt(sumSquare / (running_count - 1));
    System.out.println("Standard deviation for movieID " + key + " is :- " + stdDev);

    //.append(median)
    String outcome = new StringBuilder().append("\t").append(stdDev).append("\t").append(running_sum)
            .append("\t").append(running_count).toString();
    result = new Text(outcome);
    context.write(key, result);
}

From source file:Assignment5_P2_DistinctIPAddress.DistinctIPAddress_Mapper.java

public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
    String[] ipInfo = value.toString().split("-");
    ipaddr = new Text(ipInfo[0].trim());
    context.write(ipaddr, NullWritable.get());
}

From source file:Assignment5_P6_StructureToHierarchyPattern.Structure_Hierarchy_Movie_Mapper.java

public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
    String[] movieInfo = value.toString().split(",");
    String movieId = movieInfo[0];

    //append 'T' for title identification
    String movieTitle = "T" + movieInfo[1];

    // movieID, movieTitle
    context.write(new Text(movieId), new Text(movieTitle));
}

From source file:Assignment5_P6_StructureToHierarchyPattern.Structure_Hierarchy_Reducer.java

@Override
protected void setup(Context context) throws IOException, InterruptedException {
    context.write(null, new Text("<MovieAndGenre>"));
}

From source file:Assignment5_P6_StructureToHierarchyPattern.Structure_Hierarchy_Reducer.java

public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
    sb.append("<Movie>");

    for (Text value : values) {
        if (value.toString().charAt(0) == 'T' && count < 1) {
            String title = value.toString().substring(1, value.toString().length()).trim();
            // put it in a tag
            sb.append("<Title>").append(title).append("</Title>");
            // in count so next time it doesnt come in
            count++;/*from  www .  j  av  a2s .c o m*/
        } else {
            String tag = value.toString().substring(1, value.toString().length()).trim();

            // call genre fellow
            constructPropertyXml(tag);
        }
    }

    sb.append("</Movie>");
    context.write(null, new Text(sb.toString()));
}

From source file:Assignment5_P6_StructureToHierarchyPattern.Structure_Hierarchy_Reducer.java

@Override
protected void cleanup(Context context) throws IOException, InterruptedException {
    context.write(null, new Text("</MovieAndGenre>"));
}

From source file:Assignment5_P6_StructureToHierarchyPattern.Structure_Hierarchy_Tag_Mapper.java

public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
    String[] movieInfo = value.toString().split(",");
    String movieId = movieInfo[1];

    //append 'A' for tag identification
    String movieTag = "A" + movieInfo[2];

    // movieID, movieTag
    context.write(new Text(movieId), new Text(movieTag));
}