Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lark sheets connector #17218

Merged
merged 1 commit into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
<module>presto-delta</module>
<module>presto-grpc-api</module>
<module>presto-grpc-testing-udf-server</module>
<module>presto-lark-sheets</module>
</modules>

<dependencyManagement>
Expand Down
1 change: 1 addition & 0 deletions presto-docs/src/main/sphinx/connector.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ from different data sources.
connector/kafka
connector/kafka-tutorial
connector/kudu
connector/larksheets
connector/localfile
connector/memory
connector/mongodb
Expand Down
106 changes: 106 additions & 0 deletions presto-docs/src/main/sphinx/connector/larksheets.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
=======================
Lark Sheets connector
=======================

The Lark Sheets connector allows reading `Lark Sheets <https://www.larksuite.com/en_us/>`_ spreadsheets in Presto.

Lark App
--------

The connector requires a Lark app in order to access the Lark Sheets API.

1. Create a Lark app by clicking the **Create Custom App** button at
`Lark Open Platform <https://open.larksuite.com/app>`_ (for global users) or
`Feishu Open Platform <https://open.feishu.cn/app>`_ (for Chinese users).
Choose ``Custom App`` when creating the app. Get and remember ``app id`` and ``app secret`` from
**Credentials and Basic Info** page.
2. Grant ``View, comment, and export Sheets`` permission to the app at **Permission & Scopes** page.
And publish a new release for the app at **App Release** page to make the permissions take effect.

Configuration
-------------

Create ``etc/catalog/larksheets.properties``
to mount the Lark Sheets connector as the ``larksheets`` catalog,
replacing the properties as appropriate:

.. code-block:: text

connector.name=lark-sheets
app-domain=FEISHU
app-id=example_app_id
app-secret-file=/path/to/app-secret.json

Create ``app-secret.json`` as the example below:

.. code-block:: json

{"app-secret": "abcdefghijklmnopqrstuvwxyzabcdef"}

The following configuration properties are available:

=================================== ========================================================================
Property Name Description
=================================== ========================================================================
``app-domain`` Use ``LARK`` if created at `Lark Open Platform`, or ``FEISHU`` otherwise
``app-id`` Your app id, available at `Lark Open Platform` or `Feishu Open Platform`
``app-secret-file`` Path to the JSON file that contains your app secret
=================================== ========================================================================

Query Model
------------------

In the connector, a Lark spreadsheet is mapped to a Presto schema, and each sheet
inside the spreadsheet is mapped to a Presto table.

Create a spreadsheet-schema mapping by executing a query like:

.. code-block:: sql

CREATE SCHEMA my_ss WITH (TOKEN = 'shtcnBf5pg4BNSkwV2Ku5xwW9Pf')

The ``token`` property is taken from the last path segment of the spreadsheet url, using the pattern
``https://{lark_site_domain}/sheets/{token}?sheet={sheet_id}``. For example,
``shtcnBf5pg4BNSkwV2Ku5xwW9Pf`` is the token of the spreadsheet at
``https://test-ch80md45anra.feishu.cn/sheets/shtcnBf5pg4BNSkwV2Ku5xwW9Pf?sheet=MT1p4I``
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is a real spreadsheet. Will the link expire?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it will not expire.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make it more readable, I might use a pattern string rather than a real link. e.g. https://{account_id}.feishu.ch/sheet/{token}/sheet={sheet_id}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the pattern string. And give an example as well.


After that, all sheets (tabs) of the spreadsheet are automatically mapped to tables in the schema.

.. code-block:: sql

SHOW TABLES FROM my_ss

For each table, its name is taken from the sheet title; its columns are inferred from the head row
(the first row of the sheet). Sheets metadata could be listed through the query as:

.. code-block:: sql

SELECT * FROM my_ss."$sheets"

In a query statement, a table can be specified in such ways:

.. code-block:: sql

-- by sheet title
SELECT * FROM my_ss."number_text"

-- by sheet index
SELECT * FROM my_ss."$0"

-- by sheet id
SELECT * FROM my_ss."@MT1p4I"

As for column infer, columns that have blank value in the head row are ignored.
A sheet with duplicate column names can not be queried.

Security Model
--------------

Only the spreadsheet that enables **Link Sharing** can be queried.

The schema is visible and manipulative only to its creator by default.
To make the schema visible to others, create it as:

.. code-block:: sql

CREATE SCHEMA my_ss WITH (TOKEN = 'shtcnBf5pg4BNSkwV2Ku5xwW9Pf', PUBLIC = true)
153 changes: 153 additions & 0 deletions presto-lark-sheets/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-root</artifactId>
<version>0.272-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>presto-lark-sheets</artifactId>
<description>Presto - Lark Sheets Connector</description>
<packaging>presto-plugin</packaging>

<properties>
<air.main.basedir>${project.parent.basedir}</air.main.basedir>
</properties>

<dependencies>
<dependency>
<groupId>com.facebook.airlift</groupId>
<artifactId>bootstrap</artifactId>
</dependency>

<dependency>
<groupId>com.facebook.airlift</groupId>
<artifactId>configuration</artifactId>
</dependency>

<dependency>
<groupId>com.facebook.airlift</groupId>
<artifactId>json</artifactId>
</dependency>

<dependency>
<groupId>com.facebook.airlift</groupId>
<artifactId>log</artifactId>
</dependency>

<dependency>
<groupId>com.facebook.airlift</groupId>
<artifactId>log-manager</artifactId>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>

<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</dependency>

<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
</dependency>

<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.larksuite.oapi</groupId>
<artifactId>larksuite-oapi</artifactId>
<version>1.0.18-rc4</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- Presto SPI -->
<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-common</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-spi</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>slice</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>units</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<scope>provided</scope>
</dependency>

<!-- for testing -->
<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-main</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-tests</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.facebook.airlift</groupId>
<artifactId>http-server</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.facebook.airlift</groupId>
<artifactId>node</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.facebook.airlift</groupId>
<artifactId>testing</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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.lark.sheets;

import com.facebook.presto.common.type.Type;
import com.facebook.presto.spi.ColumnHandle;
import com.facebook.presto.spi.ColumnMetadata;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Objects;

import static java.util.Objects.requireNonNull;

public class LarkSheetsColumnHandle
implements ColumnHandle
{
private final String name;
private final Type type;
private final int index;

@JsonCreator
public LarkSheetsColumnHandle(@JsonProperty("name") String name, @JsonProperty("type") Type type, @JsonProperty("index") int index)
{
this.name = requireNonNull(name, "name is null");
this.type = requireNonNull(type, "type is null");
this.index = index;
}

@JsonProperty
public String getName()
{
return name;
}

@JsonProperty
public Type getType()
{
return type;
}

@JsonProperty
public int getIndex()
{
return index;
}

public ColumnMetadata toColumnMetadata()
{
return new ColumnMetadata(name, type);
}

@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
LarkSheetsColumnHandle that = (LarkSheetsColumnHandle) o;
return index == that.index && Objects.equals(name, that.name) && Objects.equals(type, that.type);
}

@Override
public int hashCode()
{
return Objects.hash(name, type, index);
}

@Override
public String toString()
{
return name + ":" + type + ":" + index;
}
}
Loading