代码如下:

package com.locationdataprocess;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement; //过滤数据中的GPS数据
public class FilterGPSData {
static Connection con=null;
public static void insertGPS(){
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 (int k = 0; k < 10; k++) {
File file = new File("H:\\项目数据\\Zhengye_Drive_Testing_Data\\"
+ fileName[k]);
try {
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(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[] = new String[8];
data[0] = fileName[k].substring(21, 32)+" "+s.split("=")[1].trim();
// System.out.println(s);
int i = 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 (int j = 0; j < 8; j++) {
psta.setString(j + 1, data[j]);
}
psta.executeUpdate();
psta.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.out.println(fileName[k]+"插入成功!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} //连接数据库
public static void connection(){
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 varchar,speed varchar,source varchar,satellites varchar,PRIMARY KEY (gps_time))";
Statement sta = con.createStatement();
sta.execute(sql);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
System.out.println("连接断开!");
e.printStackTrace();
}
} //关闭连接
public static void disConnection(){
if(con!=null)
try {
con.close();
System.out.println("数据库连接正常关闭!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static void main(String[] args) {
connection();
insertGPS();
disConnection();
} }

过滤文本文档中的数据并插入Cassandra数据库的更多相关文章

  1. python 读取文本文档中的数据

    import os dir = input('Please input the file dir:')#提示输入文件路径 while not os.path.exists(dir):#判断文件是否存在 ...

  2. c#导出数据到csv文本文档中,数据前面的0不见了解决方法

    ((char)(9)).ToString() + dataRow["FUserName"].ToString().Trim() + "\t",

  3. 编写Java程序,读取文本文档的内容,去除文本中包含的“广告”字样,把更改后的内容保存到一个新的文本文档中

    查看本章节 查看作业目录 需求说明: 读取文本文档的内容,去除文本中包含的"广告"字样,把更改后的内容保存到一个新的文本文档中 实现思路: 在main() 方法中,使用 new F ...

  4. Java 写一段字符到指定的文本文档中,如果该文本文档不存在,则创建该文本文档

    写一段字符到指定的文本文档中,如果该文本文档不存在,则创建该文本文档 import java.io.File; import java.io.FileNotFoundException; import ...

  5. java从文件中读取数据然后插入到数据库表中

    实习工作中,完成了领导交给的任务,将搜集到的数据插入到数据库中,代码片段如下: static Connection getConnection() throws SQLException, IOExc ...

  6. 使用 AWK 去掉文本文档中的空白行

    在 Linux 操作系统中,可以使用 AWK 命令高效地处理文本文档.AWK 命令通过执行使用 AWK 语言编写的脚本程序,处理文本文档.AWK 脚本程序是由模式(patterns)与相关操作(cor ...

  7. 文本文档中各字母出现次数汇总(java)

    package 字母频率统计; import java.io.*; public class Inputfile { public static void main(String args[]) { ...

  8. Java 单字节、多字节读取文本文档中的内容

    文本文档位于工程下. 鼠标右击工程,选择“new - File”,即可创建. 文本文档的格式:GBK 单字节读取 import java.io.File; import java.io.FileInp ...

  9. oracle数据库中将clob字段内容利用java提取出至文本文档中

    代码段: 1.执行clob转String public static String ClobToString(Clob sc) throws SQLException, IOException { S ...

随机推荐

  1. 40个最好的Tumblr主题

    如果安装了一款较好的Tumblr主题,你的Tumblr空间将焕然一新.然而找到一款合适的主题并不是一件容易的事,这正是本文中我整理那么多优质的Tumblr模板作为灵感的原因.其中有一些免费的Tumbl ...

  2. TopFreeTheme精选免费模板【20130701.特别版】

    今天我们整理了16款WordPress和Joomla的最新主题.它们都是来自Themeforest,RocketTheme,YooTheme以及TemPlaza的高质量主题,赶快收藏起来吧. Este ...

  3. extjs 学习笔记(二)

    EXTJS实用开发指南 1. 要使用ExtJS 框架的页面中一般包括下面几句: <link rel="stylesheet" type="text/css" ...

  4. HDU5873:Football Games

    题目链接: Football Games 分析: 先将分数排序,然后 设当前队编号为p,设个指针为p+1,然后p>1,每次p-=2,指针右移一位p==1,指针指向的队-=1p==0,从指针开始到 ...

  5. shell学习目录

    1. 了解shell 2. shell 入门基础 3. Shell脚本文件中常用的操作语句

  6. WIFI接入Internet配置过程

    转载请注明出处:http://club.gizwits.com/thread-256-1-1.html AirLink模式GoKit2代配置路由SSID和密码方法如下:1.GoKit2上电2.长按Ke ...

  7. HDU3635Dragon Balls(并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=3635 题目意思是说n个球在n个城市. 每次操作把编号i的球所在的城市的所有的求全部一道另一城市B 每次询问访问编 ...

  8. HDU3033I love sneakers!(分组背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=3033 本题的意思就是说现在有n种牌子的鞋子,每种品牌有一些不同的鞋,每双鞋子都有一个特定的权值,现在要求每种品牌 ...

  9. JedisPool使用原理和源代码

    1,JedisPool的使用 <!-- 连接池的配置信息 --><beanid="jedisConfig"class="redis.clients.je ...

  10. (VC)解决绘图时闪烁问题的一点经验[转]

    转自:http://www.cnblogs.com/lidabo/p/3429862.html 清除屏幕闪烁 (转自网上) <一> 由于作图过于复杂和频繁,所以时常出现闪烁的情况,一些防止 ...