存档在 2016年8月

Tomcat的输出信息打印

2016年8月25日

如果你用startup.sh命令打开Tomcat服务器,会发现Linux下Tomcat的输出信息默认是记录在日志文件中(前提是开启了日志记录功能),并不将输出信息打印在屏幕上。这点区别于windwos,在windows下,如果你用startup.bat命令打开Tomcat,会发现屏幕有”错误”输出信息。
注:如果不知道如何开启Tomcat的日志记录功能,请参考:http://blog.csdn.net/zhu_xun/article/details/16817811
那么如何在Linux下如何 将Tomcat的输出信息打印在屏幕上呢?我们需要修改脚本文件catalina.sh(在tomcat/bin目录下),将其错误输出信息重定向至屏幕。具体参考如下:

    eval "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
      -Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
      -Djava.security.manager \
      -Djava.security.policy=="\"$CATALINA_BASE/conf/catalina.policy\"" \
      -Dcatalina.base="\"$CATALINA_BASE\"" \
      -Dcatalina.home="\"$CATALINA_HOME\"" \
      -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
      org.apache.catalina.startup.Bootstrap "$@" start \
      >> "$CATALINA_OUT" 2>&1 "&"

  else
    eval "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
      -Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
      -Dcatalina.base="\"$CATALINA_BASE\"" \
      -Dcatalina.home="\"$CATALINA_HOME\"" \
      -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
      org.apache.catalina.startup.Bootstrap "$@" start \
      >> "$CATALINA_OUT" 2>&1 "&"

改成

    eval "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
      -Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
      -Djava.security.manager \
      -Djava.security.policy=="\"$CATALINA_BASE/conf/catalina.policy\"" \
      -Dcatalina.base="\"$CATALINA_BASE\"" \
      -Dcatalina.home="\"$CATALINA_HOME\"" \
      -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
      #org.apache.catalina.startup.Bootstrap "$@" start \
      org.apache.catalina.startup.Bootstrap "$@" start
      >> "$CATALINA_OUT" 2>&1 "&"

  else
    eval "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
      -Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
      -Dcatalina.base="\"$CATALINA_BASE\"" \
      -Dcatalina.home="\"$CATALINA_HOME\"" \
      -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
     # org.apache.catalina.startup.Bootstrap "$@" start \
      org.apache.catalina.startup.Bootstrap "$@" start \
      >> "$CATALINA_OUT" 2>&1 "&"

require.js加载某非规范的模块

2016年8月23日

1.注册

<!-- require.js -->
<script type="text/javascript" src="__STATIC__/js/require.js?__VERSION__"></script>
<script type="text/javascript">
	window.UEDITOR_HOME_URL   = '__STATIC__/js/ueditor/';
	window.UEDITOR_SERVER_URL = '<{:U('Upload/ueditor')}>';
	require.config({
		baseUrl: "__STATIC__/module/admin/",
		urlArgs: '__VERSION__',
		waitSeconds: 0,
		paths: {
			'jquery':                '../../js/jquery.min',
			'jquery.easyui':         '../../js/easyui/jquery.easyui.min',
			'jquery.easyui.portal':  '../../js/easyui/plugins/jquery.portal',
			'jquery.easyui.lang':    '../../js/easyui/locale/easyui-lang-zh_CN',
			'jquery.easyui.app':     '../../js/jquery.easyui.app',
			'jquery.venobox':        '../../js/venobox/venobox.min',
			'jquery.croppic':        '../../js/croppic/croppic',
			'editor':                '../../js/ueditor/ueditor.require',
			'editor.zeroclipboard':  '../../js/ueditor/third-party/zeroclipboard/ZeroClipboard.min',
			'bootstrap':                '../../../statics/album/assets/js/bootstrap.min',
			'viewer':                '../../../statics/album/dist/viewer',
			'album':                '../../../statics/album/assets/js/main',
		},
		shim: {
			'jquery.venobox':      {deps: ['jquery']},
			'jquery.croppic':      {deps: ['jquery']},
			'jquery.easyui':       {deps: ['jquery']},
			'jquery.easyui.lang':  {deps: ['jquery', 'jquery.easyui']},
			'jquery.easyui.portal':{deps: ['jquery', 'jquery.easyui', 'jquery.easyui.lang']},
			'jquery.easyui.app': {
				deps: ['jquery', 'jquery.easyui', 'jquery.easyui.portal', 'jquery.easyui.lang', 'jquery.venobox', 'jquery.croppic'],
				exports: '$'
			},
			'editor':              {deps: ['../../js/ueditor/ueditor.config', 'editor.zeroclipboard']},
			'album':      {deps: ['viewer', 'bootstrap']}
		}
	});
