com.facebook.presto.spi.ConnectorSession Java Examples

The following examples show how to use com.facebook.presto.spi.ConnectorSession. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KinesisMetadata.java    From presto-kinesis with Apache License 2.0 6 votes vote down vote up
@Override
public Map<SchemaTableName, List<ColumnMetadata>> listTableColumns(ConnectorSession session, SchemaTablePrefix prefix)
{
    checkNotNull(prefix, "prefix is null");
    log.debug("Called listTableColumns on %s.%s", prefix.getSchemaName(), prefix.getTableName());

    ImmutableMap.Builder<SchemaTableName, List<ColumnMetadata>> columns = ImmutableMap.builder();

    // NOTE: prefix.getTableName or prefix.getSchemaName can be null
    List<SchemaTableName> tableNames;
    if (prefix.getSchemaName() != null && prefix.getTableName() != null) {
        tableNames = ImmutableList.of(new SchemaTableName(prefix.getSchemaName(), prefix.getTableName()));
    }
    else {
        tableNames = listTables(session, (String) null);
    }

    for (SchemaTableName tableName : tableNames) {
        ConnectorTableMetadata tableMetadata = getTableMetadata(tableName);
        if (tableMetadata != null) {
            columns.put(tableName, tableMetadata.getColumns());
        }
    }
    return columns.build();
}
 
Example #2
Source File: KuduMetadata.java    From presto-kudu with Apache License 2.0 6 votes vote down vote up
@Override
public ConnectorInsertTableHandle beginInsert(ConnectorSession session, ConnectorTableHandle connectorTableHandle) {
    KuduTableHandle tableHandle = fromConnectorTableHandle(session, connectorTableHandle);

    KuduTable table = tableHandle.getTable(clientSession);
    Schema schema = table.getSchema();

    List<ColumnSchema> columns = schema.getColumns();
    List<String> columnNames = columns.stream().map(ColumnSchema::getName).collect(toImmutableList());
    List<Type> columnTypes = columns.stream()
            .map(TypeHelper::fromKuduColumn).collect(toImmutableList());

    return new KuduInsertTableHandle(
            connectorId,
            tableHandle.getSchemaTableName(),
            columnNames,
            columnTypes,
            table);
}
 
Example #3
Source File: ElasticsearchMetadata.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
@Override
public ConnectorInsertTableHandle beginInsert(ConnectorSession session, ConnectorTableHandle tableHandle)
{
    checkNoRollback();
    ElasticsearchTableHandle handle = (ElasticsearchTableHandle) tableHandle;
    setRollback(() -> {
        //--------插入操作无法回退
        // Rollbacks for inserts are off the table when it comes to data in Hbase.
        // When a batch of Mutations fails to be inserted, the general strategy
        // is to run the insert operation again until it is successful
        // Any mutations that were successfully written will be overwritten
        // with the same values, so that isn't a problem.
        throw new PrestoException(NOT_SUPPORTED, format("Unable to rollback insert for table %s.%s. Some rows may have been written. Please run your insert again.", handle.getSchemaName(), handle.getTableName()));
    });
    return handle;
}
 
Example #4
Source File: ElasticsearchMetadata.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
private List<SchemaTableName> listTables(ConnectorSession session, SchemaTablePrefix prefix)
{
    // List all tables if schema or table is null
    if (prefix.getSchemaName() == null || prefix.getTableName() == null) {
        return listTables(session, prefix.getSchemaName());
    }

    // Make sure requested table exists, returning the single table of it does
    SchemaTableName table = new SchemaTableName(prefix.getSchemaName(), prefix.getTableName());
    if (getTableHandle(session, table) != null) {
        return ImmutableList.of(table);
    }

    // Else, return empty list
    return ImmutableList.of();
}
 
Example #5
Source File: TestSessionVariables.java    From presto-kinesis with Apache License 2.0 6 votes vote down vote up
@Test
public void testTimestampAccess()
{
    // Test timestamp functions:
    TimeZoneKey tzKey = session.getTimeZoneKey();
    assertEquals(tzKey.getId(), "America/Los_Angeles");

    // Test a known result
    long result = SessionVariables.getTimestampAsLong("2016-07-10 12:03:56.124", session);
    assertTrue(result != 0);
    assertEquals(result, 1468177436124L);

    ConnectorSession altSession = makeSessionWithTimeZone("America/New_York");
    long result2 = SessionVariables.getTimestampAsLong("2016-07-10 12:03:56.124", altSession);
    assertTrue(result2 != 0);
    assertEquals(result2, 1468166636124L);
}
 
