目标:将不同格式的JSON文件存入MySQL数据库
涉及的点有:
1. java处理JSON对象,直接见源码。
2. java.sql.SQLException: Incorrect string value: ‘\xF0\x9F\x99\x8F\xE5\x8D…’ for column ‘text’ at row 1报错问题,报错原因:因为我没有对插入文本做任何处理,文本内有不同字节的utf8字符,我的处理方式就是过滤后再插入,因为特殊的字符其实也没什么用。

 public static String StringFilter(String str) throws PatternSyntaxException {
// 清除掉所有特殊字符,只允许汉字字母数字和某些常见符号出现
String regEx = "[^0-9a-zA-Z\u4e00-\u9fa5~!@#$%^&*()+=|{}':;',//[//].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]+";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.replaceAll(" ").trim().replaceAll("s+", " ");
} public static void insertMicroblogs(String uid, String mid, String time,String geo,String source,
String repost,String comment,String attitude,String text,int flag,Statement statement) throws Exception {
if (uid == null) return;
String sql = "insert into data2016.microblog values(\"" + mid+ "\",\"" + uid + "\",\"" + time
+ "\",\"" + geo+ "\",\"" + source + "\",\"" +repost + "\",\"" + comment + "\",\"" + attitude
+ "\",\"" + text+ "\"," + flag + ")";
String selectsql = "select count(*) from data2016.microblog where uid = \""+ uid+ "\" and mid=\"" + mid + "\""; try {
ResultSet rset = statement.executeQuery(selectsql);
int rowCount = 0; //记录查询结果记录数
if(rset.next()) {
rowCount=rset.getInt(1);
}
if(rowCount == 0){
statement.execute(sql);
} } catch (Exception e) {
pErrorUser.println("microblog:"+uid);
// System.out.println(sql);
e.printStackTrace();
return;
}
}
public static void readUserMicroblogs() throws Exception{
File file = new File("data/source/first/microblogs/microblogs12.txt");
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file));
BufferedReader reader = new BufferedReader(new InputStreamReader(fis,"utf-8"),5*1024*1024);// 用5M的缓冲读取文本文件 String line = "";
int count = 0;
while((line = reader.readLine()) != null){
count++;
JSONObject blogObject = new JSONObject(line);
String uid = blogObject.getString("uid");
String microblogs = blogObject.getString("microblogs");
JSONArray microblogsArray = new JSONArray(microblogs);
int size = microblogsArray.length();
// System.out.println("Size:" + size);
for (int i = 0; i < size; i++) {
JSONObject jsonObj = microblogsArray.getJSONObject(i);
String mid=jsonObj.get("mid").toString();
String created_at=jsonObj.get("created_at").toString();
String geo=StringFilter(jsonObj.get("geo").toString().replaceAll("\"", " "));
String source=jsonObj.get("source").toString().replaceAll("\"", " ");
String reposts_count=jsonObj.get("reposts_count").toString();
String comments_count=jsonObj.get("comments_count").toString();
String attitudes_count=jsonObj.get("attitudes_count").toString();
String text;
int flag;
if(jsonObj.has("retweeted_status")){
flag=1;
String retweet = jsonObj.get("retweeted_status").toString();
JSONObject weibo = new JSONObject(retweet);
text=StringFilter(weibo.get("text").toString().replaceAll("\"", " "));
}else{
flag=0;
text=StringFilter(jsonObj.get("text").toString().replaceAll("\"", " "));
}
insertMicroblogs(uid,mid,created_at,geo,source,reposts_count,comments_count,attitudes_count,text,flag,statement); } if(count%50==0){
System.out.print(count +"...");
}
if(count%1000==0){
System.out.println();
}
}
}

如上代码是我对新浪微博数据文档的存入工作,因为文档较大,所以加入了缓存读取。遇到的其他问题基本都是特殊字符问题,其中插入文本中有双引号,原本处理方法是:

String source=jsonObj.get("source").toString().replaceAll("\"", "\\\" ");

但是不知道为嘛,转义字符没有成功加入,所以就直接将双引号替换为空格处理了。这一块内容原本不打算记,原以为是很顺利的事还是倒腾了一天,所以小记一下咯。

