本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文连接,谢谢合做。html
文章是哥(mephisto)写的,SourceLinkjava
在cdh集成的solr cloud中,咱们能够经过solr管理界面进行查询,也能够经过java的api进行查询,但查询过程当中,若是是时间类型的,可能会存在二者在界面上看上去不一致的问题,二者时间恰好相差本地的时区。git
一:上传配置文件
为了模拟现象,咱们设置以下solr文档结构github
solrctl instancedir --create date_demo /data/solr_s二:建立collection
solrctl collection --create date_demo -s 2 -m 2 -r 2建立完后solr的collection以下apache
一:编写程序
编写模拟插入程序。为了容易查看,只插入2条数据。api
这里咱们使用的solr版本为4.10.3。dom
View Codeprivate void insert() throws SolrServerException, IOException, ParseException { String zhHost = "master1/solr"; CloudSolrServer cloudSolrServer = new CloudSolrServer(zhHost); cloudSolrServer.setDefaultCollection("date_demo"); String id_1 = UUID.randomUUID().toString().replaceAll("-", "") .toUpperCase(); String name_1 = "1点前+8"; Date createDate_1 = sdfDate.parse("2016-12-30 00:11:12"); String day_1 = sdfDay.format(createDate_1); String id_2 = UUID.randomUUID().toString().replaceAll("-", "") .toUpperCase(); String name_2 = "1点后+8"; Date createDate_2 = sdfDate.parse("2016-12-30 10:13:14"); String day_2 = sdfDay.format(createDate_2); SolrInputDocument solrInputDocument1 = create(id_1, name_1, day_1, createDate_1); SolrInputDocument solrInputDocument2 = create(id_2, name_2, day_2, createDate_2); cloudSolrServer.add(solrInputDocument1); cloudSolrServer.add(solrInputDocument2); cloudSolrServer.commit(); System.out.println("success"); }二:运行程序
能够看到咱们已经插入2条数据。ide
三:程序查询
在程序查询的结果以下。工具
能够看到solr本身的查询界面使用的时间格式是UTC的,会有时差,咱们这里是8小时。
CREATEDAY和CREATEDATE有时候不一致。oop四:处理
因此为了3方的统一,要么本身改solr界面查询的。要么本身改下时差,使得3方结果一致,方便使用。
这里咱们采用本身修改时差来同步。
但工具量挺大,得在solr插入的时候转换下时间格式程utc。还的在每次查询的时候转换回来。
因此这里就本身恶心下本身,改下solr源码,在源码中找到对应的位置,固定的修改为本身这里的时差。
这样就间接的使3方同步了。找到solr相关的处理代码类
org.apache.solr.common.util.JavaBinCodec.java在readVal下
return new Date(dis.readLong()-28800000l);//由于存储的时候solr的时间格式是utc的,因此这里减掉当前时区的值在writePrimitive下
daos.writeLong(((Date) val).getTime()+28800000l);//存入的时候为了同day string同步 加8小时这样就能够了。
咱们查看效果。
为了对比 将数据的名称加备注+8
solr查询页面
--------------------------------------------------------------------
到此,本章节的内容讲述完毕。
Github: https://github.com/sinodzh/HadoopExample/tree/master/2017/solr.demo/
本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文连接,谢谢合做。
文章是哥(mephisto)写的,SourceLink