惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
GbyAI
GbyAI
C
CERT Recently Published Vulnerability Notes
S
Security @ Cisco Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
The Hacker News
The Hacker News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
P
Proofpoint News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
S
Securelist
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Simon Willison's Weblog
Simon Willison's Weblog
Latest news
Latest news
T
Tor Project blog
T
Threat Research - Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Spread Privacy
Spread Privacy
K
Kaspersky official blog
T
The Exploit Database - CXSecurity.com
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
V2EX - 技术
V2EX - 技术
L
Lohrmann on Cybersecurity
Google Online Security Blog
Google Online Security Blog
Cyberwarzone
Cyberwarzone
Help Net Security
Help Net Security
The Last Watchdog
The Last Watchdog
C
Cybersecurity and Infrastructure Security Agency CISA
Attack and Defense Labs
Attack and Defense Labs
大猫的无限游戏
大猫的无限游戏
Schneier on Security
Schneier on Security
H
Heimdal Security Blog
O
OpenAI News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
AI
AI
H
Hacker News: Front Page
博客园_首页
博客园 - 【当耐特】
L
LINUX DO - 最新话题
MyScale Blog
MyScale Blog
量子位
Vercel News
Vercel News
C
Cisco Blogs
L
LINUX DO - 热门话题
Y
Y Combinator Blog
T
Threatpost
爱范儿
爱范儿
P
Privacy & Cybersecurity Law Blog

博客园 - sinodzh

从零自学Hadoop(25):Impala相关操作下 从零自学Hadoop(24):Impala相关操作上 从零自学Hadoop(23):Impala介绍及安装 从零自学Hadoop(22):HBase协处理器 从零自学Hadoop(21):HBase数据模型相关操作下 从零自学Hadoop(20):HBase数据模型相关操作上 从零自学Hadoop(19):HBase介绍及安装 Hadoop技巧(03):HostName命名带来的问题 Hadoop技巧(02):时间同步 使用Nexus搭建Maven本地仓库 Hadoop技巧(01):插件,终端权限 Hadoop技巧系列索引 从零自学Hadoop(18):Hive的CLI和JDBC 从零自学Hadoop(17):Hive数据导入导出,集群数据迁移下 从零自学Hadoop(16):Hive数据导入导出,集群数据迁移上 从零自学Hadoop(15):Hive表操作 从零自学Hadoop(14):Hive介绍及安装 从零自学Hadoop(13):Hadoop命令下 从零自学Hadoop(12):Hadoop命令中
Hadoop技巧(04):简易处理solr date 时区问题
sinodzh · 2017-02-16 · via 博客园 - sinodzh

阅读目录

本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作。

     在cdh集成的solr cloud中,我们可以通过solr管理界面进行查询,也可以通过java的api进行查询,但查询过程中,如果是时间类型的,可能会存在两者在界面上看上去不一致的问题,两者时间刚好相差本地的时区。

创建collection

一:上传配置文件

  为了模拟现象,我们设置如下solr文档结构

solrctl instancedir --create date_demo /data/solr_s

二:创建collection

solrctl collection --create date_demo -s 2 -m 2 -r 2

创建完后solr的collection如下

模拟程序

一:编写程序

  编写模拟插入程序。为了容易查看,只插入2条数据。

  这里我们使用的solr版本为4.10.3。

    private 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");
    }

View Code

二:运行程序

  可以看到我们已经插入2条数据。

三:程序查询

  在程序查询的结果如下。

  可以看到solr自己的查询界面使用的时间格式是UTC的,会有时差,我们这里是8小时。
CREATEDAY和CREATEDATE有时候不一致。

四:处理

  所以为了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/

系列索引

  Hadoop技巧系列索引

本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作。