</script>

2.在需要的地方

define(['jquery.easyui.app', 'common/event', 'common/method','album'], function($, event, method,album) {
    var module = {
        init: function(e){
            $('.docs-pictures').viewer();
        }
    };
    return module;
});

3.写好前端并加入js
参考文献:http://www.ruanyifeng.com/blog/2012/11/require_js.html

MAC OS .DS_store隐藏

2016年8月22日

删除当前目录的.DS_store

find . -name '*.DS_Store' -type f -delete

删除所有目录的.DS_store

sudo find / -name “.DS_Store” -depth -exec rm {} \;

禁止.DS_store生成:

defaults write com.apple.desktopservices DSDontWriteNetworkStores -bool TRUE

恢复.DS_store生成:

defaults delete com.apple.desktopservices DSDontWriteNetworkStores

ElasticSearch同步MySql

2016年8月19日

ElasticSearch同步MySQL的插件选择了elasticsearch-jdbc.

一、下载

下载地址:https://github.com/jprante/elasticsearch-jdbc
下载后解压,里面有bin、lib2个目录.

二、mysql配置

确保mysql能用,在mysql中新建一个test数据库

mysql>create database test;

新建一张user表

mysql> create table user(id int(10) Not null,name char(10));

插入几条数据.

mysql> insert into test values("1","zhangsan");
mysql> insert into user values("2","LiSi");
mysql> insert into user values("3","WangWu");
mysql> insert into user values("4","MaLiu");

查看所有数据:

mysql> select * from user;
+----+----------+
| id | name     |
+----+----------+
|  1 | zhangsan |
|  2 | LiSi     |
|  3 | WangWu   |
|  4 | MaLiu    |
+----+----------+
4 rows in set (0.00 sec)

这样mysql中的数据就准备好了.

三、导入数据

新建一个odbc_es文件夹,新建mysql_import_es.sh脚本,脚本内容:

bin=/Users/yaopan/Documents/bropen/elasticsearch-jdbc-2.3.2.0/bin
lib=/Users/yaopan/Documents/bropen/elasticsearch-jdbc-2.3.2.0/lib
echo '{
    "type" : "jdbc",
    "jdbc" : {
        "elasticsearch.autodiscover":true,
        "elasticsearch.cluster":"bropen",
        "url" : "jdbc:mysql://localhost:3306/test",
        "user" : "root",
        "useSSL":"true",
        "password" : "123456",
        "sql" : "select *, id as _id from user",
        "elasticsearch" : {
             "host" : "127.0.0.1",
             "port" : 9300
        },
        "index" : "test", 
        "type" : "user"
    }
}' | java \
       -cp "${lib}/*" \
       -Dlog4j.configurationFile=${bin}/log4j2.xml \
       org.xbib.tools.Runner \
       org.xbib.tools.JDBCImporter

其中bin和lib用了绝对路径.
添加可执行权限:

chmod a+x mysql_import_es.sh 

执行脚本:

./mysql_import_es.sh

报了一个SSL连接的警告,没有错误.如果出现Error: Could not find or load main class org.xbib.tools.Runner之类的错误,很有可能是bin和lib路径出现问题.

查看导入结果 or head插件中查看.

http://localhost:9200/test/user/_search?pretty

参考文章

ElasticSearch同步Mysql
elasticsearch-jdbc实现MySQL同步到ElasticSearch深入详解

ElasticSearch Java Api(四) -删除索引

2016年8月18日

删除可以是删除整个索引库,也可以根据文档id删除索引库下的文档,还可以通过query查询条件删除所有符合条件的数据。

一、删除整个索引库

下面的例子会删除indexName索引:

DeleteIndexResponse dResponse = client.admin().indices().prepareDelete(indexName)
                        .execute().actionGet();

可以根据DeleteIndexResponse对象的isAcknowledged()方法判断删除是否成功,返回值为boolean类型.
如果传人的indexName不存在会出现异常.可以先判断索引是否存在:

