stu

Java2019. 3. 11. 19:42

package com.Elastic.srv;


import java.io.IOException;


import org.apache.lucene.queryparser.flexible.core.builders.QueryBuilder;

import org.apache.lucene.search.join.ScoreMode;

import org.apache.lucene.search.spans.SpanTermQuery;

import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;

import org.elasticsearch.action.admin.indices.get.GetIndexRequest;

import org.elasticsearch.action.get.GetRequest;

import org.elasticsearch.action.get.GetResponse;

import org.elasticsearch.action.index.IndexResponse;

import org.elasticsearch.action.search.SearchRequest;

import org.elasticsearch.action.search.SearchResponse;

import org.elasticsearch.client.RequestOptions;

import org.elasticsearch.client.RestHighLevelClient;

import org.elasticsearch.index.query.BoolQueryBuilder;

import org.elasticsearch.index.query.MatchAllQueryBuilder;

import org.elasticsearch.index.query.MatchQueryBuilder;

import org.elasticsearch.index.query.NestedQueryBuilder;

import org.elasticsearch.index.query.QueryBuilders;

import org.elasticsearch.search.SearchHit;

import org.elasticsearch.search.SearchHits;

import org.elasticsearch.search.builder.SearchSourceBuilder;

import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;


public class MAIN {

static private String defaultIndex = "hex_color_list"; // index

static private RestHighLevelClient rhlc = null;

static public void main(String[] args) throws IOException {

System.out.println("test ... 중");

rhlc =  ElasticSearchConnector.getClient();

//doMatchingQuery();

//defaultFullTextSearch();

//tstFunc();

getDocument();

System.out.println("test ... 끝");

}

static public void tstFunc() {

//GetIndexRequest request = new GetIndexRequest()

SearchRequest sr = new SearchRequest();

SearchSourceBuilder sb = new SearchSourceBuilder();

sb.query(QueryBuilders.nestedQuery

("obj1", QueryBuilders.boolQuery()

.must(QueryBuilders.matchQuery("obj1.name", "john"))

.must(QueryBuilders.rangeQuery("obj1.age")

.from(5)

.to(31)

.includeLower(true)

.includeUpper(true)), ScoreMode.Avg));

sr.source(sb);

try {

SearchResponse response = rhlc.search(sr, RequestOptions.DEFAULT);

SearchHits searchHits = response.getHits();


for (SearchHit hit : searchHits) {

// totalHits 

System.out.println(hit.getSourceAsString());

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

static public void aliasSearch() {

IndicesAliasesRequest request = new IndicesAliasesRequest();

}

static public void doMatchingQuery() throws IOException {

// [1] create SearchRequest

SearchRequest sr = new SearchRequest();

SearchSourceBuilder sb = new SearchSourceBuilder();

//sb.query(QueryBuilders.matchQuery("name", "b84c4c"));


//SpanTermQuery("name", "b84c4c");

sr.indices(defaultIndex);

sr.source(sb);

SearchResponse response = rhlc.search(sr, RequestOptions.DEFAULT);

SearchHits searchHits = response.getHits();

//System.out.println(searchHits);

// 매칭 되는 갯수 

long totalHits = searchHits.getTotalHits();

// 얼마만큼 매칭 되는지 

double maxScore = searchHits.getMaxScore();

System.out.println(totalHits);

System.out.println(maxScore);

for (SearchHit hit : searchHits) {

// totalHits 

System.out.println(hit.getSourceAsString());

}

}

static private void SpanTermQuery(String string, String string2) {

// TODO Auto-generated method stub

}

static public void getDocument() {

// index, type, id

GetRequest getRequest = new GetRequest("hex_color_list","doc","1");

try {

GetResponse getResponse = rhlc.get(getRequest, RequestOptions.DEFAULT);

String id   = getResponse.getId();

String idx  = getResponse.getIndex();

String type = getResponse.getType();

String sourceAsString = getResponse.getSourceAsString();

System.out.println("id : " + id + "  index: " + idx + "  type: " + type );

System.out.println (sourceAsString);

} catch (IOException e) {

System.out.println(e);

}

}


static public void defaultFullTextSearch() throws IOException {

HighlightField field = null;

// [1] create SearchRequest

SearchRequest sr = new SearchRequest();

SearchSourceBuilder sb = new SearchSourceBuilder();

sb.query(QueryBuilders.matchAllQuery());

sr.indices(defaultIndex);

sr.source(sb);

SearchResponse response = rhlc.search(sr, RequestOptions.DEFAULT);

SearchHit[] searchHits = response.getHits().getHits();

//System.out.println(searchHits);

for (SearchHit hit : searchHits) {

field = hit.getHighlightFields().get("name");

System.out.println(hit.getSourceAsString());

}

}

// indx response

static public void indxResponse() {

//IndexResponse response = rhlc.

}

}



'Java' 카테고리의 다른 글

java + beautifulsoup  (0) 2019.02.09
java + mysql 연동  (0) 2019.02.07
java yaml 파일 읽기  (0) 2019.01.20
java_yaml  (0) 2018.12.16

java + beautifulsoup

Java2019. 2. 9. 08:42

package t01;


import java.io.IOException;

import java.text.Format;

import java.time.format.FormatStyle;


import org.json.JSONArray;

import org.json.JSONObject;

import org.jsoup.Jsoup;

import org.jsoup.nodes.Document;

import org.jsoup.nodes.Element;

import org.jsoup.select.Elements;


public class crawl02 {

// ==========================

String url = null;

Document doc = null;

Elements items = null;

JSONArray  jsonArray  = null;

// ==========================

public crawl02(String paramHtml) {

// TODO Auto-generated constructor stub

this.url = paramHtml;

}

public JSONArray urlRequests() {

try {

this.doc = Jsoup.connect(this.url).get();

this.jsonArray = new JSONArray();

for (int p = 1; p <= 2; p++) {

this.items = this.doc.select("ul.ah_l")

.get(p)

.select("li.ah_item");

StringBuilder sb = new StringBuilder();

for (Element t : this.items) {

//System.out.print(t.select("span.ah_k"));

t = t.select("a").get(0);

JSONObject jsonObject = new JSONObject();

jsonObject.put("rnk", t.select("a span.ah_r").text());

jsonObject.put("txt", t.select("span.ah_k").text());

jsonArray.put(jsonObject);

}

// 테스트 코드 

// sb.append("순위 : ");

// sb.append(t.select("a span.ah_r").text());

// sb.append(" ");

// sb.append("내용 : ");

// sb.append(t.select("span.ah_k").text());

// sb.append("\n");

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println(jsonArray);

return jsonArray;

}

}



'Java' 카테고리의 다른 글

stu  (0) 2019.03.11
java + mysql 연동  (0) 2019.02.07
java yaml 파일 읽기  (0) 2019.01.20
java_yaml  (0) 2018.12.16

java + mysql 연동

Java2019. 2. 7. 22:50

package mysql_connect;

import java.sql.*;

class TableName {

// #################################################################################

int num;

String name;

// #################################################################################

public int getNum() {

return num;

}

public void setNum(int num) {

this.num = num;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

public class db {

// #################################################################################

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; // jdbc 드라이버 주소

static final String DB_URL = "jdbc:mysql://localhost:3306/kjh190106?useSSL=false";

static final String USR_NAME = "root";

static final String USR_PSSR = "1234";


public Connection conn = null;

public Statement stmt = null;

public ResultSet rs = null;

// #################################################################################

// 인스턴스 메서드 (1)

public void dbConnect() {

try {

Class.forName(this.JDBC_DRIVER);

conn = DriverManager.getConnection(this.DB_URL, this.USR_NAME, this.USR_PSSR);

// 접속 결과 출력

if (conn != null) {

System.out.println("접속 성공");

} else {

System.out.println("접속 실패");

}

} catch(ClassNotFoundException e) {

e.printStackTrace();

} catch(SQLException e) {

e.printStackTrace();

}

}

// 인스턴스 메서드 (2)

public void tableSearch() {

String query = "SELECT * FROM movi";

TableName tn = new TableName(); // 객체 생성

try {

this.stmt = this.conn.createStatement();

this.rs = this.stmt.executeQuery(query);

while(this.rs.next()) {

tn.setNum(this.rs.getInt("num"));

tn.setName(this.rs.getString("name"));

System.out.println("no : " + tn.getNum() + "  num : " + tn.getName());

}

this.stmt.close();

this.conn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}



'Java' 카테고리의 다른 글

stu  (0) 2019.03.11
java + beautifulsoup  (0) 2019.02.09
java yaml 파일 읽기  (0) 2019.01.20
java_yaml  (0) 2018.12.16

java yaml 파일 읽기

Java2019. 1. 20. 21:22

package test;


import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.InputStream;

import java.io.Reader;

import java.util.Map;


import org.yaml.snakeyaml.Yaml;


public class stu01 {


public static void main(String[] args) throws FileNotFoundException {

// TODO Auto-generated method stub

Yaml y = new Yaml();

Reader yamlFile = new FileReader("C:\\Users\\user\\Desktop\\today_20190120\\test\\src\\main\\java\\test\\tst.yml");

Map<String, Object> yamlMaps = y.load(yamlFile);

System.out.println(yamlMaps.get("name"));

}

}



'Java' 카테고리의 다른 글

stu  (0) 2019.03.11
java + beautifulsoup  (0) 2019.02.09
java + mysql 연동  (0) 2019.02.07
java_yaml  (0) 2018.12.16

java_yaml

Java2018. 12. 16. 17:07

yaml file

----------------------------------------------

my:
name: kim
age: 10


----------------------------------------------



package stu02;

import org.yaml.snakeyaml.Yaml;


import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.util.HashMap;

import java.util.Map;

public class yaml02 {

    public static void main(String[] args) {

        Yaml yamFile = new Yaml();

        Map<String, Map<String, Object>> node = new HashMap<>();

        HashMap<String, Object> xnode = new HashMap<>();

        try {

            File file = new File("C:\\Users\\sleep\\IdeaProjects\\2018_12_15java\\src\\stu02\\info");

            //System.out.println(yamFile.load(new FileInputStream(file)));

            node  = (Map<String, Map<String, Object>>)yamFile.load(new FileInputStream(file));

            xnode = (HashMap<String, Object>)node.get("my");

            System.out.println(xnode.get("name"));

        } catch (FileNotFoundException e) {

            System.out.println(e.getMessage());

        }

    }

}



'Java' 카테고리의 다른 글

stu  (0) 2019.03.11
java + beautifulsoup  (0) 2019.02.09
java + mysql 연동  (0) 2019.02.07
java yaml 파일 읽기  (0) 2019.01.20