有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用Hadoop处理大型数据集时,在Mapper中排序数据的最佳方法是什么

我正在尝试使用Hadoop从一个巨大的数据集中找到前10部电影。我正在使用Map Reduce方法。我使用了一个本地集合,即TreeMap来对数据进行排序,但不建议使用这种方法。在Mapper中处理大量数据时,我可以知道正确的排序方法吗?我正在给我的映射器和减速器代码

映射程序代码

public class HighestViewedMoviesMapper extends Mapper<Object, Text, NullWritable, Text> {
    private TreeMap<Integer, Text> highestView = new TreeMap<Integer, Text>();

    @Override
    public void map( Object key, Text values, Context context ) throws IOException, InterruptedException {
        String data = values.toString();
        String[] field = data.split( "::", -1 );
        if ( null != field && field.length == 2 ) {
            int views = Integer.parseInt( field[1] );
            highestView.put( views, new Text( field[0] + "::" + field[1] ) );
            if ( highestView.size() > 10 ) {
                highestView.remove( highestView.firstKey() );
            }
        }
    }

    @Override
    protected void cleanup( Context context ) throws IOException, InterruptedException {
        for ( Map.Entry<Integer, Text> entry : highestView.entrySet() ) {
            context.write( NullWritable.get(), entry.getValue() );
        }
    }
}

减速机代码

public class HighestViewMoviesReducer extends Reducer<NullWritable, Text, NullWritable, Text> {
    private TreeMap<Integer, Text> highestView = new TreeMap<Integer, Text>();

    public void reduce( NullWritable key, Iterable<Text> values, Context context )
        throws IOException, InterruptedException {
        for ( Text value : values ) {
            String data = value.toString();
            String[] field = data.split( "::", -1 );
            if ( field.length == 2 ) {
                highestView.put( Integer.parseInt( field[1] ), new Text( value ) );
                if ( highestView.size() > 10 ) {
                    highestView.remove( highestView.firstKey() );
                }
            }
        }
        for ( Text t : highestView.descendingMap().values() ) {
            context.write( NullWritable.get(), t );
        }
    }
}

有谁能告诉我最好的方法吗?提前谢谢


共 (0) 个答案