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:boa.functions.BoaAstIntrinsics.java

License:Apache License

/**
 * Given a ChangedFile, return the comments for that file at that revision.
 * /*from  w ww . j a  va 2s .co  m*/
 * @param f the ChangedFile to get a snapshot of the comments for
 * @return the comments list, or an empty list on any sort of error
 */
@FunctionSpec(name = "getcomments", returnType = "CommentsRoot", formalParameters = { "ChangedFile" })
public static CommentsRoot getcomments(final ChangedFile f) {
    // since we know only certain kinds have comments, filter before looking up
    final ChangedFile.FileKind kind = f.getKind();
    if (kind != ChangedFile.FileKind.SOURCE_JAVA_ERROR && kind != ChangedFile.FileKind.SOURCE_JAVA_JLS2
            && kind != ChangedFile.FileKind.SOURCE_JAVA_JLS3 && kind != ChangedFile.FileKind.SOURCE_JAVA_JLS4)
        return emptyComments;

    final String rowName = f.getKey() + "!!" + f.getName();

    if (commentsMap == null)
        openCommentMap();

    try {
        final BytesWritable value = new BytesWritable();
        if (commentsMap.get(new Text(rowName), value) != null) {
            final CodedInputStream _stream = CodedInputStream.newInstance(value.getBytes(), 0,
                    value.getLength());
            final CommentsRoot root = CommentsRoot.parseFrom(_stream);
            return root;
        }
    } catch (final InvalidProtocolBufferException e) {
        e.printStackTrace();
    } catch (final IOException e) {
        e.printStackTrace();
    } catch (final RuntimeException e) {
        e.printStackTrace();
    } catch (final Error e) {
        e.printStackTrace();
    }

    System.err.println("error with comments: " + rowName);
    return emptyComments;
}

From source file:boa.functions.BoaAstIntrinsics.java

License:Apache License

/**
 * Given an IssueRepository, return the issues.
 * /*from   www . ja v a 2  s . c o  m*/
 * @param f the IssueRepository to get issues for
 * @return the issues list, or an empty list on any sort of error
 */
@FunctionSpec(name = "getissues", returnType = "IssuesRoot", formalParameters = { "IssueRepository" })
public static IssuesRoot getissues(final IssueRepository f) {
    if (issuesMap == null)
        openIssuesMap();

    try {
        final BytesWritable value = new BytesWritable();
        if (issuesMap.get(new Text(f.getKey()), value) != null) {
            final CodedInputStream _stream = CodedInputStream.newInstance(value.getBytes(), 0,
                    value.getLength());
            final IssuesRoot root = IssuesRoot.parseFrom(_stream);
            return root;
        }
    } catch (final InvalidProtocolBufferException e) {
        e.printStackTrace();
    } catch (final IOException e) {
        e.printStackTrace();
    } catch (final RuntimeException e) {
        e.printStackTrace();
    } catch (final Error e) {
        e.printStackTrace();
    }

    System.err.println("error with issues: " + f.getKey());
    return emptyIssues;
}

From source file:br.com.lassal.mrunit.example.mapreduce.SMSCDRMapper.java

/**
 * Returns the SMS status code and its count
 */// w  ww.j a va 2 s  . co  m

protected void map(LongWritable key, Text value, Context context)
        throws java.io.IOException, InterruptedException {

    //655209;1;796764372490213;804422938115889;6 is the Sample record format
    String[] line = value.toString().split(";");
    // If record is of SMS CDR
    if (Integer.parseInt(line[1]) == 1) {
        status.set(line[4]);
        context.write(status, addOne);
        context.write(new Text("Z"), addOne);
    }
}

From source file:br.com.lassal.mrunit.example.SMSCDRMapperReducerTest.java

@Test
public void testMapper() throws IOException {
    mapDriver.withInput(new LongWritable(), new Text("655209;1;796764372490213;804422938115889;6"));
    mapDriver.withOutput(new Text("6"), new IntWritable(1));
    mapDriver.withOutput(new Text("Z"), new IntWritable(1));
    mapDriver.runTest();//from  w  w  w . j a  va  2s.co  m
}

From source file:br.com.lassal.mrunit.example.SMSCDRMapperReducerTest.java

