通用查询引擎Restful service设计(目前支持hive,shark)

来源:互联网 发布:淘宝怎么办信用卡 编辑:程序博客网 时间:2024/05/22 00:48

最近在设计开发一个通用查询Restful Service (https://github.com/lalaguozhe/polestar-1) ,项目名polestar (中文名叫北极星,野营灯,指导者,希望把大家的查询语句都吸引汇聚过来,你懂的) ,之前查询Hive语句基本都是走Hive Server,但是Hive Server 1不太完善,比如

1. 有编译器memory leak问题

2. thrift api不支持multiple connections和client sessions

3. 提交语句后就会被block住,无法实时获取执行状态信息

4. 不支持authentication(kerberos)

这些问题要到hive server 2才能解决(https://issues.apache.org/jira/browse/HIVE-2935),因此我们开发一个统一查询语句的Restul Service,让用户把查询语句(Hive, Shark和Phoenix等) 都通过restful api提交上来,后台worker节点会以command line的方式把它启起来,然后把结果和中间状态信息返回给他。

Polestar 架构图


最上层是应用层,比如Hive Web(用户在edit box自定义查询语句), 运营工具(DW的报表工具,会生成Query模版查询), ad hoc (其他应用ad hoc查询),所有请求都会经过HAProxy+Keepalived做load balance,HAProxy支持多种balance algorithm,默认是leastconn,我们这边使用source,也就是根据client的IP和server权重进行hash。接下来请求就会被转发到某一台worker节点。每一个worker节点都是独立部署,安装有Hive, Shark的客户端,根据用户指定的执行引擎起不同的process处理,并且抓取stdout和stderr

Restful API:

@Path("/query")public class PolestarController {private IQueryService queryService = DefaultQueryService.getInstance();@GET@Produces(MediaType.TEXT_PLAIN)public String getQueryId() {return queryService.getQueryID();}@GET@Path("/status/{id}")@Produces(MediaType.APPLICATION_JSON)public QueryStatus getQueryStatus(@PathParam("id") String id) {return queryService.getStatusInfo(id);}@GET@Path("/download/{filename}")@Produces(MediaType.APPLICATION_OCTET_STREAM)public Response get(@PathParam("filename") String filename) {return Response.ok(queryService.getDataFile(filename)).build();}@GET@Path("/cancel/{id}")@Produces(MediaType.APPLICATION_JSON)public Boolean cancelQuery(@PathParam("id") String id) {return queryService.cancel(id);}@POST@Path("/post")@Consumes(MediaType.APPLICATION_JSON)@Produces(MediaType.APPLICATION_JSON)public Response postQuery(Query query) {QueryResult result = queryService.postQuery(query);return Response.status(Status.CREATED).entity(result).build();}}

每次提交查询语句前都需要通过get /query请求获取一个唯一的QueryID,然后组装id, sql, mode, database, username, password,storeResult这些信息成JSON格式,再post到/query/post路径,执行完成后会返回一个QueryResult对象的JSON格式,执行期间可以get /status/{id}实时获取执行中间状态信息,比如已经执行到第几个job了,每个job的map和reduce完成情况。 也可以get /cancel/{id}来kill启动在worker节点上的client jvm和已经提交的mr job

用户如果指定storeResult=true,则会把query结果gzip压缩后put到HDFS上,之后通过get /download/{filename} 就可以下载了。否则只会默认返回最多500条record作为预览

目前第一阶段支持hive和shark,接下来会支持phoenix(salesforce出的构建在HBase之上的SQL引擎)


客户端测试代码:

@Testpublic void testPostQuery() {try {Client client = Client.create();WebResource webResource = client.resource("http://10.1.77.84:8080/polestar/query/post");String sql = "show tables";String input = "{\"sql\":\"" + sql + "\",\"mode\":\"hive\","+ "\"database\":\"default\","+ "\"username\":\"yukang.chen\","+ "\"password\":\"yukang.chen\","+ "\"storeResult\":\"true\"," + "\"id\":\"5685854852\"}";ClientResponse response = webResource.type("application/json").post(ClientResponse.class, input);if (response.getStatus() != 201) {throw new RuntimeException("Failed : HTTP error code : "+ response.getStatus());}System.out.println("Output from Server .... \n");String output = response.getEntity(String.class);System.out.println(output);} catch (Exception e) {e.printStackTrace();}}

设置为保存结果文件

{"data":[],"success":true,"columnNames":[],"execTime":7,"errorMsg":"","resultFilePath":"hdfs://10.1.77.86/data/polestar/411260fa51524eaa99665066c6e9cc51.gz","id":"5685854852"}

不保存结果文件,直接返回结果

{"data":[["dpods_mc_table_info"],["guid_count"],["hbasetable"],["hcatalog_test"],["hippolog"],["hippolog_input_nosort"],["hippolog_input_sort"],["hippolog_partitioner"],["hippologcurrent"],["hippologsum"],["jinss_test_1129"],["lzo_rcfile_test"],["metastore_stress_testing"],["newmysqltable"],["nginx"],["nginx_bak"],["nginx_search_condition"],["nginxlogcurrent"],["nginxlogcurrenttmp"],["range_keys"],["rcfilenginx_gz"],["result"],["search_log"],["searchexec"],["test"],["test1"],["testresult"],["tt1"],["tt2"],["tt3"],["ttt1"]],"success":true,"columnNames":["tab_name"],"execTime":3,"errorMsg":"","resultFilePath":"","id":"5685854852"}

本文链接http://blog.csdn.net/lalaguozhe/article/details/9614061,转载请注明