Package com.gosun.spark1;
Import java.util.ArrayList;
Import java.util.List;
Import java.util.Properties;
Import org.apache.spark.SparkConf;
Import Org.apache.spark.api.java.JavaRDD;
Import Org.apache.spark.api.java.JavaSparkContext;
Import org.apache.spark.api.java.function.Function;
Import Org.apache.spark.sql.DataFrame;
Import Org.apache.spark.sql.Row;
Import Org.apache.spark.sql.RowFactory;
Import Org.apache.spark.sql.SQLContext;
Import Org.apache.spark.sql.SaveMode;
Import Org.apache.spark.sql.types.DataType;
Import Org.apache.spark.sql.types.DataTypes;
Import Org.apache.spark.sql.types.DateType;
Import Org.apache.spark.sql.types.Metadata;
Import Org.apache.spark.sql.types.StringType;
Import Org.apache.spark.sql.types.StructField;
Import Org.apache.spark.sql.types.StructType;
public class Mysqlwriter {
public static void Main (string[] args) throws ClassNotFoundException {
Long current = System.currenttimemillis ();
String master = "spark://192.168.31.34:7077";
String localmaster = "local[5]";
sparkconf sparkconf = new sparkconf (). Setappname ("Mysqljdbc"). Setmaster (Localmaster);
Top and bottom objects for spark apps
Javasparkcontext sc = new Javasparkcontext (sparkconf);
SqlContext sqlcontext = new SqlContext (SC);
String url = "Jdbc:mysql://192.168.31.16:3306/db?useunicode=true&characterencoding=utf-8";
String table = "Tb_user";
Properties ConnectionProperties = new properties ();
Connectionproperties.put ("User", "root");
Connectionproperties.put ("Password", "MySQL");
Connectionproperties.put ("Driver", "com.mysql.jdbc.Driver");
Loading data from a database
Javardd<string> lines = Sc.textfile ("F:/baiduyundownload/data/class9/user1.txt");
javardd<row> Personrdd = Lines.map (New function<string, row> () {
Private static final long serialversionuid = 1L;
Public Row Call (String line)
Throws Exception {
string[] split = Line.split ("\ t");
Return Rowfactory.create (string.valueof (split[0]), string.valueof (split[1]));
}
});
list<structfield> structfields = new arraylist<structfield> ();
Structfields.add (Datatypes.createstructfield ("id", Datatypes.stringtype, false));
Structfields.add (Datatypes.createstructfield ("name", Datatypes.stringtype, true));
Structtype Structtype = Datatypes.createstructtype (structfields);
DataFrame usersdf = Sqlcontext.createdataframe (Personrdd, Structtype);
Usersdf.write (). Mode (Savemode.append). Mode (savemode.overwrite). JDBC (URL, table, connectionproperties);
System.out.println ((System.currenttimemillis ()-current)/+ + "s");
}
}
Spark1.6.2 Java Implementation read TXT file insert MySQL database code