@Test
public void testReducer() throws IOException {
    List<IntWritable> values = new ArrayList<IntWritable>();
    values.add(new IntWritable(1));
    values.add(new IntWritable(1));
    reduceDriver.withInput(new Text("6"), values);
    reduceDriver.withOutput(new Text("6"), new IntWritable(2));
    reduceDriver.runTest();//from w  w w  . j a va 2  s  .  c o  m
}

From source file:br.com.lassal.nqueens.grid.mapreduce.NQueenIncrementalCounterMapper.java

private void mapeiaInicioSolucao(int nQueensSize, Context mapContext) throws IOException, InterruptedException {
    int qtdPosicoesPreSolucao = NQueenIncrementalCounterMapper.QTD_POSICOES_PRESOLUCAO;

    int[] boardRow = new int[qtdPosicoesPreSolucao];

    int y = 0;//from   www .j  a  v  a 2  s .c o  m
    boardRow[y] = -1;
    while (y >= 0) {
        do {
            boardRow[y]++;
        } while ((boardRow[y] < nQueensSize) && unsafe(boardRow, y));
        if (boardRow[y] < nQueensSize) {
            if (y < (qtdPosicoesPreSolucao - 1)) {
                boardRow[++y] = -1;
            } else {
                Text keyValue = new Text(NQueenCountRecord.writePredicate(nQueensSize, boardRow));
                mapContext.write(keyValue, keyValue);
                //     String key = String.format("%d:%d,%d,%d,%d", nQueensSize, boardRow[0], boardRow[1], boardRow[2], boardRow[3]);
                //     System.out.format("mapDriver.withOutput(new Text(\"%s\"), new Text(\"%s\"));\n", keyValue.toString(), keyValue.toString());
            }
        } else {
            y--;
        }
    }

}

From source file:br.com.lassal.nqueens.grid.mapreduce.NQueenIncrementalCounterMapper.java

private void resolveNQueens(NQueenCountRecord record, long tempoLimite, Context mapContext)
        throws IOException, InterruptedException {
    int qtdPosicoesPreSolucao = NQueenIncrementalCounterMapper.QTD_POSICOES_PRESOLUCAO;
    Date startProcessTime = Calendar.getInstance().getTime();
    if (record.isBranchSolved()) {
        mapContext.write(new Text(record.getPredicate()), new Text(record.toString()));
    } else {/* ww  w  .j  a v  a  2  s. co  m*/
        int[] boardRow = record.getPartialSolution();

        long numPassos = 0;
        int numPositionsSolved = record.getNumPositionsSolved();
        int y = numPositionsSolved;
        boardRow[y] = -1;
        int tamAgrupador = record.getNQueensSize() - 4;
        tamAgrupador = tamAgrupador < NQueenIncrementalCounterMapper.QTD_POSICOES_PRESOLUCAO
                ? NQueenIncrementalCounterMapper.QTD_POSICOES_PRESOLUCAO
                : tamAgrupador;
        int[] agrupador = null;
        int numSolucoesAgrupador = 0;
        long elapsedTime = 0;

        while (y >= qtdPosicoesPreSolucao && elapsedTime < tempoLimite) {
            do {
                boardRow[y]++;
                numPassos++;
            } while ((boardRow[y] < record.getNQueensSize()) && unsafe(boardRow, y));

            if (boardRow[y] < record.getNQueensSize()) {
                if (y < (record.getNQueensSize() - 1)) {
                    boardRow[++y] = -1;
                } else {

                    int[] agrupadorAtual = Arrays.copyOf(boardRow, tamAgrupador);
                    if (agrupador == null) {
                        agrupador = agrupadorAtual;
                    }
                    if (!Arrays.equals(agrupador, agrupadorAtual)) {
                        if (numSolucoesAgrupador > 0) {
                            Text key = new Text(record.getPredicate());
                            Text value = new Text(NQueenCountRecord.writeRecord(record.getNQueensSize(),
                                    agrupador, numSolucoesAgrupador, true));
                            mapContext.write(key, value);
                            //   System.out.println(value.toString());
                        }
                        agrupador = agrupadorAtual;
                        numSolucoesAgrupador = 0;
                    }
                    numSolucoesAgrupador++;
                }
            } else {
                boardRow[y] = -1;
                y--;
            }

            if ((numPassos % 1000) == 0) {
                Date now = Calendar.getInstance().getTime();
                elapsedTime = now.getTime() - startProcessTime.getTime();
            }
        }
        if (y >= qtdPosicoesPreSolucao) {
            Text key = new Text(record.getPredicate());
            Text value = new Text(NQueenCountRecord.writeRecord(record.getNQueensSize(), boardRow,
                    numSolucoesAgrupador, false));
            mapContext.write(key, value);
            //System.out.println(value.toString());
        } else if (agrupador != null && numSolucoesAgrupador > 0) {
            Text key = new Text(record.getPredicate());
            Text value = new Text(NQueenCountRecord.writeRecord(record.getNQueensSize(), agrupador,
                    numSolucoesAgrupador, true));
            mapContext.write(key, value);
            // System.out.println(value.toString());
        }

    }
}