Example #6
Source File: HbaseMetadata.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
@Override
public List<SchemaTableName> listTables(ConnectorSession session, String schemaNameOrNull)
{
    Set<String> schemaNames;
    if (schemaNameOrNull != null) {
        schemaNames = ImmutableSet.of(schemaNameOrNull);
    }
    else {
        schemaNames = client.getSchemaNames();
    }
    ImmutableList.Builder<SchemaTableName> builder = ImmutableList.builder();
    for (String schemaName : schemaNames) {
        for (String tableName : client.getTableNames(schemaName)) {
            builder.add(new SchemaTableName(schemaName, tableName));
        }
    }
    return builder.build();
}
 
Example #7
Source File: HbaseMetadata.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
@Override
public ConnectorOutputTableHandle beginCreateTable(ConnectorSession session, ConnectorTableMetadata tableMetadata, Optional<ConnectorNewTableLayout> layout)
{
    checkNoRollback();   //采用乐观锁 设计

    SchemaTableName tableName = tableMetadata.getTable();
    HbaseTable table = client.createTable(tableMetadata);

    HbaseTableHandle handle = new HbaseTableHandle(
            connectorId,
            tableName.getSchemaName(),
            tableName.getTableName(),
            table.getRowId(),
            table.isExternal(),
            table.getScanAuthorizations());

    setRollback(() -> client.dropTable(table));  //回滚操作

    return handle;
}
 
Example #8
Source File: HbaseMetadata.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
private List<SchemaTableName> listTables(ConnectorSession session, SchemaTablePrefix prefix)
{
    // List all tables if schema or table is null
    if (prefix.getSchemaName() == null || prefix.getTableName() == null) {
        return listTables(session, prefix.getSchemaName());
    }

    // Make sure requested table exists, returning the single table of it does
    SchemaTableName table = new SchemaTableName(prefix.getSchemaName(), prefix.getTableName());
    if (getTableHandle(session, table) != null) {
        return ImmutableList.of(table);
    }

    // Else, return empty list
    return ImmutableList.of();
}
 
Example #9
Source File: ParaflowPageSourceProvider.java    From paraflow with Apache License 2.0 6 votes vote down vote up
@Override
public ConnectorPageSource createPageSource(ConnectorTransactionHandle transactionHandle, ConnectorSession session,
                                            ConnectorSplit split, List<ColumnHandle> columns)
{
    List<ParaflowColumnHandle> hdfsColumns = columns.stream()
            .map(col -> (ParaflowColumnHandle) col)
            .collect(Collectors.toList());
    ParaflowSplit paraflowSplit = checkType(split, ParaflowSplit.class, "hdfs split");
    Path path = new Path(paraflowSplit.getPath());

    Optional<ConnectorPageSource> pageSource = createParaflowPageSource(
            path,
            paraflowSplit.getStart(),
            paraflowSplit.getLen(),
            hdfsColumns);
    if (pageSource.isPresent()) {
        return pageSource.get();
    }
    throw new RuntimeException("Could not find a file reader for split " + paraflowSplit);
}
 
Example #10
Source File: ParaflowMetadata.java    From paraflow with Apache License 2.0 6 votes vote down vote up
@Override
public List<SchemaTableName> listTables(ConnectorSession session, String schemaNameOrNull)
{
    List<String> schemaNames;
    if (schemaNameOrNull != null) {
        schemaNames = ImmutableList.of(schemaNameOrNull);
    }
    else {
        schemaNames = paraflowMetaDataReader.getAllDatabases();
    }

    ImmutableList.Builder<SchemaTableName> builder = ImmutableList.builder();
    for (String schemaName : schemaNames) {
        for (String tableName : paraflowMetaDataReader.getTableNames(schemaName)) {
            builder.add(new SchemaTableName(schemaName, tableName));
        }
    }
    return builder.build();
}
 
Example #11
Source File: EthereumMetadata.java    From presto-ethereum with Apache License 2.0 5 votes vote down vote up
@Override
public EthereumTableHandle getTableHandle(ConnectorSession session, SchemaTableName schemaTableName) {
    if (EthereumTable.BLOCK.getName().equals(schemaTableName.getTableName())) {
        return new EthereumTableHandle(connectorId, DEFAULT_SCHEMA, EthereumTable.BLOCK.getName());
    } else if (EthereumTable.TRANSACTION.getName().equals(schemaTableName.getTableName())) {
        return new EthereumTableHandle(connectorId, DEFAULT_SCHEMA, EthereumTable.TRANSACTION.getName());
    } else if (EthereumTable.ERC20.getName().equals(schemaTableName.getTableName())) {
        return new EthereumTableHandle(connectorId, DEFAULT_SCHEMA, EthereumTable.ERC20.getName());
    } else {
        throw new IllegalArgumentException("Unknown Table Name " + schemaTableName.getTableName());
    }
}
 
