gpt4 book ai didi

hadoop - 编写mapreduce代码以搜索模式

转载 作者:行者123 更新时间:2023-12-02 21:16:58 25 4
gpt4 key购买 nike

customer_id, server_id, code
12342344, 1232, 3
12345456, 1433, 2
16345436, 2343, 4
12245456, 1434, 3
11145456, 1436, 2

如果我正在 hive 上运行此查询:
select * from table where code=3;

用mapreduce编写的代码是什么,在哪里可以找到?

换句话说,我该如何编写mapreduce作业以给出与上面相同的查询结果?

谢谢

最佳答案

您可以从hive运行EXPLAIN select * from table where code=3;来查看hive的查询执行计划,但是hive不会为任何查询输出任何mapreduce代码。还要检查YSmart-Another SQL-to-MapReduce Translator-查看用于SQL的mapreduce作业-我从未使用过,但是您可以尝试一下:

这是示例,您可以尝试对其他不同的查询进行修改以获得一个提示。基本上,您将过滤map类中的数据,并在reduce类中运行一些聚合。但在此示例中,您不需要reduce类,因为它是从数据的简单映射。您将需要先进行单词计数(hadooop的helloworld示例),以了解如何运行此程序以及在何处查找输出等。

输出:

12245456, 1434, 3   
12342344, 1232, 3

码:
public class customers{

public static class customersMapper extends Mapper<Object, Text, Text, Text> {

@Override
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {

//this code should be converted to Int - try it for your self
String code = value.toString().split(",")[2].trim();

//this line should be chnaged if code variable is converted to Int
if (code.equals("3")){ // only map rec where code=3
context.write(value,new Text(""));
}
}
}

public static void main(String[] args) throws Exception {

Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "get-customer-with-code=3");

job.setJarByClass(customers.class);
job.setMapperClass(customersMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);

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

job.waitForCompletion(true);
}
}

关于hadoop - 编写mapreduce代码以搜索模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38540398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com