目标:将不同格式的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. ubuntu 14.04 32位库

    如果是ubuntu 14.04,则请先执行: 方法1: sudo gedit /etc/apt/sources.list 然后在最后添加上: deb http://archive.ubuntu.com ...

  2. 高精度运算专题1-加法运算(The addition operation)

    这个专题呢,我就来讲讲高精度的加法,下面是一个计算加法的函数(用数组a加上数组b结果存到数组c里面). 思路:先测一下数组a和数组b的长度,分别放到a[0].b[0]里面去,再从第二位开始相加,记得满 ...

  3. Openjudge-计算概论(A)-找和为K的两个元素

    描述: 在一个长度为n(n < 1000)的整数序列中,判断是否存在某两个元素之和为k. 输入第一行输入序列的长度n和k,用空格分开.第二行输入序列中的n个整数,用空格分开.输出如果存在某两个元 ...

  4. String,StringBuilder,StringBuffer

    (转:http://blog.csdn.net/rmn190/article/details/1492013)   String 字符串常量StringBuffer 字符串变量(线程安全)String ...

  5. 关于oracle数据库(8)查询2

    筛选数据,直接加where条件,并且and,或者or 使用rownum获取前N条数据 select * from 表名 where rownum <= 数字; 如:获取前5条数据 select ...

  6. 关于css的hack问题

    <!--[if <keywords>? IE <version>?]> HTML代码块 <![endif]--> 取值: <keywords> ...

  7. install font

    哪些字体包含国际音标呢? 在微软的Windows与Office的2000或以上版本中分别带有Lucida Sans Unicode和Arial Unicode MS两种字体(以下分别简称LSU和AUM ...

  8. Saltstack 操作目标,正则匹配,及组管理

    如果我们要维护好一个庞大的配置管理系统那么首选得维护好我们的管理对象,在saltstack系统中我们的管理对象叫做Target, 在master上我们可以采用不同Target去管理不同的Minion. ...

  9. input之placeholder与行高的问题

    我们实现一个输入框的视觉的时候为了保持其各种各样的兼容性: 1.鼠标要跟文字一样高度. 2.文字要居中对齐. 3.还要有placeholder 第一个目标,当实现一个高度为40像素的高度输入框时,为了 ...

  10. U盘做svn版本控制

    svn提供的访问方式有: file:///本地路径/to/svnrepo/ //访问本地磁盘 http://host/to/svnrepo/ //通过配置subversion的apache服务器的we ...