Example #12
Source File: KinesisMetadata.java    From presto-kinesis with Apache License 2.0 5 votes vote down vote up
@Override
public List<ConnectorTableLayoutResult> getTableLayouts(ConnectorSession connectorSession, ConnectorTableHandle table,
                                                        Constraint<ColumnHandle> constraint, Optional<Set<ColumnHandle>> optional)
{
    KinesisTableHandle tblHandle = handleResolver.convertTableHandle(table);
    ConnectorTableLayout layout = new ConnectorTableLayout(new KinesisTableLayoutHandle(connectorId, tblHandle));
    return ImmutableList.of(new ConnectorTableLayoutResult(layout, constraint.getSummary()));
 }
 
Example #13
Source File: EthereumMetadata.java    From presto-ethereum with Apache License 2.0 5 votes vote down vote up
@Override
public List<SchemaTableName> listTables(ConnectorSession session, String schemaNameOrNull)
{
    return ImmutableList.of(new SchemaTableName(DEFAULT_SCHEMA, EthereumTable.BLOCK.getName()),
            new SchemaTableName(DEFAULT_SCHEMA, EthereumTable.TRANSACTION.getName()),
            new SchemaTableName(DEFAULT_SCHEMA, EthereumTable.ERC20.getName()));
}
 
Example #14
Source File: ElasticsearchMetadata.java    From presto-connectors with Apache License 2.0 5 votes vote down vote up
@Override
public List<ConnectorTableLayoutResult> getTableLayouts(ConnectorSession session, ConnectorTableHandle table, Constraint<ColumnHandle> constraint, Optional<Set<ColumnHandle>> desiredColumns)
{
    ElasticsearchTableHandle tableHandle = (ElasticsearchTableHandle) table;
    ConnectorTableLayout layout = new ConnectorTableLayout(new ElasticsearchTableLayoutHandle(tableHandle, constraint.getSummary()));
    return ImmutableList.of(new ConnectorTableLayoutResult(layout, constraint.getSummary()));
}
 
Example #15
Source File: KinesisRecordSetProvider.java    From presto-kinesis with Apache License 2.0 5 votes vote down vote up
@Override
public RecordSet getRecordSet(ConnectorTransactionHandle transactionHandle, ConnectorSession session,
                              ConnectorSplit split, List<? extends ColumnHandle> columns)
{
    KinesisSplit kinesisSplit = handleResolver.convertSplit(split);

    ImmutableList.Builder<KinesisColumnHandle> handleBuilder = ImmutableList.builder();
    ImmutableMap.Builder<KinesisColumnHandle, KinesisFieldDecoder<?>> messageFieldDecoderBuilder = ImmutableMap.builder();

    KinesisRowDecoder messageDecoder = registry.getRowDecoder(kinesisSplit.getMessageDataFormat());

    for (ColumnHandle handle : columns) {
        KinesisColumnHandle columnHandle = handleResolver.convertColumnHandle(handle);
        handleBuilder.add(columnHandle);

        if (!columnHandle.isInternal()) {
            KinesisFieldDecoder<?> fieldDecoder = registry.getFieldDecoder(kinesisSplit.getMessageDataFormat(),
                    columnHandle.getType().getJavaType(),
                    columnHandle.getDataFormat());

            messageFieldDecoderBuilder.put(columnHandle, fieldDecoder);
        }
    }

    ImmutableList<KinesisColumnHandle> handles = handleBuilder.build();
    ImmutableMap<KinesisColumnHandle, KinesisFieldDecoder<?>> messageFieldDecoders = messageFieldDecoderBuilder.build();

    return new KinesisRecordSet(kinesisSplit, session, clientManager, handles, messageDecoder, messageFieldDecoders, kinesisConnectorConfig);
}
 
Example #16
Source File: KuduMetadata.java    From presto-kudu with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<ConnectorOutputMetadata> finishCreateTable(ConnectorSession session,
        ConnectorOutputTableHandle tableHandle,
        Collection<Slice> fragments,
        Collection<ComputedStatistics> computedStatistics) {
    return Optional.empty();
}
 
