package com.c4c.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; public class OracleToETS {
static final String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";//"oracle.jdbc.driver.OracleDriver";//驱动
static final String DB_URL = "jdbc:oracle:thin:@127.0.0.1:1521:s2";//数据库URL static final String USER ="joey";//数据库对应的用户名
static final String PASS = "joey";//登录密码 public static void main(String[] args) {
OracleToETS tmp = new OracleToETS();
List<User> userList = tmp.getAllList();
ESClient esclient = new ESClient();
esclient.initESClient();
esclient.bulkdoc(userList);
} public List<User> getAllList()
{
ResultSet rs = null;//执行SQL语句,返回的结果
Statement stmt = null; //将数据发送到数据库中
Connection conn = null;//建立数据库的连接
List<User> list = new ArrayList<User>();
try
{
Class.forName(JDBC_DRIVER);//oracle数据库驱动
conn = DriverManager.getConnection(DB_URL,USER,PASS);//数据库url 用户名 密码
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM o_user ");
while(rs.next())
{
String tmpId = rs.getString("ID");
String tmpName = rs.getString("NAME");
String tmpAddress = rs.getString("ADDRESS");
String tmpetype = rs.getString("E_TYPE");
String tmpeindustry = rs.getString("E_INDUSTRY");
Date tmpcheckdate = rs.getDate("CHECK_DATE");
list.add(new User(tmpId,tmpName,tmpAddress,tmpetype,tmpeindustry,rs.getString("EMPIRICAL_SCOPE"),tmpcheckdate,
rs.getDate("CREATE_TIME"),rs.getString("LEGAL_PERSON"),rs.getString("REGISTRATION_UNIT"),rs.getString("JURISDICTION_UNIT"),
rs.getString("PHONE_NO"),rs.getString("BAR_ID"),rs.getString("BUILD_ID"),rs.getString("FLOOR_ID"),
rs.getString("ROOM_ID"),rs.getString("BAR_LNG"),rs.getString("BAR_LAT"),rs.getString("BUILD_LNG"),rs.getString("BUILD_LAT"),
rs.getString("ROOM_LNG"),rs.getString("ROOM_LAT")));
}
}catch(Exception e)
{
e.printStackTrace();
}finally
{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
}
package com.c4c.test;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set; import org.apache.lucene.analysis.compound.hyphenation.TernaryTree.Iterator;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.filters.Filters.Bucket;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.After;
import org.junit.Before; /**
* @author joey
*
*/
public class ESClient { private Client client;
/**
* 初始化客户端连接
*/
@Before
public Client initESClient() {
// 配置你的es,如果你的集群名称不是默认的elasticsearch,需要以下这步
Settings settings = Settings.settingsBuilder().put("cluster.name","elasticsearch").build();
// 这里可以同时连接集群的服务器,可以多个,并且连接服务是可访问的
try {
// on startup,因为是默认的集群名,所以不需要setting
client = TransportClient.builder().build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
} catch (UnknownHostException e) {
e.printStackTrace();
}finally
{
if(client != null)
{
System.out.println("连接成功!");
}
}
return client;
} @After
public void closeESClient() {
client.close();
System.out.println("连接关闭!");
} /**
* 创建索引、类型、文档并插入ID为1文档内容
*/
@SuppressWarnings("unused")
private void createIndex() {
IndexResponse response = null;
try {
response = client.prepareIndex("customertest", "joey", "1")
.setSource(XContentFactory.jsonBuilder().startObject()
.field("name", "joey")
.field("time", new Date())
.endObject()
)
.get();
} catch (IOException e) {
e.printStackTrace();
}finally
{
if(response != null)
{
System.out.println("索引创建成功!");
}
}
}
/**
* 获取文档
*/
public void get(){
GetResponse response = client.prepareGet("customer", "external", "1").get();
Set<String> headers = response.getHeaders();
System.out.println(headers);//获取请求头
boolean exists = response.isExists();
System.out.println(exists);//判断索引是否存在
String sourceString = response.getSourceAsString();
System.out.println(sourceString);//获取索引,并打印出索引内容
String id = response.getId();
System.out.println(id);//获取索引id
boolean sourceEmpty = response.isSourceEmpty();
System.out.println(sourceEmpty);//获取索引的内容是否为空
} /**
* 删除
*/
public void delete(){
//DeleteResponse response = client.prepareDelete("customertest", "joey", "1").get(); //删除文档
//删除索引
DeleteIndexResponse deleteIndexResponse = client.admin().indices()
.prepareDelete("s2")
.execute().actionGet();
boolean isFound = deleteIndexResponse.isAcknowledged();
System.out.println(isFound);//返回文档是否存在,存在删除
} /**
* 搜索索引
*/
public void search(){
//创建查询索引
SearchRequestBuilder searchRequestBuilder = client.prepareSearch("s2");
//设置查询索引类型
searchRequestBuilder.setTypes("o_user");
//设置查询类型
searchRequestBuilder.setSearchType(SearchType.DEFAULT);
//设置查询关键字
searchRequestBuilder.setQuery(QueryBuilders.commonTermsQuery("name", "宁波"));
// 设置查询数据的位置,分页用吧
searchRequestBuilder.setFrom(0);
// 设置查询结果集的最大条数
searchRequestBuilder.setSize(10);
// 设置是否按查询匹配度排序
searchRequestBuilder.setExplain(true);
// 最后就是返回搜索响应信息
SearchResponse response = searchRequestBuilder.execute().actionGet();
//System.out.println("响应结果:"+response); //获取搜索文档的结果
SearchHits searchHits = response.getHits();
SearchHit[] hits = searchHits.getHits();
System.out.println("数量:"+hits.length);
for (int i = 0; i < hits.length; i++) {
SearchHit hit = hits[i];
Map<String, Object> result = hit.getSource();
System.out.println("文档结果:"+result);
}
System.out.println("查询索引完毕!");
}
/**
* 批量新增
*/
public void bulkdoc(List<User> list)
{
SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
BulkRequestBuilder bulkRequest = client.prepareBulk();
long b = System.currentTimeMillis();
try {
for(User user:list)
{
String _id = user.getId();
bulkRequest.add(client.prepareIndex("s2", "o_user", _id)
.setSource(XContentFactory.jsonBuilder()
.startObject()
.field("name", user.getName())
.field("address",user.getAddress())
.field("etype",user.getE_type())
.field("e_industry",user.getE_industry())
.field("empirical_scope",user.getEmpirical_scope())
.field("checkdate",dateFormater.format(user.getCheck_date()))
.field("create_time",dateFormater.format(user.getCreate_time()))
.field("legal_person",user.getLegal_person())
.field("registration_unit",user.getRegistration_unit())
.field("jurisdiction_unit",user.getJurisdiction_unit())
.field("phone_no",user.getPhone_no())
.field("bar_id",user.getBar_id())
.field("build_id",user.getBuild_id())
.field("floor_id",user.getFloor_id())
.field("room_id",user.getRoom_id())
.field("bar_lng",user.getBar_lng())
.field("bar_lat",user.getBar_lat())
.field("build_lng",user.getBuild_lng())
.field("build_lat",user.getBuild_lng())
.field("room_lng",user.getRoom_lng())
.field("room_lat",user.getRoom_lat())
.endObject()
)
);
}
} catch (IOException e) {
e.printStackTrace();
}
BulkResponse bulkResponse = bulkRequest.execute().actionGet();
if (bulkResponse.hasFailures()) {
System.out.println(bulkResponse.buildFailureMessage());
}
long useTime = System.currentTimeMillis()-b;
System.out.println("useTime:" + useTime);
}
/**
* 聚合查询
*/
public void aggSearch()
{
SearchRequestBuilder srb = client.prepareSearch("s2");
srb.setSize(0);
srb.setTypes("o_user");
srb.setHighlighterType("o_user");
srb.setSearchType(SearchType.QUERY_THEN_FETCH);
TermsBuilder typeTermsBuilder = AggregationBuilders.terms("etypeAgg").field("etype");
typeTermsBuilder.size(100);
srb.addAggregation(typeTermsBuilder);
SearchResponse sr = srb.execute().actionGet();
System.out.println(sr);
} public void useScrolls()
{
QueryBuilder qb = QueryBuilders.multiMatchQuery("工厂", "name");
SearchResponse scrollResp = client.prepareSearch("s2")
.addSort("name", SortOrder.ASC)
.setScroll(new TimeValue(60000))
.setQuery(qb)
.setSize(100).execute().actionGet(); //100 hits per shard will be returned for each scroll
while (true) {
for (SearchHit hit : scrollResp.getHits().getHits()) {
System.out.println(hit.getScore());
}
scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(60000)).execute().actionGet();
if (scrollResp.getHits().getHits().length == 0) {
break;
}
}
} public static void main(String args[]){
System.out.println("ES集群连接测试");
ESClient esc = new ESClient();
esc.initESClient();
//esc.createIndex();
//esc.get();
//esc.delete();
//esc.search();
//esc.bulkdoc();
// esc.closeESClient();
esc.aggSearch();
//esc.useScrolls();
}
}

