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

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

Introduction

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

Prototype

public ByteWritable(byte value) 

Source Link

Usage

From source file:org.openx.data.jsonserde.objectinspector.primitive.JavaStringByteObjectInspector.java

License:Open Source License

@Override
public Object getPrimitiveWritableObject(Object o) {
    if (o == null)
        return null;

    if (o instanceof String) {
        return new ByteWritable(ParsePrimitiveUtils.parseByte((String) o));
    } else {//from w ww  .  j  av a  2  s .  c  o  m
        return new ByteWritable((Byte) o);
    }
}

From source file:org.shaf.core.util.IOUtils.java

License:Apache License

/**
 * Writes an {@link Object} to the {@link DataOutput}.
 * //  w  ww .j  a v a 2 s.  c  o m
 * @param obj
 *            the object to write.
 * @param out
 *            the data output stream.
 * @throws IOException
 *             if I/O error occurs.
 */
public static final void writeObject(Object obj, DataOutput out) throws IOException {
    try {
        if (obj == null) {
            throw new IOException("Writing object is not defined: null.");
        } else if (ClassUtils.isBoolean(obj)) {
            (new BooleanWritable((boolean) obj)).write(out);
        } else if (ClassUtils.isByte(obj)) {
            (new ByteWritable((byte) obj)).write(out);
        } else if (ClassUtils.isShort(obj)) {
            (new ShortWritable((short) obj)).write(out);
        } else if (ClassUtils.isInteger(obj)) {
            (new IntWritable((int) obj)).write(out);
        } else if (ClassUtils.isLong(obj)) {
            (new LongWritable((long) obj)).write(out);
        } else if (ClassUtils.isFloat(obj)) {
            (new FloatWritable((float) obj)).write(out);
        } else if (ClassUtils.isDouble(obj)) {
            (new DoubleWritable((double) obj)).write(out);
        } else if (ClassUtils.isString(obj)) {
            Text.writeString(out, (String) obj);
        } else if (ClassUtils.isEnum(obj)) {
            (new IntWritable(((Enum<?>) obj).ordinal())).write(out);
        } else if (ClassUtils.isArray(obj)) {
            int length = Array.getLength(obj);
            writeObject(length, out);
            for (int j = 0; j < length; j++) {
                writeObject(Array.get(obj, j), out);
            }
        } else {
            ((Writable) obj).write(out);
        }
    } catch (IllegalArgumentException exc) {
        throw new IOException(exc);
    }
}

From source file:org.shaf.core.util.IOUtilsTest.java

License:Apache License

/**
 * Test reading of {@code byte} value./*from ww  w . ja  v  a 2 s.  c  o  m*/
 */
@Test
public void testReadByte() {
    byte[] buf = null;

    byte value = 123;
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataOutputStream out = new DataOutputStream(baos);) {
        new ByteWritable(value).write(out);
        buf = baos.toByteArray();
    } catch (IOException exc) {
        fail(exc.getMessage());
    }

    try (ByteArrayInputStream bais = new ByteArrayInputStream(buf);
            DataInputStream in = new DataInputStream(bais);) {
        assertEquals(value, (byte) IOUtils.readObject(byte.class, in));
    } catch (IOException exc) {
        fail(exc.getMessage());
    }
}

From source file:uk.ac.cam.eng.extraction.hadoop.datatypes.ProvenanceCountMap.java

License:Apache License

private static ByteWritable getCached(byte b) {
    if (bytesCache.containsKey(b)) {
        return bytesCache.get(b);
    }/*ww w .  j  a  v  a2  s. c o  m*/
    ByteWritable result = new ByteWritable(b);
    bytesCache.put(b, result);
    return result;
}

From source file:uk.ac.cam.eng.rule.filtering.RuleFilter.java

License:Apache License

private Map<Rule, RuleData> filterRulesBySource(SidePattern sourcePattern, List<Pair<Rule, RuleData>> rules,
        int provMapping) {
    Map<Rule, RuleData> results = new HashMap<>();
    // If the source side is a phrase, then we want everything
    if (sourcePattern.isPhrase()) {
        rules.forEach(entry -> results.put(entry.getFirst(), entry.getSecond()));
        return results;
    }//from  w w w .  j a v  a2  s. c  o m
    int numberTranslations = 0;
    int numberTranslationsMonotone = 0; // case with more than 1 NT
    int numberTranslationsInvert = 0;
    int prevCount = -1;
    double nTransConstraint = sourcePatternConstraints.get(sourcePattern).nTrans;
    ByteWritable countIndex = new ByteWritable((byte) provMapping);
    for (Pair<Rule, RuleData> entry : rules) {
        // number of translations per source threshold
        // in case of ties we either keep or don't keep the ties
        // depending on the config
        RulePattern rulePattern = RulePattern.getPattern(entry.getFirst());
        int count = (int) entry.getSecond().getProvCounts().get(countIndex).get();
        boolean notTied = count != prevCount;
        boolean moreThan1NT = sourcePattern.hasMoreThan1NT();
        if (notTied && ((moreThan1NT && nTransConstraint <= numberTranslationsMonotone
                && nTransConstraint <= numberTranslationsInvert)
                || (!moreThan1NT && nTransConstraint <= numberTranslations))) {
            break;
        }
        results.put(entry.getFirst(), entry.getSecond());
        if (moreThan1NT) {
            if (rulePattern.isSwappingNT()) {
                ++numberTranslationsInvert;
            } else {
                ++numberTranslationsMonotone;
            }
        }
        numberTranslations++;
        prevCount = count;
    }
    return results;
}

From source file:uk.ac.cam.eng.rule.filtering.RuleFilter.java

License:Apache License

/**
 * Indicates whether we should filter a rule. The decision is based on a
 * particular provenance./*from ww w  .j  a v  a  2s . com*/
 * 
 * @return
 */
private boolean filterRule(Feature s2t, Feature t2s, SidePattern sourcePattern, Rule rule, RuleData data,
        int provMapping) {
    IntWritable provIW = IntWritableCache.createIntWritable(provMapping);
    // Immediately filter if there is data for this rule under this
    // provenance
    if (!data.getFeatures().get(s2t).containsKey(provIW)) {
        return true;
    }
    RulePattern rulePattern = RulePattern.getPattern(rule);
    if (!(sourcePattern.isPhrase() || allowedPatterns.contains(rulePattern))) {
        return true;
    }
    double source2targetProbability = data.getFeatures().get(s2t).get(provIW).get();
    double target2sourceProbability = data.getFeatures().get(t2s).get(provIW).get();
    int numberOfOccurrences = (int) data.getProvCounts().get(new ByteWritable((byte) provMapping)).get();

    if (sourcePattern.isPhrase()) {
        // source-to-target threshold
        if (source2targetProbability <= minSource2TargetPhraseLog) {
            return true;
        }
        // target-to-source threshold
        if (target2sourceProbability <= minTarget2SourcePhraseLog) {
            return true;
        }
    } else {
        // source-to-target threshold
        if (source2targetProbability <= minSource2TargetRuleLog) {
            return true;
        }
        // target-to-source threshold
        if (target2sourceProbability <= minTarget2SourceRuleLog) {
            return true;
        }
        // minimum number of occurrence threshold
        if (numberOfOccurrences < sourcePatternConstraints.get(sourcePattern).nOcc) {
            return true;
        }
    }
    return false;
}