Spark1.6.2 Java Implementation read TXT file insert MySQL database code

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.