Spark1.6.2 Java Implementation read JSON data file into MySQL database

Source: Internet
Author: User

PublicClassMainImplementsSerializable {/*** */PrivateStaticFinalLong Serialversionuid =-8513279306224995844L;PrivateStaticFinal String Mysql_username ="Demo";PrivateStaticFinal String mysql_pwd ="Demo";PrivateStaticFinal String Mysql_connection_url ="Jdbc:mysql://";PrivateStaticFinal Javasparkcontext sc =New Javasparkcontext (New Sparkconf (). Setappname ("Sparksavetodb"). Setmaster ("local[*]"));PrivateStaticFinal SqlContext SqlContext =New SqlContext (SC);public static void main (string[] args) { //Sample data-frame loaded from a json filedataframe USERSDF = (). JSON ( "Users.json"); //Save data-frame to MySQL (or any other JDBC supported databases) Properties Connectionproper Ties = new Properties (); Connectionproperties.put ( "user", Mysql_username) connectionproperties.put (" users ", connectionproperties);}           

In order to write the data for easy testing, we need a JSON file, similar to the below:

{"id":994,"Name":"Betty","Email":"Bsmithrl@simplemachines. org ","City":"Eláteia","Country":"Greece","IP":""},{"id":995,"Name":"Anna,""Email":"ALEWISRM@canalblog. com ","City":"Shangjing","Country":"China","IP":""},{"id":996,"Name":"David","Email":"Dgarrettrn@japanpost. JP ","City":"Tsarychanka","Country":"Ukraine","IP":""},{"id":997,"Name":"Heather","Email":"Hgilbertro@skype. com ","City":"Koilás","Country":"Greece","IP":""},{"id":998,"Name":"Diane","Email":"DDANIELSRP@statcounter. com ","City":"Transmitted","Country":"Colombia","IP":""},{"id":999,"Name": "Philip",  "email":  "Pfullerrq @reuters. com ", " City ": " El Cairo ",  "country":  "Colombia",  "IP": " "},{" id ": 1000,  "name":  "Maria",  "email":  "Mfordrr",  "City":  "Karabash",  "country":  "Russia",  "IP":  ""}        

When reading a file, the Users.json needs to be in the same directory as the jar package, and the test takes the local run mode:

DataFrame usersDf ="users.json");

The code in this line of mode (Savemode.append) to pay special attention, this makes each write data is added to the data table. Otherwise you will always be prompted: Exception in Thread "main" java.lang.RuntimeException:Table users already exists.

Usersdf. Write (). Mode (Savemode. Append). JDBC (Mysql_connection_url, "Users ", connectionPrope rties)                

Spark1.6.2 Java Implementation reads JSON data file into MySQL database

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: 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.