在实际使用Presto的过程中,经常会有以下的一些需求。

  • 添加一个新的Catalog
  • 对不再使用的Catalog希望把它删除
  • 修改某个Catalog的参数

但在Presto中如果进行上述的修改,需要重启Presto服务才可以生效,这给集群维护带来额外的工作量之外,还给上层应用带来很不好的使用体验。

如果还不能在开发环境很好运行Presto的话,参考在windows的IDEA运行Presto

观察PrestoServer的run方法,可以知道Presot分了多个模块,而且多个模块依赖airlift(Airlift framework for building REST services)的项目,倒不如说airlift是Presto的根基。说实在话,我对于Presto的理解可能还在管中窥豹的阶段,但是不影响我对它做一些手脚。

1、新增一个Hello world测试接口

在presto-main中com.facebook.presto.server中新增一个CatalogResource,作用和Spring类似吧。

/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.server; import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response; /**
* @author Gin
* @since 2018/8/15.
*/
@Path("/v1/catalog")
public class CatalogResource
{
@Inject
public CatalogResource()
{
} @GET
@Path("test")
public Response test()
{
return Response.ok("Hello world").build();
}
}

在ServerMainModule的setup()的方法中最后一行注册CatalogResource:

// Catalogs
jaxrsBinder(binder).bind(CatalogResource.class);

启动server,访问http://localhost:8080/v1/catalog/test,如果出现Hello World的话,那么后面的步骤才行得通。

2、新增一个Add Connector的RESTful接口

新建CatalogInfo用于接收参数:

package com.facebook.presto.server;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import java.util.Map; import static java.util.Objects.requireNonNull; /**
* @author Gin
* @since 2018/8/17.
*/
public class CatalogInfo
{ private final String catalogName; private final String connectorName; private final Map<String, String> properties; @JsonCreator
public CatalogInfo(
@JsonProperty("catalogName") String catalogName,
@JsonProperty("connectorName") String connectorName,
@JsonProperty("properties")Map<String, String> properties)
{
this.catalogName = requireNonNull(catalogName, "catalogName is null");
this.connectorName = requireNonNull(connectorName, "connectorName is null");
this.properties = requireNonNull(properties, "properties is null");
} @JsonProperty
public String getCatalogName() {
return catalogName;
} @JsonProperty
public String getConnectorName() {
return connectorName;
} @JsonProperty
public Map<String, String> getProperties() {
return properties;
}
}

在CatalogResource中增加对应的接口,用到的服务用注入方法声明。

/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.server; import com.facebook.presto.connector.ConnectorId;
import com.facebook.presto.connector.ConnectorManager;
import com.facebook.presto.metadata.InternalNodeManager;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import io.airlift.discovery.client.Announcer;
import io.airlift.discovery.client.ServiceAnnouncement; import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set; import static com.google.common.base.Strings.nullToEmpty;
import static io.airlift.discovery.client.ServiceAnnouncement.serviceAnnouncement;
import static java.util.Objects.requireNonNull; /**
* @author Gin
* @since 2018/8/15.
*/
@Path("/v1/catalog")
public class CatalogResource
{
private final ConnectorManager connectorManager;
private final Announcer announcer; @Inject
public CatalogResource(
ConnectorManager connectorManager,
Announcer announcer)
{
this.connectorManager = requireNonNull(connectorManager, "connectorManager is null");
this.announcer = requireNonNull(announcer, "announcer is null");
} @GET
@Path("test")
public Response test()
{
return Response.ok("Hello world").build();
} @PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createCatalog(CatalogInfo catalogInfo)
{
requireNonNull(catalogInfo, "catalogInfo is null"); ConnectorId connectorId = connectorManager.createConnection(
catalogInfo.getCatalogName(),
catalogInfo.getConnectorName(),
catalogInfo.getProperties()); updateConnectorIdAnnouncement(announcer, connectorId);
return Response.status(Response.Status.OK).build();
} private static void updateConnectorIdAnnouncement(Announcer announcer, ConnectorId connectorId)
{
//
// This code was copied from PrestoServer, and is a hack that should be removed when the connectorId property is removed
// // get existing announcement
ServiceAnnouncement announcement = getPrestoAnnouncement(announcer.getServiceAnnouncements()); // update connectorIds property
Map<String, String> properties = new LinkedHashMap<>(announcement.getProperties());
String property = nullToEmpty(properties.get("connectorIds"));
Set<String> connectorIds = new LinkedHashSet<>(Splitter.on(',').trimResults().omitEmptyStrings().splitToList(property));
connectorIds.add(connectorId.toString());
properties.put("connectorIds", Joiner.on(',').join(connectorIds)); // update announcement
announcer.removeServiceAnnouncement(announcement.getId());
announcer.addServiceAnnouncement(serviceAnnouncement(announcement.getType()).addProperties(properties).build());
announcer.forceAnnounce();
} private static ServiceAnnouncement getPrestoAnnouncement(Set<ServiceAnnouncement> announcements)
{
for (ServiceAnnouncement announcement : announcements) {
if (announcement.getType().equals("presto")) {
return announcement;
}
}
throw new RuntimeException("Presto announcement not found: " + announcements);
}
}