IndicesExistsRequest inExistsRequest = new IndicesExistsRequest(indexName);

IndicesExistsResponse inExistsResponse = client.admin().indices()
                    .exists(inExistsRequest).actionGet();

根据IndicesExistsResponse对象的isExists()方法的boolean返回值可以判断索引库是否存在.

二、通过ID删除

下面的例子是删除索引名为blog,类型为article,id为1的文档:

DeleteResponse dResponse = client.prepareDelete("blog", "article", "1").execute().actionGet();

通过DeleteResponse对象的isFound()方法,可以得到删除是否成功,返回值为boolean类型.

三、通过Query删除

elasticsearch-2.3 中和旧版本api不太一样,安装插件:

sudo bin/plugin install delete-by-query

集群有多个节点的情况下,每个节点都需要安装并重启.
如果想要移除插件,可以执行以下命令:

sudo bin/plugin remove delete-by-query

删除索引名为twitter,类型为tweet,user字段中含有kimchy的所有文档:

DELETE /twitter/tweet/_query?q=user:kimchy

四、java demo

package cn.com.bropen.es;

import static org.elasticsearch.index.query.QueryBuilders.termQuery;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilder;

public class ElasticSearchCreate {

    private static String ServerIP = "127.0.0.1";// ElasticSearch server ip
    private static int ServerPort = 9300;// port
    private Client client;

