use of org.apache.hadoop.io.DoubleWritable in project camel by apache.
the class HdfsConsumerTest method testReadDouble.
@Test
public void testReadDouble() throws Exception {
if (!canTest()) {
return;
}
final Path file = new Path(new File("target/test/test-camel-double").getAbsolutePath());
Configuration conf = new Configuration();
FileSystem fs1 = FileSystem.get(file.toUri(), conf);
SequenceFile.Writer writer = createWriter(fs1, conf, file, NullWritable.class, DoubleWritable.class);
NullWritable keyWritable = NullWritable.get();
DoubleWritable valueWritable = new DoubleWritable();
double value = 3.1415926535;
valueWritable.set(value);
writer.append(keyWritable, valueWritable);
writer.sync();
writer.close();
MockEndpoint resultEndpoint = context.getEndpoint("mock:result", MockEndpoint.class);
resultEndpoint.expectedMessageCount(1);
context.addRoutes(new RouteBuilder() {
public void configure() {
from("hdfs:localhost/" + file.toUri() + "?fileSystemType=LOCAL&fileType=SEQUENCE_FILE&initialDelay=0").to("mock:result");
}
});
context.start();
resultEndpoint.assertIsSatisfied();
}
use of org.apache.hadoop.io.DoubleWritable in project camel by apache.
the class HdfsProducerTest method testWriteDouble.
@Test
public void testWriteDouble() throws Exception {
if (!canTest()) {
return;
}
Double aDouble = 12.34D;
template.sendBody("direct:write_double", aDouble);
Configuration conf = new Configuration();
Path file1 = new Path("file:///" + TEMP_DIR.toUri() + "/test-camel-double");
FileSystem fs1 = FileSystem.get(file1.toUri(), conf);
SequenceFile.Reader reader = new SequenceFile.Reader(fs1, file1, conf);
Writable key = (Writable) ReflectionUtils.newInstance(reader.getKeyClass(), conf);
Writable value = (Writable) ReflectionUtils.newInstance(reader.getValueClass(), conf);
reader.next(key, value);
Double rDouble = ((DoubleWritable) value).get();
assertEquals(rDouble, aDouble);
IOHelper.close(reader);
}
use of org.apache.hadoop.io.DoubleWritable in project camel by apache.
the class HdfsConsumerTest method testReadDouble.
@Test
public void testReadDouble() throws Exception {
if (!canTest()) {
return;
}
final Path file = new Path(new File("target/test/test-camel-double").getAbsolutePath());
Configuration conf = new Configuration();
SequenceFile.Writer writer = createWriter(conf, file, NullWritable.class, DoubleWritable.class);
NullWritable keyWritable = NullWritable.get();
DoubleWritable valueWritable = new DoubleWritable();
double value = 3.1415926535;
valueWritable.set(value);
writer.append(keyWritable, valueWritable);
writer.sync();
writer.close();
MockEndpoint resultEndpoint = context.getEndpoint("mock:result", MockEndpoint.class);
resultEndpoint.expectedMessageCount(1);
context.addRoutes(new RouteBuilder() {
public void configure() {
from("hdfs2:localhost/" + file.toUri() + "?fileSystemType=LOCAL&fileType=SEQUENCE_FILE&initialDelay=0").to("mock:result");
}
});
context.start();
resultEndpoint.assertIsSatisfied();
}
use of org.apache.hadoop.io.DoubleWritable in project incubator-systemml by apache.
the class MapReduceTool method pickValueWeight.
public static double[] pickValueWeight(String dir, NumItemsByEachReducerMetaData metadata, double p, boolean average) throws IOException {
long[] counts = metadata.getNumItemsArray();
long[] ranges = new long[counts.length];
ranges[0] = counts[0];
for (int i = 1; i < counts.length; i++) ranges[i] = ranges[i - 1] + counts[i];
long total = ranges[ranges.length - 1];
// do averaging only if it is asked for; and sum_wt is even
average = average && (total % 2 == 0);
int currentPart = 0;
double cum_weight = 0;
long pos = (long) Math.ceil(total * p);
while (ranges[currentPart] < pos) {
currentPart++;
cum_weight += ranges[currentPart];
}
int offset;
if (currentPart > 0)
offset = (int) (pos - ranges[currentPart - 1] - 1);
else
offset = (int) pos - 1;
Path path = new Path(dir);
FileSystem fs = IOUtilFunctions.getFileSystem(path);
FileStatus[] files = fs.listStatus(path);
Path fileToRead = null;
for (FileStatus file : files) if (file.getPath().toString().endsWith(Integer.toString(currentPart))) {
fileToRead = file.getPath();
break;
}
if (fileToRead == null)
throw new RuntimeException("cannot read partition " + currentPart);
int buffsz = 64 * 1024;
DoubleWritable readKey = new DoubleWritable();
IntWritable readValue = new IntWritable();
FSDataInputStream currentStream = null;
double ret = -1;
try {
currentStream = fs.open(fileToRead, buffsz);
boolean contain0s = false;
long numZeros = 0;
if (currentPart == metadata.getPartitionOfZero()) {
contain0s = true;
numZeros = metadata.getNumberOfZero();
}
ReadWithZeros reader = new ReadWithZeros(currentStream, contain0s, numZeros);
int numRead = 0;
while (numRead <= offset) {
reader.readNextKeyValuePairs(readKey, readValue);
numRead += readValue.get();
cum_weight += readValue.get();
}
ret = readKey.get();
if (average) {
if (numRead <= offset + 1) {
reader.readNextKeyValuePairs(readKey, readValue);
cum_weight += readValue.get();
ret = (ret + readKey.get()) / 2;
}
}
} finally {
IOUtilFunctions.closeSilently(currentStream);
}
return new double[] { ret, (average ? -1 : readValue.get()), (average ? -1 : cum_weight) };
}
use of org.apache.hadoop.io.DoubleWritable in project nifi by apache.
the class TestConvertAvroToORC method test_onTrigger_complex_record.
@Test
public void test_onTrigger_complex_record() throws Exception {
Map<String, Double> mapData1 = new TreeMap<String, Double>() {
{
put("key1", 1.0);
put("key2", 2.0);
}
};
GenericData.Record record = TestNiFiOrcUtils.buildComplexAvroRecord(10, mapData1, "DEF", 3.0f, Arrays.asList(10, 20));
DatumWriter<GenericData.Record> writer = new GenericDatumWriter<>(record.getSchema());
DataFileWriter<GenericData.Record> fileWriter = new DataFileWriter<>(writer);
ByteArrayOutputStream out = new ByteArrayOutputStream();
fileWriter.create(record.getSchema(), out);
fileWriter.append(record);
// Put another record in
Map<String, Double> mapData2 = new TreeMap<String, Double>() {
{
put("key1", 3.0);
put("key2", 4.0);
}
};
record = TestNiFiOrcUtils.buildComplexAvroRecord(null, mapData2, "XYZ", 4L, Arrays.asList(100, 200));
fileWriter.append(record);
fileWriter.flush();
fileWriter.close();
out.close();
Map<String, String> attributes = new HashMap<String, String>() {
{
put(CoreAttributes.FILENAME.key(), "test");
}
};
runner.enqueue(out.toByteArray(), attributes);
runner.run();
runner.assertAllFlowFilesTransferred(ConvertAvroToORC.REL_SUCCESS, 1);
// Write the flow file out to disk, since the ORC Reader needs a path
MockFlowFile resultFlowFile = runner.getFlowFilesForRelationship(ConvertAvroToORC.REL_SUCCESS).get(0);
assertEquals("CREATE EXTERNAL TABLE IF NOT EXISTS complex_record " + "(myInt INT, myMap MAP<STRING, DOUBLE>, myEnum STRING, myLongOrFloat UNIONTYPE<BIGINT, FLOAT>, myIntList ARRAY<INT>)" + " STORED AS ORC", resultFlowFile.getAttribute(ConvertAvroToORC.HIVE_DDL_ATTRIBUTE));
assertEquals("2", resultFlowFile.getAttribute(ConvertAvroToORC.RECORD_COUNT_ATTRIBUTE));
assertEquals("test.orc", resultFlowFile.getAttribute(CoreAttributes.FILENAME.key()));
byte[] resultContents = runner.getContentAsByteArray(resultFlowFile);
FileOutputStream fos = new FileOutputStream("target/test1.orc");
fos.write(resultContents);
fos.flush();
fos.close();
Configuration conf = new Configuration();
FileSystem fs = FileSystem.getLocal(conf);
Reader reader = OrcFile.createReader(new Path("target/test1.orc"), OrcFile.readerOptions(conf).filesystem(fs));
RecordReader rows = reader.rows();
Object o = rows.next(null);
assertNotNull(o);
assertTrue(o instanceof OrcStruct);
TypeInfo resultSchema = TestNiFiOrcUtils.buildComplexOrcSchema();
StructObjectInspector inspector = (StructObjectInspector) OrcStruct.createObjectInspector(resultSchema);
// Check some fields in the first row
Object intFieldObject = inspector.getStructFieldData(o, inspector.getStructFieldRef("myInt"));
assertTrue(intFieldObject instanceof IntWritable);
assertEquals(10, ((IntWritable) intFieldObject).get());
Object mapFieldObject = inspector.getStructFieldData(o, inspector.getStructFieldRef("myMap"));
assertTrue(mapFieldObject instanceof Map);
Map map = (Map) mapFieldObject;
Object mapValue = map.get(new Text("key1"));
assertNotNull(mapValue);
assertTrue(mapValue instanceof DoubleWritable);
assertEquals(1.0, ((DoubleWritable) mapValue).get(), Double.MIN_VALUE);
mapValue = map.get(new Text("key2"));
assertNotNull(mapValue);
assertTrue(mapValue instanceof DoubleWritable);
assertEquals(2.0, ((DoubleWritable) mapValue).get(), Double.MIN_VALUE);
}
Aggregations