@Override public SyncPolicy create(String sensor, WriterConfiguration config) { try { //we do a deep clone of the SyncPolicy via kryo serialization. This gives us a fresh policy //to work with. The reason we need a fresh policy is that we want to ensure each handler //(one handler per task & sensor type and one handler per file) has its own sync policy. // Reusing a sync policy is a bad idea, so we need to clone it here. Unfortunately the // SyncPolicy object does not implement Cloneable, so we'll need to clone it via serialization //to get a fresh policy object. Note: this would be expensive if it was in the critical path, // but should be called infrequently (once per sync). byte[] serializedForm = SerDeUtils.toBytes(syncPolicy); return SerDeUtils.fromBytes(serializedForm, SyncPolicy.class); } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } } }
/** * Serializes the given Object into bytes. * */ @Override public byte[] apply(Object o) { return toBytes(o); } }
/** * Deserializes the given bytes. * * @param bytes the function argument * @return the function result */ @Override public T apply(byte[] bytes) { return fromBytes(bytes, clazz); } }
public void setProfileValue(Object profileValue) { this.profileValue = SerDeUtils.toBytes(profileValue); } }
@Override public void write(byte[] obj, Optional<String> output, Configuration hadoopConfig) throws IOException { System.out.println(SerDeUtils.fromBytes(obj, Object.class)); } }
void write(byte[] obj, Optional<String> output, Configuration hadoopConfig) throws IOException; }
@Override public Object load(String s) throws Exception { if(StringUtils.isEmpty(s)) { return null; } Path p = new Path(s); if(fs.exists(p)) { try(InputStream is = new BufferedInputStream(fs.open(p))) { byte[] serialized = IOUtils.toByteArray(is); if(serialized.length > 0) { Object ret = SerDeUtils.fromBytes(serialized, Object.class); return ret; } } } return null; } }
@Override public ColumnList columns(ProfileMeasurement measurement) { ColumnList cols = new ColumnList(); cols.addColumn(columnFamilyBytes, getColumnQualifier("value"), SerDeUtils.toBytes(measurement.getProfileValue())); return cols; }
measurement.withProfileValue(SerDeUtils.fromBytes(value, clazz)); values.add(measurement);
public void assertDataIsReadCorrectly(String filename) throws IOException { try(BufferedOutputStream bos = new BufferedOutputStream(fs.create(new Path(filename), true))) { IOUtils.write(SerDeUtils.toBytes(data), bos); } List<String> readData = (List<String>) StellarProcessorUtils.run("OBJECT_GET(loc)", ImmutableMap.of("loc", filename)); Assert.assertEquals(readData, data); Assert.assertTrue(ObjectGet.cache.asMap().containsKey(filename)); }
public ProfileMeasurement toProfileMeasurement() { ProfilePeriod period = ProfilePeriod.fromPeriodId(periodId, durationMillis, TimeUnit.MILLISECONDS); ProfileMeasurement measurement = new ProfileMeasurement() .withProfileName(profileName) .withEntity(entity) .withPeriod(period) .withProfileValue(SerDeUtils.fromBytes(profileValue, Object.class)); return measurement; }
@Test public void testFloat() { final Float expected = 2.2F; byte[] raw = SerDeUtils.toBytes(expected); { float actual = SerDeUtils.fromBytes(raw, Float.class); assertEquals(expected, actual, 0.01); } { float actual = (float) SerDeUtils.fromBytes(raw, Object.class); assertEquals(expected, actual, 0.01); } }
public ProfileMeasurementAdapter(ProfileMeasurement measurement) { this.profileName = measurement.getProfileName(); this.entity = measurement.getEntity(); this.periodId = measurement.getPeriod().getPeriod(); this.durationMillis = measurement.getPeriod().getDurationMillis(); this.profileValue = SerDeUtils.toBytes(measurement.getProfileValue()); }
errorStats.addValue(rawError); timeStats.addValue(timeElapsed); sizeStats.addValue(SerDeUtils.toBytes(hllp).length);
@Test public void testDouble() { final double expected = 2.0; byte[] raw = SerDeUtils.toBytes(expected); { double actual = SerDeUtils.fromBytes(raw, Double.class); assertEquals(expected, actual, 0.01); } { double actual = (double) SerDeUtils.fromBytes(raw, Object.class); assertEquals(expected, actual, 0.01); } }