Example #17
Source File: HbaseMetadata.java    From presto-connectors with Apache License 2.0 5 votes vote down vote up
@Override
public ConnectorTableMetadata getTableMetadata(ConnectorSession session, ConnectorTableHandle table)
{
    HbaseTableHandle handle = (HbaseTableHandle) table;
    checkArgument(handle.getConnectorId().equals(connectorId), "table is not for this connector");
    SchemaTableName tableName = new SchemaTableName(handle.getSchema(), handle.getTable());
    ConnectorTableMetadata metadata = getTableMetadata(tableName);
    if (metadata == null) {
        throw new TableNotFoundException(tableName);
    }
    return metadata;
}
 
Example #18
Source File: KubeRecordSetProvider.java    From kubesql with Apache License 2.0 5 votes vote down vote up
@Override
public RecordSet getRecordSet(ConnectorTransactionHandle transactionHandle, ConnectorSession session, ConnectorSplit split, List<? extends ColumnHandle> columns)
{
    requireNonNull(split, "split is null");
    KubeSplit kubeSplit = (KubeSplit) split;

    ImmutableList.Builder<KubeColumnHandle> handles = ImmutableList.builder();
    for (ColumnHandle handle : columns) {
        handles.add((KubeColumnHandle) handle);
    }

    return new KubeRecordSet(kubeTables, kubeSplit, handles.build());
}
 
Example #19
Source File: HbaseMetadata.java    From presto-connectors with Apache License 2.0 5 votes vote down vote up
@Override
public List<ConnectorTableLayoutResult> getTableLayouts(ConnectorSession session, ConnectorTableHandle table, Constraint<ColumnHandle> constraint, Optional<Set<ColumnHandle>> desiredColumns)
{
    HbaseTableHandle tableHandle = (HbaseTableHandle) table;
    ConnectorTableLayout layout = new ConnectorTableLayout(new HbaseTableLayoutHandle(tableHandle, constraint.getSummary()));
    return ImmutableList.of(new ConnectorTableLayoutResult(layout, constraint.getSummary()));
}
 
Example #20
Source File: ParaflowMetadata.java    From paraflow with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, ColumnHandle> getColumnHandles(ConnectorSession session, ConnectorTableHandle tableHandle)
{
    ParaflowTableHandle table = checkType(tableHandle, ParaflowTableHandle.class, "table");
    List<ParaflowColumnHandle> cols = paraflowMetaDataReader.getTableColumnHandle(connectorId, table.getSchemaName(), table.getTableName())
            .orElse(new ArrayList<>());
    Map<String, ColumnHandle> columnMap = new HashMap<>();
    for (ParaflowColumnHandle col : cols) {
        columnMap.putIfAbsent(col.getName(), col);
    }
    return columnMap;
}
 
Example #21
Source File: KinesisMetadata.java    From presto-kinesis with Apache License 2.0 5 votes vote down vote up
@Override
public KinesisTableHandle getTableHandle(ConnectorSession session, SchemaTableName schemaTableName)
{
    KinesisStreamDescription table = getDefinedTables().get(schemaTableName);
    if (table == null) {
        throw new TableNotFoundException(schemaTableName);
    }

    return new KinesisTableHandle(connectorId,
            schemaTableName.getSchemaName(),
            schemaTableName.getTableName(),
            table.getStreamName(),
            getDataFormat(table.getMessage()));
}
 
Example #22
Source File: ElasticsearchMetadata.java    From presto-connectors with Apache License 2.0 5 votes vote down vote up
@Override
public Map<SchemaTableName, List<ColumnMetadata>> listTableColumns(ConnectorSession session, SchemaTablePrefix prefix)
{
    requireNonNull(prefix, "prefix is null");
    ImmutableMap.Builder<SchemaTableName, List<ColumnMetadata>> columns = ImmutableMap.builder();
    for (SchemaTableName tableName : listTables(session, prefix)) {
        ConnectorTableMetadata tableMetadata = getTableMetadata(tableName);
        // table can disappear during listing operation
        if (tableMetadata != null) {
            columns.put(tableName, tableMetadata.getColumns());
        }
    }
    return columns.build();
}
 
Example #23
Source File: ParaflowMetadata.java    From paraflow with Apache License 2.0 5 votes vote down vote up
@Override
public ConnectorTableMetadata getTableMetadata(ConnectorSession session, ConnectorTableHandle table)
{
    ParaflowTableHandle paraflowTable = (ParaflowTableHandle) table;
    checkArgument(paraflowTable.getConnectorId().equals(connectorId), "tableHandle is not for this connector");
    SchemaTableName tableName = new SchemaTableName(paraflowTable.getSchemaName(), paraflowTable.getTableName());
    return getTableMetadata(tableName);
}
 
