List of usage examples for org.apache.hadoop.io Text getBytes
@Override public byte[] getBytes()
From source file:mvm.rya.indexing.accumulo.entity.AccumuloDocIdIndexer.java
License:Apache License
private QueryBindingSet deserializeKey(Key key, StarQuery sq, BindingSet currentBs, Set<String> unCommonVar) { QueryBindingSet currentSolutionBs = new QueryBindingSet(); Text row = key.getRow(); Text cq = key.getColumnQualifier(); String[] cqArray = cq.toString().split(DocIndexIteratorUtil.DOC_ID_INDEX_DELIM); boolean commonVarSet = false; //if common Var is constant there is no common variable to assign a value to if (sq.commonVarConstant()) { commonVarSet = true;//www . j a va 2 s. com } if (!commonVarSet && sq.isCommonVarURI()) { RyaURI rURI = new RyaURI(row.toString()); currentSolutionBs.addBinding(sq.getCommonVarName(), RyaToRdfConversions.convertValue(rURI)); commonVarSet = true; } for (String s : sq.getUnCommonVars()) { byte[] cqBytes = cqArray[sq.getVarPos().get(s)].getBytes(); int firstIndex = Bytes.indexOf(cqBytes, DELIM_BYTE); int secondIndex = Bytes.lastIndexOf(cqBytes, DELIM_BYTE); int typeIndex = Bytes.indexOf(cqBytes, TYPE_DELIM_BYTE); byte[] tripleComponent = Arrays.copyOfRange(cqBytes, firstIndex + 1, secondIndex); byte[] cqContent = Arrays.copyOfRange(cqBytes, secondIndex + 1, typeIndex); byte[] objType = Arrays.copyOfRange(cqBytes, typeIndex, cqBytes.length); if ((new String(tripleComponent)).equals("object")) { byte[] object = Bytes.concat(cqContent, objType); org.openrdf.model.Value v = null; try { v = RyaToRdfConversions.convertValue(RyaContext.getInstance().deserialize(object)); } catch (RyaTypeResolverException e) { e.printStackTrace(); } currentSolutionBs.addBinding(s, v); } else if ((new String(tripleComponent)).equals("subject")) { if (!commonVarSet) { byte[] object = Bytes.concat(row.getBytes(), objType); org.openrdf.model.Value v = null; try { v = RyaToRdfConversions.convertValue(RyaContext.getInstance().deserialize(object)); } catch (RyaTypeResolverException e) { e.printStackTrace(); } currentSolutionBs.addBinding(sq.getCommonVarName(), v); commonVarSet = true; } RyaURI rURI = new RyaURI(new String(cqContent)); currentSolutionBs.addBinding(s, RyaToRdfConversions.convertValue(rURI)); } else { throw new IllegalArgumentException("Invalid row."); } } for (String s : unCommonVar) { currentSolutionBs.addBinding(s, currentBs.getValue(s)); } return currentSolutionBs; }
From source file:mvm.rya.indexing.accumulo.freetext.ColumnPrefixes.java
License:Apache License
public static Text removePrefix(Text termWithPrefix) { Text temp = new Text(); temp.set(termWithPrefix.getBytes(), 2, termWithPrefix.getLength() - 2); return temp;//from ww w. ja va 2 s. c o m }
From source file:mvm.rya.joinselect.mr.CardinalityMapperTest.java
License:Apache License
@Test public void testOutput() throws InterruptedException, IOException { String s = "urn:gem:etype#1234"; String p = "urn:gem#pred"; Text t1 = new Text(TripleValueType.subject.name() + DELIM + s + DELIM + 1); Text t2 = new Text(TripleValueType.predicate.name() + DELIM + p + DELIM + 2); Text t3 = new Text(TripleValueType.subjectpredicate.name() + DELIM + s + DELIM + p + DELIM + 3); byte[] b = new byte[0]; byte[] c = "25".getBytes(); byte[] d = "47".getBytes(); byte[] e = "15".getBytes(); Key key1 = new Key(t1.getBytes(), b, b, b, 1); Key key2 = new Key(t2.getBytes(), b, b, b, 1); Key key3 = new Key(t3.getBytes(), b, b, b, 1); Value val1 = new Value(c); Value val2 = new Value(d); Value val3 = new Value(e); // System.out.println("Keys are " + key1 + " and " + key2); new MapDriver<Key, Value, CompositeType, TripleCard>() .withMapper(new JoinSelectProspectOutput.CardinalityMapper()).withInput(key1, val1) .withInput(key2, val2).withInput(key3, val3) .withOutput(new CompositeType(s, 1), new TripleCard(new CardinalityType(25, "subject", 1))) .withOutput(new CompositeType(p, 1), new TripleCard(new CardinalityType(47, "predicate", 2))) .withOutput(new CompositeType(s + DELIM + p, 1), new TripleCard(new CardinalityType(15, "subjectpredicate", 3))) .runTest();/*from www . j a v a 2 s. co m*/ }
From source file:mvm.rya.joinselect.mr.JoinSelectProspectOutputTest.java
License:Apache License
@Test public void testOutput() throws InterruptedException, IOException { String s = "urn:gem:etype#1234"; String p = "urn:gem#pred"; String ts = "798497748386999999"; Text t1 = new Text(TripleValueType.subject.name() + DELIM + s + DELIM + 1); Text t2 = new Text(TripleValueType.predicate.name() + DELIM + p + DELIM + 2); Text t3 = new Text(TripleValueType.subjectpredicate.name() + DELIM + s + DELIM + p + DELIM + ts); byte[] b = new byte[0]; byte[] c = "25".getBytes(); byte[] d = "47".getBytes(); byte[] e = "15".getBytes(); Key key1 = new Key(t1.getBytes(), b, b, b, 1); Key key2 = new Key(t2.getBytes(), b, b, b, 1); Key key3 = new Key(t3.getBytes(), b, b, b, 1); Value val1 = new Value(c); Value val2 = new Value(d); Value val3 = new Value(e); // System.out.println("Keys are " + key1 + " and " + key2); new MapDriver<Key, Value, CompositeType, TripleCard>() .withMapper(new JoinSelectProspectOutput.CardinalityMapper()).withInput(key1, val1) .withInput(key2, val2).withInput(key3, val3) .withOutput(new CompositeType(s, 1), new TripleCard(new CardinalityType(25, "subject", 1))) .withOutput(new CompositeType(p, 1), new TripleCard(new CardinalityType(47, "predicate", 2))) .withOutput(new CompositeType(s + DELIM + p, 1), new TripleCard(new CardinalityType(15, "subjectpredicate", Long.parseLong(ts)))) .runTest();// www .j ava 2 s . c o m }
From source file:net.mooncloud.hadoop.hive.ql.udf.UDFMd5.java
License:Apache License
/** * Convert String to md5//from ww w . ja va 2 s. c om */ public Text evaluate(Text n) { if (n == null) { return null; } digest.reset(); digest.update(n.getBytes(), 0, n.getLength()); byte[] md5Bytes = digest.digest(); String md5Hex = Hex.encodeHexString(md5Bytes); result.set(md5Hex); return result; }
From source file:net.mooncloud.hadoop.hive.ql.udf.UDFRSASign.java
License:Apache License
public BytesWritable evaluate(Text n, Text privateKey) { if (n == null || privateKey == null) { return null; }/*from www. j av a 2 s . c o m*/ try { byte[] bytes = new byte[privateKey.getLength()]; System.arraycopy(privateKey.getBytes(), 0, bytes, 0, privateKey.getLength()); byte[] decoded = Base64.decodeBase64(bytes); result = new BytesWritable(RSAUtils.sign(n.getBytes(), decoded)); } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:net.mooncloud.hadoop.hive.ql.udf.UDFRSAVerify.java
License:Apache License
public BooleanWritable evaluate(Text n, Text sign, Text publicKey) { if (n == null || sign == null || publicKey == null) { return null; }/*from ww w.j av a2 s.co m*/ try { byte[] publicKeybytes = new byte[publicKey.getLength()]; System.arraycopy(publicKey.getBytes(), 0, publicKeybytes, 0, publicKey.getLength()); byte[] publicKeydecoded = Base64.decodeBase64(publicKeybytes); byte[] signbytes = new byte[sign.getLength()]; System.arraycopy(sign.getBytes(), 0, signbytes, 0, sign.getLength()); byte[] signdecoded = Base64.decodeBase64(signbytes); result = new BooleanWritable(RSAUtils.verify(n.getBytes(), publicKeydecoded, signdecoded)); } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:net.mooncloud.hadoop.hive.ql.udf.UDFUnbase64.java
License:Apache License
public BytesWritable evaluate(Text value) { if (value == null) { return null; }/* w w w . ja va2 s. co m*/ byte[] bytes = new byte[value.getLength()]; System.arraycopy(value.getBytes(), 0, bytes, 0, value.getLength()); byte[] decoded = Base64.decodeBase64(bytes); result.set(decoded, 0, decoded.length); return result; }
From source file:nl.bioinf.wvanhelvoirt.HadoopPhredCalculator.NReadRecordReader.java
License:Open Source License
/** * Override method that if the file has not already been read, reads it into memory, so that a call to * getCurrentValue() will return the lines this file as Text. Then, returns true. If it has already been read, * then returns false without updating any internal state. * * @return Boolean whether the file was read or not. * @throws IOException If there is an error reading the file. * @throws InterruptedException If there is an error. */// w w w .j av a 2 s. com @Override public boolean nextKeyValue() throws IOException, InterruptedException { // Initialize key and value. if (this.key == null) { this.key = new LongWritable(); } if (this.value == null) { this.value = new Text(); } // Get the key and value. this.key.set(this.pos); this.value.clear(); Text endline = new Text("\n"); int newSize = 0; for (int i = 0; i < this.NLINESTOPROCESS; i++) { Text v = new Text(); while (this.pos < this.end) { newSize = this.in.readLine(v, this.maxLineLength, Math.max((int) Math.min(Integer.MAX_VALUE, this.end - this.pos), this.maxLineLength)); this.value.append(v.getBytes(), 0, v.getLength()); this.value.append(endline.getBytes(), 0, endline.getLength()); if (newSize == 0) { break; } this.pos += newSize; if (newSize < this.maxLineLength) { break; } } } // If newSize is still zero, return false, else true. if (newSize == 0) { this.key = null; this.value = null; return false; } else { return true; } }
From source file:org.apache.accumulo.core.client.admin.FindMax.java
License:Apache License
private static Text findMidPoint(Text minBS, Text maxBS) { ByteArrayOutputStream startOS = new ByteArrayOutputStream(); startOS.write(0); // add a leading zero so bigint does not think its negative startOS.write(minBS.getBytes(), 0, minBS.getLength()); ByteArrayOutputStream endOS = new ByteArrayOutputStream(); endOS.write(0);// add a leading zero so bigint does not think its negative endOS.write(maxBS.getBytes(), 0, maxBS.getLength()); // make the numbers of the same magnitude if (startOS.size() < endOS.size()) appendZeros(startOS, endOS.size() - startOS.size()); else if (endOS.size() < startOS.size()) appendZeros(endOS, startOS.size() - endOS.size()); BigInteger min = new BigInteger(startOS.toByteArray()); BigInteger max = new BigInteger(endOS.toByteArray()); BigInteger mid = max.subtract(min).divide(BigInteger.valueOf(2)).add(min); byte[] ba = mid.toByteArray(); Text ret = new Text(); if (ba.length == startOS.size()) { if (ba[0] != 0) throw new RuntimeException(); // big int added a zero so it would not be negative, drop it ret.set(ba, 1, ba.length - 1);/* w w w .j a v a 2 s.co m*/ } else { int expLen = Math.max(minBS.getLength(), maxBS.getLength()); // big int will drop leading 0x0 bytes for (int i = ba.length; i < expLen; i++) { ret.append(new byte[] { 0 }, 0, 1); } ret.append(ba, 0, ba.length); } // remove trailing 0x0 bytes while (ret.getLength() > 0 && ret.getBytes()[ret.getLength() - 1] == 0 && ret.compareTo(minBS) > 0) { Text t = new Text(); t.set(ret.getBytes(), 0, ret.getLength() - 1); ret = t; } return ret; }