Filter the data in a text document and insert the Cassandra Database

Source: Internet
Author: User
Tags cassandra readline

The code is as follows:

 Packagecom.locationdataprocess;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;ImportJava.io.InputStreamReader;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;Importjava.sql.SQLException;Importjava.sql.Statement;//filtering the GPS data in the data Public classFiltergpsdata {StaticConnection con=NULL;  Public Static voidInsertgps () {String filename[]={"Zhengye_drivetesting_08-18.09-07.txt", "Zhengye_drivetesting_08-18.09-13.txt",                "Zhengye_drivetesting_08-18.17-40.txt", "Zhengye_drivetesting_08-18.17-48.txt",                "Zhengye_drivetesting_08-19.17-19.txt", "Zhengye_drivetesting_08-20.09-33.txt",                "Zhengye_drivetesting_08-20.18-05.txt", "Zhengye_drivetesting_08-19.09-08.txt",                "Zhengye_drivetesting_08-21.18-07.txt", "Zhengye_drivetesting_08-21.18-07.txt"        };  for(intk = 0; K < 10; k++) {File file=NewFile ("h:\\ Project data \\Zhengye_Drive_Testing_Data\\" +Filename[k]); Try{FileInputStream fis=Newfileinputstream (file); InputStreamReader ISR=NewInputStreamReader (FIS); BufferedReader BR=NewBufferedReader (ISR); String Line=NULL;  while(line = Br.readline ())! =NULL) {String s=Line.trim (); //String pre[] = new STRING[8]; //pre[0] = "GPS time"; //pre[1] = "longitude"; //pre[2] = "Latitude"; //pre[3] = "Altitude"; //pre[4] = "Heading"; //pre[5] = "Speed"; //pre[6] = "Source"; //pre[7] = "Satellites"; //for (int i=0;i<pre.length;i++) {//if (S.startswith (Pre[i])) {//System.out.println (s); //Break ; // }                    // }                    if(S.startswith ("GPS Time"))) {                        Try{String data[]=NewString[8]; data[0] = filename[k].substring (+, +) + "" +s.split ("=") [1].trim (); //System.out.println (s);                            inti = 0;  while(I < 7 && (line = Br.readline ())! =NULL) {                                if(!line.equals ("") ) {Data[i+ 1] = line.split ("=") [1].trim (); //System.out.println (Line.trim ());i++; }} String Insert= "INSERT into Gpsdata.gps (gps_time,longitude,latitude,altitude,heading,speed,source,satellites)" + "VALUES (?,?,?,?,?,?,?,?)"; PreparedStatement Psta=con. preparestatement (insert);  for(intj = 0; J < 8; J + +) {psta.setstring (J+ 1, Data[j]);                            } psta.executeupdate ();                        Psta.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }}} System.out.println (Filename[k]+ "Insert success! "); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }    }        //connecting to a database     Public Static voidconnection () {Try{class.forname ("Org.apache.cassandra.cql.jdbc.CassandraDriver"); Con=DriverManager. getconnection ("Jdbc:cassandra://127.0.0.1:9160/gpsdata"); String SQL= "CREATE TABLE IF not EXISTS Gpsdata.gps (gps_time varchar,longitude varchar,latitude varchar,altitude varchar,heading VA Rchar,speed varchar,source varchar,satellites varchar,primary KEY (gps_time)) "; Statement STA=con.createstatement ();        Sta.execute (SQL); } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(SQLException e) {System.out.println ("The connection is broken!" ");        E.printstacktrace (); }    }    //Close Connection     Public Static voiddisconnection () {if(con!=NULL)            Try{con.close (); System.out.println ("The database connection is closed properly!" "); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }                 Public Static voidMain (string[] args) {connection ();        Insertgps ();    Disconnection (); }}

Filter the data in a text document and insert the Cassandra Database

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.