Example #24
Source File: KinesisMetadata.java    From presto-kinesis with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, ColumnHandle> getColumnHandles(ConnectorSession connectorSession, ConnectorTableHandle tableHandle)
{
    KinesisTableHandle kinesisTableHandle = handleResolver.convertTableHandle(tableHandle);

    KinesisStreamDescription kinesisStreamDescription = getDefinedTables().get(kinesisTableHandle.toSchemaTableName());
    if (kinesisStreamDescription == null) {
        throw new TableNotFoundException(kinesisTableHandle.toSchemaTableName());
    }

    ImmutableMap.Builder<String, ColumnHandle> columnHandles = ImmutableMap.builder();

    int index = 0;
    // Note: partition key and related fields are handled by internalFieldDescriptions below
    KinesisStreamFieldGroup message = kinesisStreamDescription.getMessage();
    if (message != null) {
        List<KinesisStreamFieldDescription> fields = message.getFields();
        if (fields != null) {
            for (KinesisStreamFieldDescription kinesisStreamFieldDescription : fields) {
                columnHandles.put(kinesisStreamFieldDescription.getName(), kinesisStreamFieldDescription.getColumnHandle(connectorId, index++));
            }
        }
    }

    for (KinesisInternalFieldDescription kinesisInternalFieldDescription : internalFieldDescriptions) {
        columnHandles.put(kinesisInternalFieldDescription.getName(), kinesisInternalFieldDescription.getColumnHandle(connectorId, index++, kinesisConnectorConfig.isHideInternalColumns()));
    }

    return columnHandles.build();
}
 
Example #25
Source File: KuduRecordSetProvider.java    From presto-kudu with Apache License 2.0 5 votes vote down vote up
@Override
public RecordSet getRecordSet(ConnectorTransactionHandle transactionHandle,
                              ConnectorSession session, ConnectorSplit split, List<? extends ColumnHandle> columns) {
    requireNonNull(split, "split is null");
    requireNonNull(columns, "columns is null");

    KuduSplit kuduSplit = checkType(split, KuduSplit.class, "split is not class KuduSplit");

    return new KuduRecordSet(clientSession, kuduSplit, columns);
}
 
Example #26
Source File: KuduMetadata.java    From presto-kudu with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<ConnectorOutputMetadata> finishInsert(ConnectorSession session,
        ConnectorInsertTableHandle insertHandle,
        Collection<Slice> fragments,
        Collection<ComputedStatistics> computedStatistics) {
    return Optional.empty();
}
 
Example #27
Source File: TestSessionVariables.java    From presto-kinesis with Apache License 2.0 5 votes vote down vote up
protected ConnectorSession makeSessionWithTimeZone(String tzId)
{
    return Session.builder(propManager)
            .setIdentity(new Identity("user", Optional.empty()))
            .setSource("source")
            .setCatalog("kinesis")
            .setSchema("default")
            .setTimeZoneKey(TimeZoneKey.getTimeZoneKey(tzId))
            .setLocale(ENGLISH)
            .setQueryId(new QueryId("dummy"))
            .build().toConnectorSession(new ConnectorId("kinesis"));
}
 
Example #28
Source File: HbaseMetadata.java    From presto-connectors with Apache License 2.0 5 votes vote down vote up
@Override
public void dropTable(ConnectorSession session, ConnectorTableHandle tableHandle)
{
    HbaseTableHandle handle = (HbaseTableHandle) tableHandle;
    HbaseTable table = client.getTable(handle.toSchemaTableName());
    if (table != null) {
        client.dropTable(table);
    }
}
 
Example #29
Source File: KuduPageSinkProvider.java    From presto-kudu with Apache License 2.0 5 votes vote down vote up
@Override
public ConnectorPageSink createPageSink(ConnectorTransactionHandle transactionHandle, ConnectorSession session, ConnectorOutputTableHandle outputTableHandle) {
    requireNonNull(outputTableHandle, "outputTableHandle is null");
    checkArgument(outputTableHandle instanceof KuduOutputTableHandle, "outputTableHandle is not an instance of KuduOutputTableHandle");
    KuduOutputTableHandle handle = (KuduOutputTableHandle) outputTableHandle;

    return new KuduPageSink(session, clientSession, handle, handle.isGenerateUUID());
}
 
Example #30
Source File: ElasticsearchMetadata.java    From presto-connectors with Apache License 2.0 5 votes vote down vote up
@Override
public ConnectorTableHandle getTableHandle(ConnectorSession session, SchemaTableName tableName)
{
    requireNonNull(tableName, "tableName is null");
    // Need to validate that SchemaTableName is a table
    if (!client.existsTable(tableName)) {
        return null;
    }
    return new ElasticsearchTableHandle(connectorId, tableName.getSchemaName(), tableName.getTableName());
}