Package com.wolfram.hadoop.dfs

Source Code of com.wolfram.hadoop.dfs.SequenceFileExportWriter

package com.wolfram.hadoop.dfs;


import com.wolfram.jlink.Expr;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.typedbytes.TypedBytesWritable;

import com.wolfram.hadoop.ExprUtil;

/**
* Helper class to speed up export of Mathematica expressions to sequence files.
*/
public class SequenceFileExportWriter {

  SequenceFile.Writer writer;
  TypedBytesWritable key;
  TypedBytesWritable value;

  public SequenceFileExportWriter(Configuration conf, Path path)
      throws Exception {
    FileSystem fs = FileSystem.get(conf);
    key = new TypedBytesWritable();
    value = new TypedBytesWritable();
    writer = SequenceFile.createWriter(fs, conf, path, TypedBytesWritable.class,
        TypedBytesWritable.class);
  }

  public void write(Expr[][] list) throws Exception {
    for (int i = 0; i < list.length; i++) {
      if (list[i].length != 2) {
        throw new IllegalArgumentException("Malformed record");
      }
      key.setValue(ExprUtil.fromExpr(list[i][0]));
      value.setValue(ExprUtil.fromExpr(list[i][1]));
      writer.append(key, value);
    }
  }

  public void close() throws Exception {
    writer.close();
  }
}
TOP

Related Classes of com.wolfram.hadoop.dfs.SequenceFileExportWriter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.