Class 如何在MapReduce中使用sqoop生成的类?
sqoop查询生成一个java文件,该文件包含一个类,该类包含在mapreduce中访问每一行的列数据的代码。(Sqoop导入是在文本中完成的,没有--as sequencefile选项,每个记录有一行,列之间有逗号) 但我们实际上如何使用它呢 我在这个类中发现了一个公共方法parse(),它将文本作为输入并填充该类的所有成员,因此为了练习,我修改了wordcount应用程序,将映射器中TextInputFormat中的一行文本转换为sqoop生成的类的指令。但这会在调用parse()方法时导致“unreported exception.com.cloudera.sqoop.lib.RecordParser.ParseError;必须被捕获或声明为抛出”Class 如何在MapReduce中使用sqoop生成的类?,class,import,mapreduce,sqoop,Class,Import,Mapreduce,Sqoop,sqoop查询生成一个java文件,该文件包含一个类,该类包含在mapreduce中访问每一行的列数据的代码。(Sqoop导入是在文本中完成的,没有--as sequencefile选项,每个记录有一行,列之间有逗号) 但我们实际上如何使用它呢 我在这个类中发现了一个公共方法parse(),它将文本作为输入并填充该类的所有成员,因此为了练习,我修改了wordcount应用程序,将映射器中TextInputFormat中的一行文本转换为sqoop生成的类的指令。但这会在调用parse()方法时导致
可以这样做吗?或者自定义InputFormat是使用每个记录中的数据填充类所必需的吗?好的,一旦发现这一点,这看起来很明显,但对于java初学者来说,这可能需要时间 首先配置您的项目: 只需在源文件夹中添加sqoop生成的.java文件。 我使用eclipse将其导入到我的类源文件夹中 然后确保正确配置了项目的java构建路径: 在项目的属性/java构建路径/libraries/addexternaljar中添加以下jar文件: (对于hadoop cdh4+): 然后调整mapreduce源代码: 首先配置它:
public int run(String [] args) throws exception
{
Job job = new Job(getConf());
job.setJarByClass(YourClass.class);
job.setMapperClass(SqoopImportMap.class);
job.setReducerClass(SqoopImprtReduce.class);
FileInputFormat.addInputPath((job,"hdfs_path_to_your_sqoop_imported_file"));
FileOutputFormat.setOutputPath((job,"hdfs_output_path"));
// I simply use text as output for the mapper but it can be any class you designed
// as long as you implement it as a Writable
job.setMapOutputKeyClass(Text.Class);
job.setMapOutputValueClass(Text.Class);
job.setOutputKeyClass(Text.Class);
job.setOutputValueClass(Text.Class);
...
现在配置映射器类。
假设您的sqoop导入的java文件名为Sqimp.java:
导入的表有以下列:id、name、age
您的映射器类应如下所示:
public static class SqoopImportMap
extends Mapper<LongWritable, Text, Text, Text>
{
public void map(LongWritable k, Text v, Context context)
{
Sqimp s = new Sqimp();
try
{
// this is where the code generated by sqoop is used.
// it automatically casts one line of the imported data into an instance of the generated class,
// to let you access the data inside the columns easily
s.parse(v);
}
catch(ParseError pe) {// do something if there is an error.}
try
{
// now the imported data is accessible:
// e.g
if (s.age>30)
{
// submit the selected data to the mapper's output as a key value pair.
context.write(new Text(s.age),new Text(s.id));
}
}
catch(Exception ex)
{//do something about the error}
}
}
公共静态类sqoopmortmap
扩展映射器
{
公共void映射(可长写k、文本v、上下文)
{
Sqimp s=新的Sqimp();
尝试
{
//这就是使用sqoop生成的代码的地方。
//它会自动将一行导入的数据强制转换到生成类的实例中,
//使您能够轻松访问列中的数据
s、 解析(v);
}
catch(ParseError pe){//如果出现错误,请执行某些操作。}
尝试
{
//现在可以访问导入的数据:
//例如
如果(年龄>30岁)
{
//将所选数据作为键值对提交到映射器的输出。
编写(新文本(s.age)、新文本(s.id));
}
}
捕获(例外情况除外)
{//对错误采取措施}
}
}
public static class SqoopImportMap
extends Mapper<LongWritable, Text, Text, Text>
{
public void map(LongWritable k, Text v, Context context)
{
Sqimp s = new Sqimp();
try
{
// this is where the code generated by sqoop is used.
// it automatically casts one line of the imported data into an instance of the generated class,
// to let you access the data inside the columns easily
s.parse(v);
}
catch(ParseError pe) {// do something if there is an error.}
try
{
// now the imported data is accessible:
// e.g
if (s.age>30)
{
// submit the selected data to the mapper's output as a key value pair.
context.write(new Text(s.age),new Text(s.id));
}
}
catch(Exception ex)
{//do something about the error}
}
}