3、测试RESTful接口

这步需要安装需要的插件,检查插件是否安装。使用postman类似的东西,发送application/json的PUT请求到http://localhost:8080/v1/catalog/,body为

{
"catalogName": "test",
"connectorName": "mysql",
"properties": {
"connection-url":"jdbc:mysql://localhost:3306",
"connection-user":"root",
"connection-password":"root"
}
}

我们可以看到控制台,重新输出了connector的信息:

2018-08-19T14:09:03.502+0800	INFO	main	com.facebook.presto.server.PrestoServer	======== SERVER STARTED ========
2018-08-19T14:09:23.496+0800 INFO http-worker-133 Bootstrap PROPERTY DEFAULT RUNTIME DESCRIPTION
2018-08-19T14:09:23.496+0800 INFO http-worker-133 Bootstrap connection-password [REDACTED] [REDACTED]
2018-08-19T14:09:23.496+0800 INFO http-worker-133 Bootstrap connection-url null jdbc:mysql://localhost:3306
2018-08-19T14:09:23.496+0800 INFO http-worker-133 Bootstrap connection-user null root
2018-08-19T14:09:23.496+0800 INFO http-worker-133 Bootstrap allow-drop-table false false Allow connector to drop tables
2018-08-19T14:09:23.496+0800 INFO http-worker-133 Bootstrap mysql.auto-reconnect true true
2018-08-19T14:09:23.496+0800 INFO http-worker-133 Bootstrap mysql.connection-timeout 10.00s 10.00s
2018-08-19T14:09:23.496+0800 INFO http-worker-133 Bootstrap mysql.max-reconnects 3 3
2018-08-19T14:09:23.876+0800 INFO http-worker-133 io.airlift.bootstrap.LifeCycleManager Life cycle starting...
2018-08-19T14:09:23.877+0800 INFO http-worker-133 io.airlift.bootstrap.LifeCycleManager Life cycle startup complete. System ready.

接下来要怎么利用,要看大家的脑洞了:)?

参考文献:

Presto技术内幕

Presto通过RESTful接口新增Connector的更多相关文章

  1. [转]简单识别 RESTful 接口

         本文描述了识别一个接口是否真的是 RESTful 接口的基本方法.符合 REST 架构风格的接口,称为 RESTful 接口.本文不打算从架构风格的推导方面描述,而是从 HTTP 标准的方面 ...

  2. 三种方法实现调用Restful接口

    1.基本介绍 Restful接口的调用,前端一般使用ajax调用,后端可以使用的方法比较多, 本次介绍三种: 1.HttpURLConnection实现 2.HttpClient实现 3.Spring ...

  3. 三种方法实现java调用Restful接口

    1,基本介绍 Restful接口的调用,前端一般使用ajax调用,后端可以使用的方法比较多, 本次介绍三种: 1.HttpURLConnection实现 2.HttpClient实现 3.Spring ...

  4. 【快学springboot】2.Restful简介,SpringBoot构建Restful接口

    Restful简介 Restful一种软件架构风格.设计风格,而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服务器交互类的软件.基于这个风格设计的软件可以更简洁,更有层次,更易于实现 ...

  5. SpringBoot第十一集:整合Swagger3.0与RESTful接口整合返回值(2020最新最易懂)

    SpringBoot第十一集:整合Swagger3.0与RESTful接口整合返回值(2020最新最易懂) 一,整合Swagger3.0 随着Spring Boot.Spring Cloud等微服务的 ...

  6. Spring Cloud实战: 基于Spring Cloud Gateway + vue-element-admin 实现的RBAC权限管理系统,实现网关对RESTful接口方法权限和自定义Vue指令对按钮权限的细粒度控制

    一. 前言 信我的哈,明天过年. 这应该是农历年前的关于开源项目 的最后一篇文章了. 有来商城 是基于 Spring Cloud OAuth2 + Spring Cloud Gateway + JWT ...

  7. Spring Cloud实战 | 第十一篇:Spring Cloud Gateway 网关实现对RESTful接口权限控制和按钮权限控制

    一. 前言 hi,大家好,这应该是农历年前的关于开源项目 的最后一篇文章了. 有来商城 是基于 Spring Cloud OAuth2 + Spring Cloud Gateway + JWT实现的统 ...

  8. 02 RESTFul接口和HTTP的幂等性分析

    RESTFul接口和HTTP的幂等性分析 REST全称是Representational State Transfer,中文为表述性状态转移,REST指的是一组架构约束条件和原则 RESTful表述的 ...

  9. RESTful 接口调试分享利器 restc

    这个工具来自于https://elemefe.github.io/restc/  这里对Abp进行了一次封装 1.在项目中添加nuget包 Abp.Web.Api.Restc 2.在项目Abp模块的D ...