JSON文件存入MySQL数据库的更多相关文章

  1. tensorflow利用预训练模型进行目标检测(三):将检测结果存入mysql数据库

    mysql版本:5.7 : 数据库:rdshare:表captain_america3_sd用来记录某帧是否被检测.表captain_america3_d用来记录检测到的数据. python模块,包部 ...

  2. 用Python获取沪深两市上市公司股票信息,提取创近10天股价新高的、停牌的、复牌不超过一天或者新发行的股票,并存入mysql数据库

    #该脚本可以提取沪深两市上市公司股票信息,并按以下信息分类:(1)当天股价创近10个交易日新高的股票:(2)停牌的股票:(3)复牌不超过一个交易日或者新发行的股票 #将分类后的股票及其信息(股价新高. ...

  3. 将主机IDS OSSEC日志文件存入MYSQL的方法

    将主机IDS OSSEC日志文件存入MYSQL的方法 http://www.freebuf.com/articles/system/6139.html http://ossec-docs.readth ...

  4. Linux VPS自动定时备份网站文件和MYSQL数据库到FTP空间(LNMP)

    如果我们网站更新不是很频繁,我们可以定期手动进行备份网站文件和MYSQL数据库导出.如果我们网站数据更新频繁,且数据尤为重要,建议要采用定期自动 备份,至少需要多备份数据,无论我们选择何种优秀的VPS ...

  5. lnmp更改网站文件和MySQL数据库的存放目录

    购买阿里云服务器,一般建议买一个数据盘,也就是系统盘和数据盘分开,将网站文件和Mysql数据库等都保存在数据盘,即使系统盘或者环境出问题,重置系统盘和重新配置环境,都不会影响数据盘的东西. 配置好LN ...

  6. 利用日志文件恢复MYSQL数据库

    利用日志文件恢复MYSQL数据库 650) this.width=650;" onclick='window.open("http://blog.51cto.com/viewpic ...

  7. Python Json分别存入Mysql、MongoDB数据库,使用Xlwings库转成Excel表格

    将电影数据 data.json 数据通过xlwings库转换成excel表格,存入mysql,mongodb数据库中.python基础语法.xlwings库.mysql库.pymongo库.mongo ...

  8. Java&mysql:过滤文件内容,将新文件内容存入mysql数据库

    在上一篇博文jdbc连接数据库中我已经简单介绍了如何连接到mysql数据库,今天要总结的是学长给我布置的一个小作业,把一个很大的已经用","分开了的一行一行的txt文件内容过滤掉注 ...

  9. Spark1.6.2 java实现读取json数据文件插入MySql数据库

    public class Main implements Serializable { /** * */ private static final long serialVersionUID = -8 ...

随机推荐

  1. 解决Firefox访问12306"连接不受信任"的问题

    用Firefox访问12306.cn, 总是提示"This Connection is Untrusted", 曾经有个"Add Exception" 按钮, ...

  2. LeetCode OJ 63. Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  3. tomcat容器启动的启动过程(三)

    Catalina的start方法 /** * Start a new server instance. */ public void start() { if (server == null) { l ...

  4. 将数据库的数据导入solr索引库中

    在solr与tomcat整合文章中,我用的索引库是mycore,现在就以这个为例. 首先要准备jar包:solr-dataimporthandler-4.8.1.jar.solr-dataimport ...

  5. UI弹出键盘和收回键盘

    点击textfield,会自动弹出键盘 要让键盘收回来,先设置个代理:[field setTextFieldDelegate:self];  可设置成自己,也可设置成其他对象,只要在对应的类中,遵循U ...

  6. UILabel设置富文本格式显示

    实例化方法和使用方法 实例化方法: 使用字符串初始化 - (id)initWithString:(NSString *)str; 例: NSMutableAttributedString *Attri ...

  7. PHP 流程

    登录页面 <body> <form action="loginchuli.php" method="post"> <div> ...

  8. bat自动创建文件夹(以当前时间命名)

    先cmd中查看当前的日期和时间: @echo off color 0a set dt=%date%%time% echo %dt%pause 1.使用截取进行命名(时间为12小时制时命名会出现空格,不 ...

  9. python常用正则表达式

    匹配特定数字:^[1-9]\d*//匹配正整数−[1−9]\d∗  //匹配负整数^-?[1-9]\d*//匹配整数[1−9]\d∗|0 //匹配非负整数(正整数 + 0)^-[1-9]\d*|0// ...

  10. python常用函数年初大总结

    1.常用内置函数:(不用import就可以直接使用) help(obj) 在线帮助, obj可是任何类型 callable(obj) 查看一个obj是不是可以像函数一样调用 repr(obj) 得到o ...