过滤文本文档中的数据并插入Cassandra数据库
代码如下:
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数据库的更多相关文章
- python 读取文本文档中的数据
import os dir = input('Please input the file dir:')#提示输入文件路径 while not os.path.exists(dir):#判断文件是否存在 ...
- c#导出数据到csv文本文档中,数据前面的0不见了解决方法
((char)(9)).ToString() + dataRow["FUserName"].ToString().Trim() + "\t",
- 编写Java程序,读取文本文档的内容,去除文本中包含的“广告”字样,把更改后的内容保存到一个新的文本文档中
查看本章节 查看作业目录 需求说明: 读取文本文档的内容,去除文本中包含的"广告"字样,把更改后的内容保存到一个新的文本文档中 实现思路: 在main() 方法中,使用 new F ...
- Java 写一段字符到指定的文本文档中,如果该文本文档不存在,则创建该文本文档
写一段字符到指定的文本文档中,如果该文本文档不存在,则创建该文本文档 import java.io.File; import java.io.FileNotFoundException; import ...
- java从文件中读取数据然后插入到数据库表中
实习工作中,完成了领导交给的任务,将搜集到的数据插入到数据库中,代码片段如下: static Connection getConnection() throws SQLException, IOExc ...
- 使用 AWK 去掉文本文档中的空白行
在 Linux 操作系统中,可以使用 AWK 命令高效地处理文本文档.AWK 命令通过执行使用 AWK 语言编写的脚本程序,处理文本文档.AWK 脚本程序是由模式(patterns)与相关操作(cor ...
- 文本文档中各字母出现次数汇总(java)
package 字母频率统计; import java.io.*; public class Inputfile { public static void main(String args[]) { ...
- Java 单字节、多字节读取文本文档中的内容
文本文档位于工程下. 鼠标右击工程,选择“new - File”,即可创建. 文本文档的格式:GBK 单字节读取 import java.io.File; import java.io.FileInp ...
- oracle数据库中将clob字段内容利用java提取出至文本文档中
代码段: 1.执行clob转String public static String ClobToString(Clob sc) throws SQLException, IOException { S ...
随机推荐
- android开发中遇到的bug
这种NullPointerException这么解决啊 Activity.dispatchTouchEvent 里try catch一下 参考:http://www.eoeandroid.com/th ...
- Java基础 —— 概述
Java语言: JDK(Java Development Kit)开发工具包,提供Java的开发环境和运行环境 --> 适合于开发 JRE(Java Runtime Environment)Ja ...
- iOS 后台退出app时不执行applicationWillTerminate的临时解决方法
- (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release s ...
- 关闭HTML5只能提示(form上新增novalidate)
<form novalidate> <input type="text" required /> <input type="su ...
- ms-class的进化
ms-class是avalon用得最多的几个绑定之一,也正因为如此其功能一直在扩充中.根据时期的不同,分为旧风格与新风格两种. 旧风格是指正在ms-class后面跟着类外,然后在绑定值中添加表达式,即 ...
- 前端异步解决方案——mmDeferred
Deferred是前端解决异步操作的一种编程范式,后来出现的Promise规范更是让其普适性大大提高.不过Promise规范也存在分岐.现在最流行的是Promise/A规范. Promise/A大致是 ...
- 理解js闭包(二)
@(编程) 一.什么是闭包? 官方"的解释是:闭包是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分. 相信很少有人能直接看懂这句话,因为他 ...
- redis缓存数据表
直观上看,数据库中的数据都是按表存储的:更微观地看,这些表都是按行存储的.每执行一 次select查询,数据库都会返回一个结果集,这个结果集由若干行组成.所以,一个自然而然 的想法就是在Redis中找 ...
- HDU1896Stones(优先队列)
地址http://acm.hdu.edu.cn/showproblem.php?pid=1896 题目大一比较简单,就是说在一条直线道路上有n个石头,往前走,遇到一个数一个,如果遇到的是第奇数个那就把 ...
- Mahalanobis Distance(马氏距离)
(from:http://en.wikipedia.org/wiki/Mahalanobis_distance) Mahalanobis distance In statistics, Mahalan ...