    public static void main(String[] args) {

        try {
            Client client = TransportClient.builder().build().addTransportAddress(
                    new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

            DeleteResponse dResponse = client.prepareDelete("blog", "article", "11").execute()
                    .actionGet();

            if (dResponse.isFound()) {
                System.out.println("删除成功");
            } else {
                System.out.println("删除失败");
            }

            QueryBuilder qb1 = termQuery("title", "hibernate");


        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

         deleteIndex("test");//删除名为test的索引库
    }

    // 删除索引库

    public static void deleteIndex(String indexName) {

        try {
            if (!isIndexExists(indexName)) {
                System.out.println(indexName + " not exists");
            } else {
                Client client = TransportClient.builder().build().addTransportAddress(
                        new InetSocketTransportAddress(InetAddress.getByName(ServerIP),
                                ServerPort));

                DeleteIndexResponse dResponse = client.admin().indices().prepareDelete(indexName)
                        .execute().actionGet();
                if (dResponse.isAcknowledged()) {
                    System.out.println("delete index "+indexName+"  successfully!");
                }else{
                    System.out.println("Fail to delete index "+indexName);
                }
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    // 创建索引库
    public static void createIndex(String indexName) {
        try {
            Client client = TransportClient.builder().build().addTransportAddress(
                    new InetSocketTransportAddress(InetAddress.getByName(ServerIP), ServerPort));

            // 创建索引库

            if (isIndexExists("indexName")) {
                System.out.println("Index  " + indexName + " already exits!");
            } else {
                CreateIndexRequest cIndexRequest = new CreateIndexRequest("indexName");
                CreateIndexResponse cIndexResponse = client.admin().indices().create(cIndexRequest)
                        .actionGet();
                if (cIndexResponse.isAcknowledged()) {
                    System.out.println("create index successfully!");
                } else {
                    System.out.println("Fail to create index!");
                }

            }

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

    }

    // 判断索引是否存在 传入参数为索引库名称
    public static boolean isIndexExists(String indexName) {
        boolean flag = false;
        try {
            Client client = TransportClient.builder().build().addTransportAddress(
                    new InetSocketTransportAddress(InetAddress.getByName(ServerIP), ServerPort));

            IndicesExistsRequest inExistsRequest = new IndicesExistsRequest(indexName);

            IndicesExistsResponse inExistsResponse = client.admin().indices()
                    .exists(inExistsRequest).actionGet();

            if (inExistsResponse.isExists()) {
                flag = true;
            } else {
                flag = false;
            }

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        return flag;
    }

}

ElasticSearch Java Api(三) -更新索引库

2016年8月18日

官网文档:https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-update.html

一、UpdateRequest

创建一个UpdateRequest,然后将其发送给client.

UpdateRequest uRequest = new UpdateRequest();
            uRequest.index("blog");
            uRequest.type("article");
            uRequest.id("2");
            uRequest.doc(jsonBuilder().startObject().field("content", "学习目标 掌握java泛型的产生意义ssss").endObject());
            client.update(uRequest).get();

二、prepareUpdate()

2.1使用脚本方式

首先打开elasticsearch-2.3.3/config/elasticsearch.yml,新增一行:

script.engine.groovy.inline.update: on

之后重启elasticsearch.

client.prepareUpdate("blog", "article", "1")
                .setScript(new Script("ctx._source.title = \"git入门\"", ScriptService.ScriptType.INLINE, null, null))
                .get();

2.2使用doc方式

client.prepareUpdate("blog", "article", "1")
                    .setDoc(jsonBuilder().startObject().field("content", "SVN与Git对比。。。").endObject()).get();

三、updateRequest

UpdateRequest updateRequest = new UpdateRequest("blog", "article", "1")
                    .doc(jsonBuilder().startObject().field("commet", "0").endObject());
            client.update(updateRequest).get();

这种方式可以新增字段。

四、upsert

如果文档不存在则创建新的索引.

IndexRequest indexRequest = new IndexRequest("blog", "article", "10").source(jsonBuilder().startObject()
                    .field("title", "Git安装10").field("content", "学习目标 git。。。10").endObject());

UpdateRequest uRequest2 = new UpdateRequest("blog", "article", "10").doc(
                    jsonBuilder().startObject().field("title", "Git安装").field("content", "学习目标 git。。。").endObject())
                    .upsert(indexRequest);
            client.update(uRequest2).get();

这个例子中,如果blog/article/10存在,那么根据UpdateRequest更新索引;如果不存在,新建indexRequest索引.

五、java demo

package cn.com.bropen.es;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutionException;

import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptService;

import static org.elasticsearch.common.xcontent.XContentFactory.*;

public class ElasticSearchUpdate {

    private static Client client;

    public static void main(String[] args) {

        try {
            // client startup
            client = TransportClient.builder().build()
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        upMethod1();

    }

    public static void upMethod1() {
        try {
            // 方法一:创建一个UpdateRequest,然后将其发送给client.
            UpdateRequest uRequest = new UpdateRequest();
            uRequest.index("blog");
            uRequest.type("article");
            uRequest.id("22");
            uRequest.doc(jsonBuilder().startObject().field("content", "学习目标 掌握java泛型的产生意义ssss").endObject());
            client.update(uRequest).get();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }

    public static void upMethod2() {
        // 方法二:prepareUpdate() 使用脚本更新索引
        client.prepareUpdate("blog", "article", "1")
                .setScript(new Script("ctx._source.title = \"git入门\"", ScriptService.ScriptType.INLINE, null, null))
                .get();
    }

    public static void upMethod3() {
        // 方法三:prepareUpdate() 使用doc更新索引
        try {
            client.prepareUpdate("blog", "article", "1")
                    .setDoc(jsonBuilder().startObject().field("content", "SVN与Git对比。。。").endObject()).get();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void upMethod4() {
        // 方法四: 增加新的字段

        try {
            UpdateRequest updateRequest = new UpdateRequest("blog", "article", "1")
                    .doc(jsonBuilder().startObject().field("commet", "0").endObject());
            client.update(updateRequest).get();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

    public static void upMethod5() {
        // 方法五:upsert 如果文档不存在则创建新的索引
        try {
            IndexRequest indexRequest = new IndexRequest("blog", "article", "10").source(jsonBuilder().startObject()
                    .field("title", "Git安装10").field("content", "学习目标 git。。。10").endObject());

            UpdateRequest uRequest2 = new UpdateRequest("blog", "article", "10").doc(
                    jsonBuilder().startObject().field("title", "Git安装").field("content", "学习目标 git。。。").endObject())
                    .upsert(indexRequest);
            client.update(uRequest2).get();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }
}

ElasticSearch Java Api(二) -检索索引库

2016年8月18日

一、准备数据

 String data1 = JsonUtil.model2Json(new Blog(1, "git简介", "2016-06-19", "SVN与Git最主要的区别..."));
    String data2 = JsonUtil.model2Json(new Blog(2, "Java中泛型的介绍与简单使用", "2016-06-19", "学习目标 掌握泛型的产生意义..."));
    String data3 = JsonUtil.model2Json(new Blog(3, "SQL基本操作", "2016-06-19", "基本操作:CRUD ..."));
    String data4 = JsonUtil.model2Json(new Blog(4, "Hibernate框架基础", "2016-06-19", "Hibernate框架基础..."));
    String data5 = JsonUtil.model2Json(new Blog(5, "Git基本知识git", "2016-06-19", "Shell是什么..."));
    String data6 = JsonUtil.model2Json(new Blog(6, "C++基本知识", "2016-06-19", "Shell是什么..."));
    String data7 = JsonUtil.model2Json(new Blog(7, "Mysql基本知识", "2016-06-19", "git是什么..."));

二、查询

一次查询可分为下面四个步骤:

    • 1.创建连接ElasticSearch服务的client.
      索引在ElasticSearch服务器上,进行索引的查询首先要和服务器创建连接,这是第一步。
Client client = TransportClient.builder().build()
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
    • 2.创建QueryBuilder.
      QueryBuilder可以设置单个字段的查询,也可以设置多个字段的查询.
      e.g.1: 查询title字段中包含hibernate关键字的文档:

      QueryBuilder qb1 = termQuery("title", "hibernate");

      e.g.2: 查询title字段或content字段中包含Git关键字的文档:

QueryBuilder qb2= QueryBuilders.multiMatchQuery("git", "title","content");
    • 3.执行查询
      通过client设置查询的index、type、query.返回一个SearchResponse对象:
SearchResponse response = client.prepareSearch("blog").setTypes("article").setQuery(qb2).execute()
            .actionGet();
    • 4.处理查询结果
      SearchResponse对象的getHits()方法获取查询结果,返回一个SearchHits的集合,遍历集合获取查询的文档信息:
SearchHits hits = response.getHits();

三、java代码实现

package cn.com.bropen.es;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;

import static org.elasticsearch.index.query.QueryBuilders.*;

public class ElasticSearchGet {

    public static void main(String[] args) {
        // client startup
        try {
            Client client = TransportClient.builder().build()
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

            QueryBuilder qb1 = termQuery("title", "hibernate");
            QueryBuilder qb2= QueryBuilders.multiMatchQuery("git", "title","content");


            SearchResponse response = client.prepareSearch("blog").setTypes("article").setQuery(qb2).execute()
                    .actionGet();

            SearchHits hits = response.getHits();
            if (hits.totalHits() > 0) {
                for (SearchHit hit : hits) {
                    System.out.println("score:"+hit.getScore()+":\t"+hit.getSource());// .get("title")
                }
            } else {
                System.out.println("搜到0条结果");
            }

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

    }

}

查询结果:

log4j:WARN No appenders could be found for logger (org.elasticsearch.plugins).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
score:0.5:  {posttime=2016-06-19, id=1, title=git简介, content=SVN与Git最主要的区别...}
score:0.17673586:   {posttime=2016-06-19, id=7, title=Mysql基本知识, content=git是什么...}
score:0.049935166:  {posttime=2016-06-19, id=5, title=Git基本知识git, content=Shell是什么...}

参考资料:

 

ElasticSearch Java Api(一) -创建索引

2016年8月18日

ElasticSearch Java API官网文档:https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-index.html

一、生成JSON

创建索引的第一步是要把对象转换为JSON字符串.官网给出了四种创建JSON文档的方法:

1.1手写方式生成

String json = "{" +
        "\"user\":\"kimchy\"," +
        "\"postDate\":\"2013-01-30\"," +
        "\"message\":\"trying out Elasticsearch\"" +
    "}";

手写方式很简单,但是要注意日期格式:Date Formate

1.2使用集合

集合是key:value数据类型,可以代表json结构.

Map<String, Object> json = new HashMap<String, Object>();
json.put("user","kimchy");
json.put("postDate",new Date());
json.put("message","trying out Elasticsearch");

1.3使用JACKSON序列化

ElasticSearch已经使用了jackson,可以直接使用它把javabean转为json.

// instance a json mapper
ObjectMapper mapper = new ObjectMapper(); // create once, reuse

// generate json
byte[] json = mapper.writeValueAsBytes(yourbeaninstance);

1.4使用ElasticSearch 帮助类

import static org.elasticsearch.common.xcontent.XContentFactory.*;

XContentBuilder builder = jsonBuilder()
    .startObject()
        .field("user", "kimchy")
        .field("postDate", new Date())
        .field("message", "trying out Elasticsearch")
    .endObject()

 String json = builder.string();

二、创建索引

下面的例子把json文档写入所以,索引库名为twitter、类型为tweet,id为1:

import static org.elasticsearch.common.xcontent.XContentFactory.*;

IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
        .setSource(jsonBuilder()
                    .startObject()
                        .field("user", "kimchy")
                        .field("postDate", new Date())
                        .field("message", "trying out Elasticsearch")
                    .endObject()
                  )
        .get();

也可以直接传人JSON字符串:

String json = "{" +
        "\"user\":\"kimchy\"," +
        "\"postDate\":\"2013-01-30\"," +
        "\"message\":\"trying out Elasticsearch\"" +
    "}";

IndexResponse response = client.prepareIndex("twitter", "tweet")
        .setSource(json)
        .get();

可以调用response对象的方法获取返回信息:

// 索引名称
String _index = response.getIndex();
// 类型名称
String _type = response.getType();
// 文档id
String _id = response.getId();
// 版本(if it's the first time you index this document, you will get: 1)
long _version = response.getVersion();
// 是否被创建is true if the document is a new one, false if it has been updated
boolean created = response.isCreated();

更简单的可以直接System.out.println(response)查看返回信息.

三、java实现

新建一个java项目,导入elasticsearch-2.3.3/lib目录下的jar文件.新建一个Blog类:

public class Blog {
    private Integer id;
    private String title;
    private String posttime;
    private String content;

    public Blog() {
    }

    public Blog(Integer id, String title, String posttime, String content) {
        this.id = id;
        this.title = title;
        this.posttime = posttime;
        this.content = content;
    }
  //setter and getter  
}

创建java实体类转json工具类:

import java.io.IOException;

import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;

public class JsonUtil {

    // Java实体对象转json对象
    public static String model2Json(Blog blog) {
        String jsonData = null;
        try {
            XContentBuilder jsonBuild = XContentFactory.jsonBuilder();
            jsonBuild.startObject().field("id", blog.getId()).field("title", blog.getTitle())
                    .field("posttime", blog.getPosttime()).field("content",blog.getContent()).endObject();

            jsonData = jsonBuild.string();
            //System.out.println(jsonData);

        } catch (IOException e) {
            e.printStackTrace();
        }

        return jsonData;
    }

}

添加数据,返回一个list:

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DataFactory {
    public static DataFactory dataFactory = new DataFactory();

    private DataFactory() {
    }

    public DataFactory getInstance() {
        return dataFactory;
    }

    public static List<String> getInitJsonData() {
        List<String> list = new ArrayList<String>();
        String data1 = JsonUtil.model2Json(new Blog(1, "git简介", "2016-06-19", "SVN与Git最主要的区别..."));
        String data2 = JsonUtil.model2Json(new Blog(2, "Java中泛型的介绍与简单使用", "2016-06-19", "学习目标 掌握泛型的产生意义..."));
        String data3 = JsonUtil.model2Json(new Blog(3, "SQL基本操作", "2016-06-19", "基本操作:CRUD ..."));
        String data4 = JsonUtil.model2Json(new Blog(4, "Hibernate框架基础", "2016-06-19", "Hibernate框架基础..."));
        String data5 = JsonUtil.model2Json(new Blog(5, "Shell基本知识", "2016-06-19", "Shell是什么..."));
        list.add(data1);
        list.add(data2);
        list.add(data3);
        list.add(data4);
        list.add(data5);
        return list;
    }

}

创建索引、添加数据:

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;

import cn.com.bropen.entity.DataFactory;
import static org.elasticsearch.common.xcontent.XContentFactory.*;

public class ElasticSearchHandler {
    public static void main(String[] args) {
        try {
            /* 创建客户端 */
            // client startup
            Client client = TransportClient.builder().build()
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

            XContentBuilder builder = jsonBuilder().startObject().field("user", "kimchy").field("postDate", new Date())
                    .field("message", "trying out Elasticsearch").endObject();


            List<String> jsonData = DataFactory.getInitJsonData();

            for (int i = 0; i < jsonData.size(); i++) {
                IndexResponse response = client.prepareIndex("blog", "article").setSource(jsonData.get(i)).get();
                if (response.isCreated()) {
                   System.out.println("创建成功!");
                }
            }
            client.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

可以通过head插件查看插入的数据。