HFileRecordWriter extends RecordWriter<BytesWritable, BytesWritable> { @Override public void write(BytesWritable key, BytesWritable value) throws IOException, InterruptedException { byte[] row = new byte[key.getLength()]; byte[] val = new byte[value.getLength()]; for (int i = 0; i < row.length; i++) { row[i] = key.getBytes()[i]; } for (int i = 0; i < val.length; i++) { val[i] = value.getBytes()[i]; } writer.append(new KeyValue(row, Bytes.toBytes("cf"), Bytes.toBytes(""), val)); } HFileRecordWriter(StoreFile.Writer writer); @Override void write(BytesWritable key, BytesWritable value); @Override void close(TaskAttemptContext context);  }
@Test public void testWrite() throws Exception { Configuration conf = new Configuration(); HColumnDescriptor columnDescriptor = new HColumnDescriptor(); columnDescriptor.setBlockCacheEnabled(false); FileSystem fs = FileSystem.get(conf); int blockSize = conf.getInt(Constants.HFILE_BLOCKSIZE, 16384); final StoreFile.Writer writer = new StoreFile.WriterBuilder(conf, new CacheConfig(conf, columnDescriptor), fs, blockSize) .withFilePath(new Path(tempFile.toURI())) .build(); RecordWriter<BytesWritable, BytesWritable> hfileWriter = new HFileRecordWriter(writer); List<String> keys = Lists.newArrayList(); List<String> values = Lists.newArrayList(); for (int i = 0; i < 100; ++i) { String key = String.format("%03d", i); String val = "value " + i; keys.add(key); values.add(val); hfileWriter.write(new BytesWritable(key.getBytes()), new BytesWritable(val.getBytes())); } hfileWriter.close(null); HFile.Reader reader = HFile.createReader(fs, new Path(tempFile.toURI()), new CacheConfig(conf, columnDescriptor)); HFileScanner scanner = reader.getScanner(false, false, false); boolean valid = scanner.seekTo(); List<String> gotKeys = Lists.newArrayListWithCapacity(keys.size()); List<String> gotValues = Lists.newArrayListWithCapacity(values.size()); while(valid) { KeyValue keyValue = scanner.getKeyValue(); gotKeys.add(new String(keyValue.getRow())); gotValues.add(new String(keyValue.getValue())); valid = scanner.next(); } assertEquals(keys, gotKeys); assertEquals(values, gotValues); reader.close(); }