使用teragen产生数据

使用Teragen产生数据,使用 jar 包路径如下:
/your hadoop path/share/hadoop/mapreduce2/hadoop-mapreduce-examples-2.6.0-cdh5.9.3.jar

$ hadoop jar hadoop-mapreduce-examples-2.6.0-cdh5.9.3.jar teragen 参数1 参数2

Teragen 参数解释

  • 参数1:表示需要产生行数。如1G = 102410241024/100=10737418行
  • 参数2:表示放置文件的位置

默认 teragen 启动 map 数量为 2,可以指定 map 数量,-Dmapred.map.tasks=map,如:

$ hadoop jar hadoop-mapreduce-examples-2.6.0-cdh5.9.3.jar teragen -Dmapred.map.tasks=10 参数1 参数2

map 数量影响产生文件个数

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这个问题我可以回答。首先,你需要在IDEA中创建一个Java项目。然后,你需要下载并安装Hadoop。接下来,你可以使用Hadoop自带的数据生成器工具TeraGen来生成数据。 以下是一个示例代码,你可以根据自己的需求修改: ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; import org.apache.hadoop.mapreduce.lib.input.TextInputFormat; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.util.GenericOptionsParser; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; public class DataGenerator implements Tool { private Configuration conf; public static void main(String[] args) throws Exception { int res = ToolRunner.run(new DataGenerator(), args); System.exit(res); } public int run(String[] args) throws Exception { Path outputPath = new Path(args[0]); int numRecords = Integer.parseInt(args[1]); int numMappers = Integer.parseInt(args[2]); int numReducers = Integer.parseInt(args[3]); Job job = Job.getInstance(getConf()); job.setJobName("DataGenerator"); job.setJarByClass(DataGenerator.class); // Set input/output format job.setInputFormatClass(TextInputFormat.class); job.setOutputFormatClass(TextOutputFormat.class); // Set mapper/reducer class job.setMapperClass(DataGeneratorMapper.class); job.setReducerClass(DataGeneratorReducer.class); // Set number of mappers/reducers job.setNumReduceTasks(numReducers); // Set output key/value class job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); // Set input/output path TextInputFormat.setInputPaths(job, new Path("input")); TextOutputFormat.setOutputPath(job, outputPath); // Set number of records to generate job.getConfiguration().setInt("numRecords", numRecords); // Set number of mappers job.getConfiguration().setInt("mapred.map.tasks", numMappers); return job.waitForCompletion(true) ? 0 : 1; } public void setConf(Configuration conf) { this.conf = conf; } public Configuration getConf() { return conf; } public static class DataGeneratorMapper extends Mapper<Object, Text, Text, Text> { private static final String[] PHONES = {"135", "136", "137", "138", "139", "150", "151", "152", "157", "158", "159", "182", "183", "184", "187", "188"}; private static final String[] MODELS = {"iPhone", "Samsung", "Huawei", "Xiaomi", "Oppo", "Vivo", "OnePlus", "Realme", "Lenovo", "Asus"}; private static final String[] OS = {"iOS", "Android", "Windows", "macOS", "Linux", "Chrome OS", "Ubuntu"}; private int numRecords; @Override protected void setup(Context context) throws IOException, InterruptedException { super.setup(context); numRecords = context.getConfiguration().getInt("numRecords", 1000000); } @Override protected void map(Object key, Text value, Context context) throws IOException, InterruptedException { Random random = new Random(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); for (int i = 0; i < numRecords; i++) { int id = i; String date = dateFormat.format(new Date(random.nextLong())); String phone = PHONES[random.nextInt(PHONES.length)] + String.format("%08d", random.nextInt(100000000)); String model = MODELS[random.nextInt(MODELS.length)]; String os = OS[random.nextInt(OS.length)]; context.write(new Text(String.valueOf(id)), new Text(date + "," + phone + "," + model + "," + os)); } } } public static class DataGeneratorReducer extends Reducer<Text, Text, Text, Text> { @Override protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { for (Text value : values) { context.write(key, value); } } } } ``` 在这个代码中,我们使用了一个Mapper来生成数据。在Mapper中,我们使用了一个随机数生成器和SimpleDateFormat来生成日期和手机号码。我们还定义了一些模型和操作系统,用于生成“型号”和“操作系统”字段。 你可以通过以下命令来运行这个程序: ``` hadoop jar DataGenerator.jar /output 10000000 10 1 ``` 这会生成一个包含1000万行数据的文件,文件名为part-r-00000,位于/output目录下。其中,第二个参数表示要生成的记录数量,第三个参数表示要使用的Mapper数量,第四个参数表示要使用的Reducer数量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值