curl -H "Content-Type:application/json"-H "token:4113c9cad1c301113783f433e254888c"-H "appKey:31abd9593e9983ec"-X POST --data '{"sql":"SELECT xwho,xwhat,xwhen,\"$browser\" FROM event_vd as event where event.ds=20200214 and event.\"$startup_time\" is not null LIMIT 2","format":"csv"}' http://127.0.0.1:4005/uba/api/sql/query
2. 自定义SQL导出
适合sql查询结果为较大数据量的场景(百万级数据量),例如导出事件数据,导出用户数据
2.1 接口地址
【POST】 /uba/api/sql/export
2.2 请求参数
2.2.1 Body参数说明:
body参数示例:
{"sql":"SELECT xwhat,xwhen,xwho FROM event_vd as event where event.ds=20200214 and event.\"$startup_time\" is not null LIMIT 10","format":"json"}
body参数说明:
sql: 查询的 SQL,例如 SELECT xwhat,xwhen,xwho FROM event_vd LIMIT 10。
curl -o output.json -H "Content-Type:application/json"-H "token:3edbaf427ecdda80beef322ad3c333a4"-H "appKey:31abd9593e9983ec"-X POST --data '{"sql":"SELECT xwho,xwhat,xwhen,\"$browser\" FROM event_vd as event where event.ds=20200214","format":"json"}' http://127.0.0.1:4005/uba/api/sql/export
2.5 Java HttpClient 接口调用示例
publicvoidtestSqlExport(){// 依赖 org.apache.httpcomponents 的 httpclient 包staticString UTF ="UTF-8"; CloseableHttpClient client =HttpClients.createDefault();HttpPost httpPost =newHttpPost("http://127.0.0.1:4005/uba/api/sql/export");//Header 参数httpPost.setHeader("Content-Type","application/json");httpPost.setHeader("appKey", YOUR_APP_KEY);httpPost.setHeader("token", YOUR_APP_TOKEN);// post body paramsMap<String,Object> params =newHashMap<>();params.put("format","json");params.put("sql","select * from profile_vd where arkfq_5=1");ObjectMapper objectMapper =newObjectMapper();StringEntity stringEntity =newStringEntity(objectMapper.writeValueAsString(params), UTF);stringEntity.setContentEncoding(UTF);httpPost.setEntity(stringEntity);//set timeout RequestConfig reqConfig =RequestConfig.custom().setSocketTimeout(10*60*1000).setConnectTimeout(60*1000).build();httpPost.setConfig(reqConfig);//假设存到 output.txt(也可以是其他处理方式)File dir =newFile("/tmp");File file =newFile(dir,"output.txt");try (CloseableHttpResponse response1 =client.execute(httpPost);FileOutputStream fos =newFileOutputStream(file)) {finalHttpEntity entity =response1.getEntity();if (entity !=null) {try (InputStream inputStream =entity.getContent()) {// TODO: do something useful with the streamtry (BufferedReader br =newBufferedReader(new InputStreamReader(inputStream))) {String line;while ((line =br.readLine()) !=null) {// TODO: process the line.// 以文件存储为例,按行读取,按行写入fos.write((line+"\n").getBytes(UTF));\ } } }EntityUtils.consume(entity); } } }