将Oracle中的数据放入elasticsearch的更多相关文章

  1. Elasticsearch 2.3.2 从oracle中同步数据

    Elasticsearch 2.3.2 从oracle中同步数据   1         数据批量导入-oracle 采用 elasticsearch-jdbc 插件 安装.版本需要ES版本一致 最新 ...

  2. C语言:把分数最低的学生数据放入数组b所指的数组中,-从键盘输入若干字符串,写入文件myfile4中,用-1作字符输入结束的标志,

    //学生记录由学号和成绩组成,N名学生的数据已放入主函数中的结构体数组中,fun函数:把分数最低的学生数据放入数组b所指的数组中,分数最低的学生可能不止一个.人数返回. #include <st ...

  3. java 实现每次从list中取5000条数据放入新list

    从list中取固定条数的数据放入新的list里 public static <T> List<List<T>> split(List<T> resLis ...

  4. oracle中的数据对象

    oracle中的数据对象有表.视图.索引.序列等 表的相关操作 1.创建表 方式一: 方式二:create table person( create table person1 id number(1 ...

  5. 在Oracle中更新数据时,抛出:ORA-01008: not all variables bound

    在Oracle中更新数据时,抛出了一个 :ORA-01008 not all variables bound, 我的理解是不是所有的变量/参数都有边界,不懂: 后来知道了,原来是“不是所有变量/参数都 ...

  6. MyBatis在Oracle中插入数据并返回主键的问题解决

    引言:  在MyBatis中,希望在Oracle中插入数据之时,同一时候返回主键值,而非插入的条数... 环境:MyBatis 3.2 , Oracle. Spring 3.2   SQL Snipp ...

  7. Sqoop_具体总结 使用Sqoop将HDFS/Hive/HBase与MySQL/Oracle中的数据相互导入、导出

    一.使用Sqoop将MySQL中的数据导入到HDFS/Hive/HBase watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWFyb25oYWRvb3A=/ ...

  8. Oracle中读取数据一些原理研究

    文章很多摘录了 http://blog.163.com/liaoxiangui@126/blog/static/7956964020131069843572/ 同时基于这篇文章的基础上,补充一些学习要 ...

  9. 如何用Excel直接查询Oracle中的数据

    将Oracle中查询的数据保存为Excel文件,通常使用的是PL/SQL Developer. 其实,Excel可直接写SQL语句查询Oracle中数据,在这里,用到ODBC驱动.详细步骤如下: 一. ...

随机推荐

  1. HDU2161 Primes

    /* HDU2161 Primes http://acm.hdu.edu.cn/showproblem.php?pid=2161 数论 水题 注意输入截止条件是n<=0 */ #include ...

  2. 《Python 源码剖析》之对象

    py一切皆对象的实现 Python中对象分为两类: 定长(int等), 非定长(list/dict等) 所有对象都有一些相同的东西, 源码中定义为PyObject和PyVarObject, 两个定义都 ...

  3. HDU 3342 -- Legal or Not【裸拓扑排序 &amp;&amp;水题 &amp;&amp; 邻接表实现】

    Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  4. Android 开发最佳实践

    原文地址:https://github.com/futurice/android-best-practices/blob/master/translations/Chinese/README.cn.m ...

  5. 源码高速定位工具-qwandry

    https://github.com/adamsanderson/qwandry qwandry 能高速定位到我们须要找到 库文件, 项目 的工具. Ruby中实现高速定位的方法有好多种.我知道的有三 ...

  6. c11---位运算相关

    // // main.c // 03-原码反码补码 #include <stdio.h> int main(int argc, const char * argv[]) { // int占 ...

  7. dubbo 解决既是消费者又是提供者 Duplicate application configs 的问题

    首先  有应用A  是 提供者 应用B 来实现既是消费者又是提供者 在应用 B 这边新建两个xml dubbo-consumer.xml  消费者 <!-- 自动扫描注解:通过dubbo实现 - ...

  8. 清北集训Day3T1(转换)

    这题可能是我与正解里的最近的一次了,可以还是sb的把正解叉了. 正解其实比较显然:因为$f(x)$只有81个取值,所以我们可以枚举$f(x)$,然后计算$x$,再判断$x$是否可以转化为$f(x)$ ...

  9. .csv文件内容分隔符

    CSV文件默认以英文逗号做为列分隔符,换行符作为行分隔符.  如果不提供网页形式只用命令行或二进制程序输出数据到CSV,只需要将数据段按,分割,行按\n分割,写入到一个.csv文件即可.  但有时字段 ...

  10. POJ 3624 Charm Bracelet【01背包】

    解题思路:直接套公式就能做的01背包, for(i=1;i<=n;i++) { for(v=w[i];v<=m;v++) f[i,v]=max(f[i,v],f[i-1,v-w[i]]+d ...