Java接口_v1

导入

把 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.unimas.esql_api.v1.ESql;

ESql接口类定义

public class ESql {
    // 接口返回结构
    public class Result {
        public long count=0;
        public String msg;
        public String type;
        public int  took=0;
        public List<Map<String,String>> result;
    }
    public ESql(String server_host)
    public ESql(String server_host, String username, String passwd)
    public ESql.Result dosql(String sql)
}

创建ESQL实例

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

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

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

执行SQL

ESql.Result result = esql.dosql("select * from zry_test;");

返回的数据字典(result).

完整的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
import java.util.Map;
import java.util.HashMap;
import java.io.File;
import java.io.FileOutputStream;
import java.lang.Thread;

import com.unimas.esql_api.v1.ESql;


public class Test_esql_api_v1 {

    public static void main(String[] args) {
        try {
            ESql esql = new ESql("localhost:8001", "root", "toor");
            ESql.Result result = esql.dosql("select * from zry_test;");

            System.out.println(result.count+"  "+result.took +" " +result.msg);
            for (Map<String,String> a : result.result){
                for (Map.Entry<String,String> b:a.entrySet()){
                    System.out.print(b.getKey() +":"+ b.getValue()+" ");
                }
                System.out.println("");
            }

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