【问题标题】:How to uniformly sample a big graph?如何对大图进行均匀采样?
【发布时间】:2014-01-21 00:13:30
【问题描述】:

我有一个大约 400 万个节点的大图。该图由两个文件组成,一个包含节点名称,一个包含边(每条线代表一条边)。我想对图形节点进行统一采样,并得出一个占整个图形 15% 的样本。考虑到图表的大小,生成此类样本的最佳(或可能)方法是什么?

【问题讨论】:

  • 您只想对这些节点定义的节点或子图(节点 + 对应边)进行采样?
  • 其实就是子图,也就是采样节点形成的图。

标签: hadoop mapreduce graph-algorithm random-sample


【解决方案1】:

使用这个java代码随机选择15%的顶点:

import java.io.IOException;
import java.util.*;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

public class RandomSample {

 public static class Map extends Mapper<LongWritable, Text, Text, Text> {
    private Text word = new Text();

    public void map(LongWritable key, Text value, Context context)
    throws IOException, InterruptedException {
        if (Math.random()<0.15)
            context.write(value,null);
        else
            context.write(null,null);
    context.write(value,null);
    } 
 }

 public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();

    Job job = new Job(conf, "randomsample");
    job.setJarByClass(RandomSample.class);

    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);

    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(Text.class);
    job.setInputFormatClass(TextInputFormat.class);
    job.setOutputFormatClass(TextOutputFormat.class);

    job.setNumReduceTasks(0);

    FileInputFormat.addInputPath(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));

    job.waitForCompletion(true);
 }

}

并使用这个 bash 脚本来运行它

echo "Running Job"
hadoop jar RandomSample.jar RandomSample $1 tmp
echo "copying result to local path (RandomSample)"
hadoop fs -getmerge tmp RandomSample
echo "Clean up"
hadoop fs -rmr tmp

例如,如果我们将脚本命名为 random_sample.sh,要从文件夹 /example/ 中选择 15%,只需运行

./random_sample.sh /example/

然后,您可以对第二个文件使用简单的grep 操作来仅选择包含随机选择的顶点的边

【讨论】:

    猜你喜欢
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多