随机推荐

  1. 基于MongoDB2.6版本配置MongoDB主从复制集群架构

    1:集群环境说明:mongodb1:192.168.43.10.mongodb2:192.168.43.11.mongodb3:192.168.43.12.且基于主机名相互通信/etc/hosts文件 ...

  2. Caffe使用step by step:faster-rcnn目标检测matlab代码

    faster-rcnn是MSRA在物体检测最新的研究成果,该研究成果基于RCNN,fast rcnn以及SPPnet,对之前目标检测方法进行改进,faster-rcnn项目地址.首先,faster r ...

  3. BZOJ3270 博物馆(高斯消元+概率期望)

    将两个人各自所在点视为状态,新建一个图.到达某个终点的概率等于其期望次数.那么高斯消元即可. #include<iostream> #include<cstdio> #incl ...

  4. 文件同步工具 lsyncd2.1.6 安装使用问题

    项目有文件实时同步备份的需求,做了一下调查,比较好的解决方法是使用lsyncd工具.这里主要记录一下遇到的问题及解决方法. lsyncd 的相关介绍和对比可见: lsyncd实时同步搭建指南——取代r ...

  5. Navicat使用教程:获取MySQL中的高级行数(第2部分)

    Navicat Premium是一个可连接多种数据库的管理工具,它可以让你以单一程序同时连接到MySQL.Oracle及PostgreSQL数据库,让管理不同类型的数据库更加的方便. 在上篇文章中,我 ...

  6. 51nod 1208 窗上的星星 | 线段树 扫描线

    51nod 1208 Stars In Your Window 题面 整点上有N颗星星,每颗星星有一个亮度.用一个平行于x轴和y轴,宽为W高为H的方框去套星星.套住的所有星星的亮度之和为S(包括边框上 ...

  7. BZOJ 2878 【NOI2012】 迷失游乐园

    题目链接:迷失游乐园 这道题也没有传说中的那么难写吗→_→ 似乎有篇博客讲得特详细……附上链接:戳这里 如果这道题不是基环树,而就是一棵树的话,我们来考虑改怎么做.因为树上的路径只有向上.向下两种走法 ...

  8. 「CodePlus 2017 11 月赛」大吉大利,晚上吃鸡!(dij+bitset)

    从S出发跑dij,从T出发跑dij,顺便最短路计数. 令$F(x)$为$S$到$T$最短路经过$x$的方案数,显然这个是可以用$S$到$x$的方案数乘$T$到$x$的方案数来得到. 然后第一个条件就变 ...

  9. 2019PKU\THU WC题解

    PKU: 机试: d1t1: 考虑拓扑序的合法性,每个点的入边必须先加入.f[S]表示先出来的是S集合的点,对应边的方案数.加入x的时候,把入边方向确定,出边自然后面会确定的 2^n*n d1t2: ...

  10. 使用 xhprof 进行 php 的性能分析

    基于本机环境(php7,macos) 1.xhprof 扩展 php7 下安装 xhprof 扩展: git clone https://github.com/longxinH/xhprof cd x ...