Ok điều này có vẻ hiển nhiên khi bạn tìm ra nhưng là người mới bắt đầu java, điều này có thể mất thời gian.
Trước tiên hãy định cấu hình dự án của bạn: chỉ cần thêm tệp .java được tạo sqoop vào thư mục nguồn của bạn. Tôi sử dụng nhật thực để nhập nó vào thư mục nguồn của lớp học.
Sau đó, chỉ cần đảm bảo bạn đã cấu hình java xây dựng con đường của dự án của bạn một cách chính xác:
Thêm các tập tin jar sau trong thuộc tính của dự án/java build path/thư viện/thêm jar bên ngoài: (ví hadoop cdh4 +):
/usr/lib/hadoop/hadoop-common.jar
/usr/lib/hadoop-[version]-mapreduce/hadoop-core.jar
/usr/lib/sqoop/sqoop-[sqoop-version]-cdh[cdh-version].jar
Sau đó điều chỉnh mã nguồn MapReduce của bạn: configure Đầu tiên nó:
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);
...
Không w cấu hình lớp bản đồ của bạn. Giả sử sqoop nhập khẩu tập tin java của bạn được gọi Sqimp.java: và bảng bạn nhập khẩu có các cột sau: id, tên, tuổi lớp mapper của bạn sẽ giống như thế này:
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}
}
}
Nguồn
2012-10-25 15:39:13