From source file:br.com.lassal.nqueens.grid.mapreduce.NQueenIncrementalCounterReducer.java

protected void reduce(Text key, Iterable<Text> values, Context context)
        throws java.io.IOException, InterruptedException {
    BigInteger partialCounter = BigInteger.valueOf(0);
    String lastResult = "";

    for (Text value : values) {
        NQueenCountRecord record = NQueenCountRecord.parse(value.toString(),
                NQueenIncrementalCounterMapper.QTD_POSICOES_PRESOLUCAO);

        if (record.isBranchSolved()) {
            if (record.getPartialSolutionText().compareTo(lastResult) > 0) {
                lastResult = record.getPartialSolutionText();
            }/* w w  w . ja va 2  s  . c  om*/
            partialCounter = partialCounter.add(record.getSolutionsCount());
        } else {
            context.write(NullWritable.get(), value);
        }
    }

    if (partialCounter.compareTo(BigInteger.valueOf(0)) > 0) {
        String sumValue = key.toString() + "=" + partialCounter.toString() + ";";
        context.write(NullWritable.get(), new Text(sumValue));
    }
}

From source file:br.com.lassal.nqueens.grid.mapreduce.NQueenIncrementalCounterResultMapper.java

protected void map(LongWritable key, Text value, Context context)
        throws java.io.IOException, InterruptedException {

    NQueenCountRecord record = NQueenCountRecord.parse(value.toString(), 1);

    if (record.isBranchSolved()) {
        Text outKey = new Text(record.getPredicate());
        Text outValue = new Text(record.getSolutionsCount().toString());
        context.write(outKey, outValue);
    } else {// w  ww.  j  ava2s  .c om
        Text outKey = new Text(record.getPredicate());
        Text outValue = new Text(NQueenIncrementalCounterResultMapper.PARTIAL_SOLUTION_ID);
        context.write(outKey, outValue);

        if (record.getSolutionsCount().compareTo(BigInteger.ZERO) > 0) {
            outValue = new Text(record.getSolutionsCount().toString());
            context.write(outKey, outValue);
        }
    }
}

From source file:br.com.lassal.nqueens.grid.mapreduce.NQueenIncrementalCounterResultReducer.java

protected void reduce(Text key, Iterable<Text> values, Context context)
        throws java.io.IOException, InterruptedException {
    BigInteger partialCount = BigInteger.ZERO;
    long numPartialSolutions = 0;

    for (Text item : values) {
        String value = item.toString();

        if (NQueenIncrementalCounterResultMapper.PARTIAL_SOLUTION_ID.equals(value)
                && numPartialSolutions < Long.MAX_VALUE) {
            numPartialSolutions++;// ww  w.j  a  v  a2  s.c o  m
        } else {
            BigInteger solutionCount = new BigInteger(value);
            partialCount = partialCount.add(solutionCount);
        }
    }

    context.write(NullWritable.get(), new Text(key.toString() + "=" + partialCount.toString() + ";"));

    if (numPartialSolutions > 0) {
        context.write(NullWritable.get(), new Text(
                key.toString() + " NO FINALIZADO - EXISTEM " + numPartialSolutions + " SOLUES ABERTAS."));
    }
}