`
rockelixir
  • 浏览: 309544 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
elasticsearch 批量索引 elasticsearch 批量索引
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

import java.io.IOException;

import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;

/**
 * 批量索引
 * 
 * @author xiaochuan.zhang
 * @version $Id: BulkMappingIndexer.java, v 0.1 2013-6-6 下午4:19:15 xiaochuan.zhang Exp $
 */
public class BulkMappingIndexer {

    public Client client;

    /**
     * 构造函数
     */
    public BulkMappingIndexer() {
        //使用本机做为节点
        this("127.0.0.1");
    }

    /**
     * 构造函数
     * 
     * @param ipAddress
     */
    public BulkMappingIndexer(String ipAddress) {
        /*如果10秒没有连接上搜索服务器,即超时*/
        Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "SusongES")
            .put("client.transport.ping_timeout", "10s").build();
        client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress(
            ipAddress, 9300));
    }

    public Integer addBulkIndex(String indexName, String indexType) {
        BulkRequestBuilder bulkRequest = client.prepareBulk();
        long time = System.currentTimeMillis();
        for (int i = 0; i < 200; i++) {
            try {
                IndexRequestBuilder indexRequestBuilder = client.prepareIndex(indexName, indexType);
                XContentBuilder xContentBuilder = jsonBuilder().startObject().field("id", i)
                    .field("name", "中华人民共和国" + i).endObject();
                indexRequestBuilder.setSource(xContentBuilder);
                bulkRequest.add(indexRequestBuilder);
            } catch (IOException e) {
                System.out.println(e);
            }
        }
        bulkRequest.execute().actionGet();
        bulkRequest.setRefresh(true);
        System.out.println("索引耗时:" + (System.currentTimeMillis() - time) + "ms");
        return bulkRequest.numberOfActions();
    }

    /**
     * 执行索引
     * 
     * @param args
     */
    public static void main(String[] args) {
        BulkMappingIndexer bulkIndexer = new BulkMappingIndexer();
        bulkIndexer.addBulkIndex("index", "SS_ES");
    }
}
elasticsearch 创建索引(二) elasticsearch 创建索引
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

import java.io.IOException;

import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.indices.IndexAlreadyExistsException;

/**
 * index for mapping
 * 
 * @author xiaochuan.zhang
 * @version $Id: MappingIndexer.java, v 0.1 2013-6-6 下午4:11:18 xiaochuan.zhang Exp $
 */
public class MappingIndexer {

    public Client client;

    /**
     * 构造函数
     */
    public MappingIndexer() {
        //使用本机做为节点
        this("127.0.0.1");
    }

    /**
     * 构造函数
     * 
     * @param ipAddress
     */
    public MappingIndexer(String ipAddress) {
        /*如果10秒没有连接上搜索服务器,即超时*/
        Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "SusongES")
            .put("client.transport.ping_timeout", "10s").build();
        client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress(
            ipAddress, 9300));
    }

    public void createMapping(String indexName, String type) {
        try {
            try {
                client.admin().indices().prepareCreate(indexName).execute().actionGet();
                XContentBuilder mapping = XContentFactory.jsonBuilder().startObject()
                    .startObject(type);
                mapping = mapping.startObject("id").field("type", "integer")
                // 创建索引时使用ik解析  
                    .field("indexAnalyzer", "ik")
                    // 搜索时使用ik解析  
                    .field("searchAnalyzer", "ik").field("store", "yes").endObject();
                mapping = mapping.startObject("name").field("type", "string")
                // 创建索引时使用ik解析  
                    .field("indexAnalyzer", "ik")
                    // 搜索时使用ik解析  
                    .field("searchAnalyzer", "ik").field("store", "yes").endObject();
                mapping = mapping.endObject().endObject();
                PutMappingRequest mappingRequest = Requests.putMappingRequest(indexName).type(type)
                    .source(mapping);
                client.admin().indices().putMapping(mappingRequest).actionGet();
            } catch (IndexAlreadyExistsException e) {
            }

            IndexRequestBuilder indexRequestBuilder = client.prepareIndex(indexName, type);
            XContentBuilder xContentBuilder = jsonBuilder().startObject().field("id", 1)
                .field("name", "阿尔卑斯").endObject();
            IndexResponse response = indexRequestBuilder.setSource(xContentBuilder).execute()
                .actionGet();

            indexRequestBuilder.setRefresh(true);

            System.out.println(response.getId() + "==" + response.getIndex() + "=="
                               + response.getType());
        } catch (IOException e) {
            System.out.println(e);
        }
    }

    /**
     * 执行索引
     * 
     * @param args
     */
    public static void main(String[] args) {
        MappingIndexer indexer = new MappingIndexer();
        indexer.createMapping("index", "SS_ES");
    }
}
elasticsearch 创建索引(一) elasticsearch 创建索引
import java.util.List;
import org.elasticsearch.client.Client;
import com.alipay.elasticsearch.data.DataFactory;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

/**
 * index for json
 * 
 * @author xiaochuan.zhang
 * @version $Id: JsonIndexer.java, v 0.1 2013-6-6 下午3:47:09 xiaochuan.zhang Exp $
 */
public class JsonIndexer {

    public Client client;

    /**
     * 构造函数
     */
    public JsonIndexer() {
        //使用本机做为节点
        this("127.0.0.1");
    }

    /**
     * 构造函数
     * 
     * @param ipAddress
     */
    public JsonIndexer(String ipAddress) {
        /*如果10秒没有连接上搜索服务器,即超时,SusongES集群名称*/
        Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "SusongES")
            .put("client.transport.ping_timeout", "10s").build();
        client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress(
            ipAddress, 9300));
    }

    /**
     * 建立索引
     * 
     * @param indexName  为索引库名,一个es集群中可以有多个索引库。 名称必须为小写
     * @param indexType  Type为索引类型,是用来区分同索引库下不同类型的数据的,一个索引库下可以有多个索引类型。
     * @param jsondata   json格式的数据集合
     */
    public void createIndexResponse(IndexRequestBuilder requestBuilder, List<String> jsondata) {
        for (int i = 0; i < jsondata.size(); i++) {
            requestBuilder.setSource(jsondata.get(i)).execute().actionGet();
        }
    }

    /**
     * 索引
     */
    public void createIndexForJson() {
        IndexRequestBuilder indexBuilder = client.prepareIndex("index", "SS_ES");
        for (int i = 0; i < 2; i++) {
            int start = i * 2000;
            int end = start + 2000;
            System.out.println("start=" + start + ",end=" + end);
            List<String> jsondata = DataFactory.getInitJsonData(start, end);
            long time = System.currentTimeMillis();
            createIndexResponse(indexBuilder, jsondata);
            System.out.println("索引耗时:" + (System.currentTimeMillis() - time) + "ms");
        }
        indexBuilder.setRefresh(true);
    }

    /**
     * 执行索引
     * 
     * @param args
     */
    public static void main(String[] args) {
        JsonIndexer indexer = new JsonIndexer();
        indexer.createIndexForJson();
    }
}

import java.util.ArrayList;
import java.util.List;
import com.alipay.elasticsearch.common.JsonUtil;
import com.alipay.elasticsearch.mode.Medicine;

/**
 * 数据工厂
 * 
 * @author xiaochuan.zhang
 * @version $Id: DataFactory.java, v 0.1 2013-5-31 下午7:35:44 xiaochuan.zhang Exp $
 */
public class DataFactory {

    public static DataFactory dataFactory = new DataFactory();

    /**
     * 构造函数
     */
    private DataFactory() {

    }

    /**
     * 获得实例对象
     * 
     * @return
     */
    public DataFactory getInstance() {
        return dataFactory;
    }

    /**
     * 获取数据
     * 
     * @return
     */
    public static List<String> getInitJsonData(int from, int count) {
        List<String> list = new ArrayList<String>();
        for (int i = from; i < count; i++) {
            Integer id = i;
            String name = "数科院"+i+"班";         
            Medicine medicine = new Medicine(id, name);
            list.add(medicine);
        }
        return list;
    }
}

/**
 * 实体对象
 *
 * @author xiaochuan.zhang
 * @version $Id: Medicine.java, v 0.1 2013-5-31 上午10:44:18 xiaochuan.zhang Exp $
 */
public class Medicine{

    private Integer id;
    private String  name;
    
    public Medicine() {
        super();
    }

    public Medicine(Integer id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    /**
     * Getter method for property <tt>id</tt>.
     * 
     * @return property value of id
     */
    public Integer getId() {
        return id;
    }

    /**
     * Setter method for property <tt>id</tt>.
     * 
     * @param id value to be assigned to property id
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     * Getter method for property <tt>name</tt>.
     * 
     * @return property value of name
     */
    public String getName() {
        return name;
    }

    /**
     * Setter method for property <tt>name</tt>.
     * 
     * @param name value to be assigned to property name
     */
    public void setName(String name) {
        this.name = name;
    }
}

import org.codehaus.jackson.map.ObjectMapper;

/**
 * json工具类
 * 
 * @author xiaochuan.zhang
 * @version $Id: JsonUtil.java, v 0.1 2013-5-31 上午10:46:11 xiaochuan.zhang Exp $
 */
public class JsonUtil {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    private JsonUtil() {

    }

    /**
     * 转换object对象到String对象
     * @param object
     * @return
     */
    public static String object2Json(Object object) {
        try {
            String itemValue = objectMapper.writeValueAsString(object);
            return itemValue;
        } catch (Exception e) {
            throw new RuntimeException("object2Json异常", e);

        }
    }
}
elasticsearch 环境搭建 elasticsearch 环境搭建
1. 下载elaticsearch
    http://www.elasticsearch.org/download/下载最新版elasticsearch运行包,目前最新的是0.90.1。文件有三个包:
    bin是运行的脚本,config是设置文件,lib是放依赖的包,新增插件包plugins文件夹,安装插件到此文件夹。

2. 运行elaticsearch
   windows环境bin目录:启动服务:双节elasticsearch.bat文件,停止服务:ctr+c。
   linux环境bin目录:启动服务:sh elasticsearch start,停止服务:sh elasticsearch stop。
   系统启动完成在根目录下面会多两个文件夹:data,logs。
    
3. 插件安装
   3.1 elasticsearch-servicewrapper插件
       https://github.com/elasticsearch/elasticsearch-servicewrapper下载service文件夹,放到es的bin目录下。
       bin/service/sh elasticsearch +
       console 在前台运行es
       start 在后台运行es
       stop 停止es
       install 使es作为服务在服务器启动时自动启动
       remove 取消启动时自动启动
       在service目录下有个elasticsearch.conf配置文件,主要是设置一些java运行环境参数,其中比较重要的参数:
       #es的home路径,不用默认值就可以
       set.default.ES_HOME=<Path to ElasticSearch Home>

       #分配给es的最小内存
       set.default.ES_MIN_MEM=256

       #分配给es的最大内存
       set.default.ES_MAX_MEM=1024

       # 启动等待超时时间(以秒为单位)
       wrapper.startup.timeout=300

       # 关闭等待超时时间(以秒为单位)
       wrapper.shutdown.timeout=300

       # ping超时时间(以秒为单位)
       wrapper.ping.timeout=300

       wrapper.shutdown.timeout=300
       # ping超时时间(以秒为单位)

   3.2 elasticsearch-head插件(集群管理工具)
       下载:https://github.com/Aconex/elasticsearch-head
       linux安装:
       elasticsearch/bin/plugin -install Aconex/elasticsearch-head
       windows安装:
      (1)下载:http://t.cn/zT3965m 
      (2)解压:将elasticsearch-head-master目录下所有内容放到 <ES_HOME>/plugins/head/_site目录。
       运行es
       打开http://localhost:9200/_plugin/head/
   
   
spring事务及ibatis配置 spring事务 ibatis mysql配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

	<!-- 数据库连接(jndi) -->
	<!--
		<bean id="dataSource"
		class="org.springframework.jndi.JndiObjectFactoryBean"> <property
		name="jndiName"> <value>java:comp/env/jdbc/search</value> </property>
		</bean>
	-->

	<!-- 数据源配置 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/empires" />
		<property name="username" value="root" />
		<property name="password" value="" />
	</bean>

	<!-- sqlMapClient 配置 -->
	<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="configLocation">
			<value>classpath:ibatis/sqlmap-config.xml</value>
		</property>
	</bean>

	<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
		<property name="sqlMapClient">
			<ref bean="sqlMapClient" />
		</property>
	</bean>

	<!-- spring 事务配置 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
	</bean>

	<bean id="transactionTemplate"
		class="org.springframework.transaction.support.TransactionTemplate">
		<property name="transactionManager">
			<ref bean="transactionManager" />
		</property>
	</bean>

	<!-- 开启事务,事务管理器不指定使用默认的transactionManager -->
	<tx:annotation-driven />

</beans>
Global site tag (gtag.js) - Google Analytics