Java接口_v2(推荐)

导入

把 esqlx.x.x/api/java/esql-api/libs 中的 jar 加入项目

引用

import java.util.Map;
import java.util.HashMap;
import java.io.File;
import java.io.FileOutputStream;
import java.lang.Thread;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import com.unimas.esql_api.v2.ESql;

ESql接口类定义

public class ESql {
    // 接口返回结构
    public class Result {
        public String state;
        public int code;
        public String message = "";
        public int took = 0;
        public int cost = 0;
        public long count = 0;
        public com.google.gson.JsonArray data;
        // 文件接口专用
        public byte[] file;
    }
    public ESql(String server_host)
    public ESql(String server_host, String username, String password)
    public ESql.Result login()
    public ESql.Result execute(String sql)
    public ESql.Result logout()
    public String fileSet(String file_path) throws IOException
    public String fileSet(String file_path, Map params) throws IOException
    public ESql.Result fileGet(String file_id)
    public void print_result(ESql.Result result)        // 调试时打印返回结构用
}

创建ESQL实例

ESql esql = new ESql("localhost:8001", "username", "password");

服务器开启免认证模式时,无需设置 username / password ,也无需登录(login):

ESql esql = new ESql("localhost:8001");

登录

ESql.Result result = esql.login();
esql.print_result(result);

执行SQL

ESql.Result result = esql.execute("show version;");
esql.print_result(result);

返回的数据字典(result).

登出

ESql.Result result = esql.logout();
esql.print_result(result);

fileSet

设置文件字段 INSERT 时的 VALUES 值

            esql.print_result(esql.execute("CREATE TABLE file_test(my_file FILE {analyzer=ik});"));

            String test_file = args[0] + "/websql/tests/data/es_guide_cn.pdf";

            Map file_set = new HashMap();
            file_set.put("_name", "es_guide_cn.xxx");
            file_set.put("_content_type", "application/xxx");
            file_set.put("_language", "en");
            String my_file_value = esql.fileSet(test_file, file_set);
            esql.print_result(esql.execute("INSERT INTO file_test(my_file()) VALUES(" + my_file_value + ");"));
  • params 参数可以不设置

  • params 参数没设置 _name 时, 会从file_path中自动识别(例子中为xxx.pdf)

  • params 参数可设置的值有:

    _name 文件名称
    _content_type 文件类型
    _language 语言种类

实际索引文件内容时,程序会根据文件内容自动识别文件类型,而不是通过_name中的扩展名 或 _content_type来判断

fileGet

通过 file_id 获取文件内容, 例:

                    ESql.Result result = esql.fileGet(file._file_id);

                    FileOutputStream out = new FileOutputStream(save_file);
                    out.write(result.file);
                    out.close();

                    System.out.println("-- " + file._file_id + "   >   " + save_file  + "    length: " + result.file.length);
                    esql.print_result(result);

这里的 file._file_id 来自 select 检索得到的结果集

完整的API调用样例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.Map;
import java.util.HashMap;
import java.io.File;
import java.io.FileOutputStream;
import java.lang.Thread;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import com.unimas.esql_api.v2.ESql;


public class Test_esql_api_v2 {

    public class FileResult {
        public String _file_id;
    }

    public static void main(String[] args) {

        try {
//            ESql esql = new ESql("10.68.23.81:8001","root","toor");
             ESql esql = new ESql("localhost:8001", "root", "toor");

            String test_file = args[0] + "/websql/tests/data/es_guide_cn.pdf";
            String save_file = "/tmp/es_guide_cn.pdf";
            new File(save_file).delete();

            esql.print_result(esql.login());

            esql.print_result(esql.execute("DROP TABLE file_test;"));
            esql.print_result(esql.execute("CREATE TABLE file_test(my_file FILE {analyzer=ik});"));

            Map file_set = new HashMap();
            file_set.put("_name", "es_guide_cn.xxx");
            file_set.put("_content_type", "application/xxx");
            file_set.put("_language", "en");
            String my_file_value = esql.fileSet(test_file, file_set);
            esql.print_result(esql.execute("INSERT INTO file_test(my_file()) VALUES(" + my_file_value + ");"));
            Thread.sleep(500);  // CREATE(INSERT)成功后,ES 需要时间同步节点
            esql.print_result(esql.execute("FLUSH file_test;"));

            ESql.Result select_result = esql.execute("SELECT * FROM file_test WHERE my_file like '分析';");   // analyser
            esql.print_result(select_result);
            Gson gson = new Gson();
            if (select_result.count > 0) {
                for (int i = 0; i < select_result.count; i++) {
                    JsonObject row = select_result.data.get(i).getAsJsonObject();
                    String file_attr = row.get("my_file").getAsString();

                    FileResult file = gson.fromJson(file_attr, FileResult.class);
                    ESql.Result result = esql.fileGet(file._file_id);

                    FileOutputStream out = new FileOutputStream(save_file);
                    out.write(result.file);
                    out.close();

                    System.out.println("-- " + file._file_id + "   >   " + save_file  + "    length: " + result.file.length);
                    esql.print_result(result);
                }
            }

            esql.print_result(esql.logout());

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

运行接口调用样例

cd ESQL_HOME    # 切换到esql3的安装目录下

java -Djava.ext.dirs=api/java/esql-api/libs Test_esql_api_v2 `pwd`

样例执行结果(正常情况下):

-- Success[0]  count: 0 cost: 0 took: 0 message: log in success!
--------------------------------------------------------------------------

DROP TABLE file_test;

-- Success[0]  count: 0 cost: 345 took: 0 message:
--------------------------------------------------------------------------

CREATE TABLE file_test(my_file FILE);

-- Success[0]  count: 0 cost: 3697 took: 0 message:
--------------------------------------------------------------------------

INSERT INTO file_test(my_file()) values('{"_language":"en","_name":"es_guide_cn.xxx","_content_length":2502274,"_content_type":"application/xxx","_content":"  ...  "}');

-- Success[0]  count: 1 cost: 183 took: 0 message:
-- _type: "base"            _id: "AVgA_f8UOwPeYeLeMqk4"             created: true           _version: 1             _index: "file_test"
------------------------------------------------
--------------------------------------------------------------------------

FLUSH file_test;

-- Success[0]  count: 1 cost: 27 took: 0 message:
-- successful: 10           failed: 0               total: 10
------------------------------------------------
--------------------------------------------------------------------------

SELECT * FROM file_test WHERE my_file like 'table';

-- Success[0]  count: 1 cost: 6 took: 2 message:
-- _type: "base"            _id: "AVgA_f8UOwPeYeLeMqk4"             my_file: "{\"_content_type\": \"application/xxx\", \"_language\": \"en\", \"_content_length\": 2502274, \"_file_id\": \"ZmlsZV90ZXN0LmJhc2U=-ca0aa41c-9b78-11e6-9faa-406c8f21916d-c527\", \"_name\": \"es_guide_cn.xxx\"}"              _index: "file_test"
------------------------------------------------
--------------------------------------------------------------------------

-- ZmlsZV90ZXN0LmJhc2U=-ca0aa41c-9b78-11e6-9faa-406c8f21916d-c527   >   /tmp/sql_92_table.pdf    length: 2502274
-- Success[0]  count: 0 cost: 0 took: 0 message:
--------------------------------------------------------------------------

-- Success[0]  count: 0 cost: 0 took: 0 message: log out success!
--------------------------------------------------------------------------