2010年2月28日日曜日

本いろいろ

久しぶりにまとめて本を買った。

1冊目はデブサミで@yoshioriが紹介していて
おもしろそうだと思い即購入。



2冊目はtwitterでも結構話が出ていて第2版になっていたので購入。




しばらくは電車で暇を持て余さなくて済みそうだ。。。

2010年2月9日火曜日

MapReduceのテスト

MapReduceのテストはHadopp本にはMockitoを使う良いあるよと書いております。
でも、やはり0.19.xが前提なので情報が古い。
ってことで、0.20.xで以下のような感じで書いてみた。



package example.test;

import java.util.Arrays;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;

import static org.mockito.Mockito.*;

import junit.framework.TestCase;

public class CountTest extends TestCase {
    @SuppressWarnings("unchecked")
    public void testMapLongWritableTextContext() {
        // Mapのテスト
        Map map = new Map();
        Mapper.Context map_context = mock(Mapper.Context.class);

        Text value = null;
        try {
            value = new Text("test1");
            map.map(null, value, map_context);
            verify(map_context, times(1)).write(new Text("test1"), new LongWritable(1));

            value = new Text("test2");
            map.map(null, value, map_context);
            verify(map_context, times(0)).write(new Text("test1"), new LongWritable(0));
        } catch (Exception e) {
            fail(e.getMessage());
        }


        // Reduceのテスト
        Reduce reduce = new Reduce();
        Reducer.Context red_context = mock(Reducer.Context.class);

        Text key = null;
        Iterable<longwritable> values;
        try {
            key = new Text("test1");
            values = Arrays.asList(new LongWritable(2));
            reduce.reduce(key, values, red_context);
            verify(red_context, times(1)).write(new Text("test1"), new LongWritable(2));

            key = new Text("test2");
            values = Arrays.asList(new LongWritable(2), new LongWritable(2));
            reduce.reduce(key, values, red_context);
            verify(red_context, times(1)).write(new Text("test2"), new LongWritable(4));
        } catch (Exception e) {
            fail(e.getMessage());
        }
    }
}

2010年2月2日火曜日

HDFSへの書き込み/読み込み方法

下記のような感じで。

Configuration conf = new Configuration();
// ユーザを指定する場合
conf.set(UnixUserGroupInformation.UGI_PROPERTY_NAME, "hoge,supergroup");
FileSystem fs = FileSystem.get(new URI("hdfs://hoge:9000/"), conf);

// ディレクトリの作成など
try {
  fs.getFileStatus(new Path("hoge"));
} catch (FileNotFoundException e) {
  fs.mkdirs(new Path("hoge"));
}

// ファイルへの出力
// ex. 最後のフラグは上書き
FSDataOutputStream dos = fs.create(new Path("hoge/hoge.txt"), true);
dos.writeChars("テスト\n");
dos.close();

みたいに。