4.1 Importing MySQL data into HDFs
Import org.apache.hadoop.conf.Configuration;Import Org.apache.sqoop.Sqoop;Import Org.apache.sqoop.tool.SqoopTool;Import Org.apache.sqoop.util.OptionsFileUtil;PublicClasssqooptest {PrivateStaticIntImportdatafrommysql ()Throws Exception {string[] args =New string[] {"--connect","Jdbc:mysql://192.168.1.97:3306/mydb","--driver","Com.mysql.jdbc.Driver","-username","Root","-password","Root","--table","User","-M","1", "--target-dir", "Java_import_user"}; string[] expandarguments = optionsfileutil.expandarguments (args); Sqooptool tool = Sqooptool.gettool ( "import"); Configuration conf = new Configuration (); Conf.set (" hdfs://192.168.1.200:9000 "); //set HDFS service address configuration Loadplugins = sqooptool.loadplugins (conf); Sqoop Sqoop = new Sqoop ((com.cloudera.sqoop.tool.SqoopTool) tool, Loadplugins); return sqoop.runsqoop (Sqoop, expandarguments); } public static void main (string[] args) throws Exception {importdatafrommysql ();}}
The resulting file can be viewed on HDFs
Found in HDFs generated directory/user/cyyun/java_import_user, the program --target-dir does not specify a specific path, the native Windows user name is Cyyun, on Windows on the use of program operations will be in/user/ The directory under which a Windows user name is generated
View results:
-cat /user/cyyun/java_import_user/part-m-00000hadoop fs -cat /user/cyyun/java_import_user/part-m-00001
--target-dirrun the view results as specified in the program /user/root/java_import_user :
-cat /user/root/java_import_user/part-m-00000