目标:将不同格式的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. LeetCode OJ 75. Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  2. LeetCode OJ 121. Best Time to Buy and Sell Stock

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  3. jQuery控制表格行移动,序号不变

    @model Gd.NetSign.Controllers.DTO.SysPamaterDTO @{ ViewBag.Title = "SysPamatList"; //Layou ...

  4. ural 1261. Tips(进制运算)

    1261. Tips Time limit: 1.0 secondMemory limit: 64 MB The favorite resting place of the Ural programm ...

  5. MVC DisplayTemplates and EdiotrTemplates.

    我们在mvc项目里经常要对枚举,日期,副文本输入,我们可以用笨拙的方法去view页面里绑定呈现的html内容,而且这种办法不能重用,也就是在不同的view里还是需要做相同的事情,给个日期空间选择例子吧 ...

  6. windows任务计划程序路径设置

    用任务计划启动程序,特别是脚本,比如我要启动python脚本,其中有一句是这么写的 BasePath = removeLastSlash(os.path.abspath("..\\..\\& ...

  7. Alyona and a tree

    Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  8. 浅谈C/C++结构体内存分配问题

    .wiz-todo, .wiz-todo-img {width: 16px; height: 16px; cursor: default; padding: 0 10px 0 2px; vertica ...

  9. Base64技术:把对象转变成字符串

    需求:在安卓移动端和服务器进行数据交互的时候,有些时候需要上传,下载文件.如果所有的参数都变成字符串,会更加方便. 原理:Base64只是把byte[]数组进行了编码,然后再解码的过程,文档内容无法直 ...

  10. Ubuntu 12.04 中自定义DNS服务器设置

    首先我们需要创建一个文件/etc/resolvconf/resolv.conf.d/tail: #vim /etc/resolvconf/resolv.conf.d/tail 然后我们在这个文件里写入 ...