com.facebook.presto.spi.PrestoException Java Examples

The following examples show how to use com.facebook.presto.spi.PrestoException. 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: Elasticsearch5Module.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
@Override
public Client get()
{
    try {
        Settings settings = Settings.builder().put("cluster.name", clusterName)
                .put("client.transport.sniff", true).build();

        TransportClient client = new PreBuiltTransportClient(settings);
        for (String ip : hosts.split(",")) {
            client.addTransportAddress(
                    new InetSocketTransportAddress(InetAddress.getByName(ip.split(":")[0]),
                            Integer.parseInt(ip.split(":")[1])));
        }
        return client;
    }
    catch (IOException e) {
        throw new PrestoException(UNEXPECTED_ES_ERROR, "Failed to get connection to Elasticsearch", e);
    }
}
 
Example #2
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 #3
Source File: ElasticsearchPageSource.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
private void writeSlice(BlockBuilder output, Type type, Object value)
{
    String base = type.getTypeSignature().getBase();
    if (base.equals(StandardTypes.VARCHAR)) {
        type.writeSlice(output, utf8Slice(toVarcharValue(value)));
    }
    else if (type.equals(VARBINARY)) {
        if (value instanceof byte[]) {
            type.writeSlice(output, wrappedBuffer(((byte[]) value)));
        }
        else {
            output.appendNull();
        }
    }
    else {
        throw new PrestoException(GENERIC_INTERNAL_ERROR, "Unhandled type for Slice: " + type.getTypeSignature());
    }
}
 
Example #4
Source File: Elasticsearch2Client.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
@Override
public void insertMany(List<Document> docs)
{
    final BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
    for (Document doc : docs) {
        bulkRequestBuilder.add(new IndexRequest()
                .index(doc.getIndex())
                .type(doc.getType())
                .id(doc.getId())
                .source(doc.getSource()));
    }
    BulkResponse response = bulkRequestBuilder.get();
    if (response.hasFailures()) {
        throw new PrestoException(IO_ERROR, response.buildFailureMessage());
    }
}
 
Example #5
Source File: RawKinesisFieldDecoder.java    From presto-kinesis with Apache License 2.0 6 votes vote down vote up
@Override
public boolean getBoolean()
{
    if (isNull()) {
        return false;
    }
    switch (fieldType) {
        case BYTE:
            return value.get() != 0;
        case SHORT:
            return value.getShort() != 0;
        case INT:
            return value.getInt() != 0;
        case LONG:
            return value.getLong() != 0;
        default:
            throw new PrestoException(KINESIS_CONVERSION_NOT_SUPPORTED, format("conversion %s to boolean not supported", fieldType));
    }
}
 
Example #6
Source File: Elasticsearch2Module.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
@Override
public Client get()
{
    try {
        Settings settings = Settings.builder().put("cluster.name", clusterName)
                .put("client.transport.sniff", true).build();
        TransportClient client = TransportClient.builder().settings(settings).build();
        for (String ip : hosts.split(",")) {
            client.addTransportAddress(
                    new InetSocketTransportAddress(InetAddress.getByName(ip.split(":")[0]),
                            Integer.parseInt(ip.split(":")[1])));
        }
        LOG.info("Connection to instance %s at %s established, user %s");
        return client;
    }
    catch (IOException e) {
        throw new PrestoException(UNEXPECTED_ES_ERROR, "Failed to get connection to Elasticsearch", e);
    }
}
 
Example #7
Source File: Elasticsearch6Client.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
@Override
public void insertMany(List<Document> docs)
{
    final BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
    for (Document doc : docs) {
        bulkRequestBuilder.add(new IndexRequest()
                .index(doc.getIndex())
                .type(doc.getType())
                .id(doc.getId())
                .source(doc.getSource()));
    }
    BulkResponse response = bulkRequestBuilder.execute().actionGet();
    if (response.hasFailures()) {
        throw new PrestoException(IO_ERROR, response.buildFailureMessage());
    }
}
 
Example #8
Source File: Elasticsearch6Client.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
private static void addEsQueryFilter(Map<String, Object> mergeDslMap, String dsl)
{
    //-------get query-----
    Map<String, Object> queryDsl;
    try {
        final Map<String, Object> dslMap = MAPPER.readValue(dsl, Map.class);
        queryDsl = (Map<String, Object>) dslMap.getOrDefault("query", dslMap);
    }
    catch (IOException e) {
        throw new PrestoException(ES_DSL_ERROR, e);
    }

    Map<String, Object> query = (Map<String, Object>) mergeDslMap.computeIfAbsent("query", k -> new HashMap<>());
    Map bool = (Map) query.computeIfAbsent("bool", k -> new HashMap<>());
    List must = (List) bool.computeIfAbsent("must", k -> new ArrayList<>());
    //--merge dsl--
    must.add(queryDsl);
}
 
Example #9
Source File: Elasticsearch6Module.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
@Override
public Client get()
{
    try {
        Settings settings = Settings.builder().put("cluster.name", clusterName)
                .put("client.transport.sniff", true).build();

        TransportClient client = new PreBuiltTransportClient(settings);
        for (String ip : hosts.split(",")) {
            client.addTransportAddress(
                    new TransportAddress(InetAddress.getByName(ip.split(":")[0]),
                            Integer.parseInt(ip.split(":")[1])));
        }
        return client;
    }
    catch (IOException e) {
        throw new PrestoException(UNEXPECTED_ES_ERROR, "Failed to get connection to Elasticsearch", e);
    }
}
 
Example #10
Source File: RawKinesisFieldDecoder.java    From presto-kinesis with Apache License 2.0 6 votes vote down vote up
@Override
public long getLong()
{
    if (isNull()) {
        return 0L;
    }
    switch (fieldType) {
        case BYTE:
            return value.get();
        case SHORT:
            return value.getShort();
        case INT:
            return value.getInt();
        case LONG:
            return value.getLong();
        default:
            throw new PrestoException(KINESIS_CONVERSION_NOT_SUPPORTED, format("conversion %s to long not supported", fieldType));
    }
}
 
Example #11
Source File: DummyKinesisFieldDecoder.java    From presto-kinesis with Apache License 2.0 6 votes vote down vote up
@Override
public KinesisFieldValueProvider decode(Void value, KinesisColumnHandle columnHandle)
{
    checkNotNull(columnHandle, "columnHandle is null");

    return new KinesisFieldValueProvider()
    {
        @Override
        public boolean accept(KinesisColumnHandle handle)
        {
            return false;
        }

        @Override
        public boolean isNull()
        {
            throw new PrestoException(KINESIS_CONVERSION_NOT_SUPPORTED, "is null check not supported");
        }
    };
}
 
Example #12
Source File: HbaseModule.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
@Override
public Connection get()
{
    try {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", zooKeepers);

        conf.set("hbase.client.pause", "50");
        conf.set("hbase.client.retries.number", "3");
        conf.set("hbase.rpc.timeout", "2000");
        conf.set("hbase.client.operation.timeout", "3000");
        conf.set("hbase.client.scanner.timeout.period", "10000");

        Connection connection = ConnectionFactory.createConnection(conf);
        LOG.info("Connection to instance %s at %s established, user %s");
        return connection;
    }
    catch (IOException e) {
        throw new PrestoException(UNEXPECTED_HBASE_ERROR, "Failed to get connection to HBASE", e);
    }
}
 
Example #13
Source File: HbaseTableManager.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
/**
 * Ensures the given Hbase namespace exist, creating it if necessary
 *
 * @param schema Presto schema (Hbase namespace)
 */
public void ensureNamespace(String schema)
{
    try {
        // If the table schema is not "default" and the namespace does not exist, create it
        try (Admin admin = connection.getAdmin()) {
            Set<String> namespaces = Arrays.stream(admin.listNamespaceDescriptors())
                    .map(x -> x.getName()).collect(Collectors.toSet());
            if (!schema.equals(DEFAULT) && !namespaces.contains(schema)) {
                admin.createNamespace(NamespaceDescriptor.create(schema).build());
            }
        }
    }
    catch (IOException e) {
        throw new PrestoException(UNEXPECTED_HBASE_ERROR, "Failed to check for existence or create Hbase namespace", e);
    }
}
 
Example #14
Source File: NativeKuduClientSession.java    From presto-kudu with Apache License 2.0 6 votes vote down vote up
@Override
public void dropSchema(String schemaName) {
    if (DEFAULT_SCHEMA.equals(schemaName)) {
        throw new PrestoException(GENERIC_USER_ERROR, "Deleting default schema not allowed.");
    }
    else {
        try {
            for (SchemaTableName table : listTables(schemaName)) {
                dropTable(table);
            }
            KuduTable schemasTable = getSchemasTable();
            KuduSession session = client.newSession();
            try {
                Delete delete = schemasTable.newDelete();
                fillSchemaRow(delete.getRow(), schemaName);
                session.apply(delete);
            }
            finally {
                session.close();
            }
        }
        catch (KuduException e) {
            throw new PrestoException(GENERIC_INTERNAL_ERROR, e);
        }
    }
}
 
Example #15
Source File: Elasticsearch2Client.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
private static void addEsQueryFilter(Map<String, Object> mergeDslMap, String dsl)
{
    //-------get query-----
    Map<String, Object> queryDsl;
    try {
        final Map<String, Object> dslMap = MAPPER.readValue(dsl, Map.class);
        queryDsl = (Map<String, Object>) dslMap.getOrDefault("query", dslMap);
    }
    catch (IOException e) {
        throw new PrestoException(ES_DSL_ERROR, e);
    }

    Map<String, Object> query = (Map<String, Object>) mergeDslMap.computeIfAbsent("query", k -> new HashMap<>());
    Map bool = (Map) query.computeIfAbsent("bool", k -> new HashMap<>());
    List must = (List) bool.computeIfAbsent("must", k -> new ArrayList<>());
    //--merge dsl--
    must.add(queryDsl);
}
 
Example #16
Source File: Elasticsearch5Client.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
private static void addEsQueryFilter(Map<String, Object> mergeDslMap, String dsl)
{
    //-------get query-----
    Map<String, Object> queryDsl;
    try {
        final Map<String, Object> dslMap = MAPPER.readValue(dsl, Map.class);
        queryDsl = (Map<String, Object>) dslMap.getOrDefault("query", dslMap);
    }
    catch (IOException e) {
        throw new PrestoException(ES_DSL_ERROR, e);
    }

    Map<String, Object> query = (Map<String, Object>) mergeDslMap.computeIfAbsent("query", k -> new HashMap<>());
    Map bool = (Map) query.computeIfAbsent("bool", k -> new HashMap<>());
    List must = (List) bool.computeIfAbsent("must", k -> new ArrayList<>());
    //--merge dsl--
    must.add(queryDsl);
}
 
Example #17
Source File: Elasticsearch5Client.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
@Override
public void insertMany(List<Document> docs)
{
    final BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
    for (Document doc : docs) {
        bulkRequestBuilder.add(new IndexRequest()
                .index(doc.getIndex())
                .type(doc.getType())
                .id(doc.getId())
                .source(doc.getSource()));
    }
    BulkResponse response = bulkRequestBuilder.execute().actionGet();
    if (response.hasFailures()) {
        throw new PrestoException(IO_ERROR, response.buildFailureMessage());
    }
}
 
Example #18
Source File: HbaseRecordCursor.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
@Override
public boolean advanceNextPosition()
{
    if (nanoStart == 0) {
        nanoStart = System.nanoTime();
    }

    try {
        if (resultRecordReader.nextKeyValue()) {
            this.result = resultRecordReader.getCurrentValue();
            return true;
        }
        else {
            return false;
        }
    }
    catch (IOException | InterruptedException e) {
        throw new PrestoException(IO_ERROR, "Caught IO error from resultScanner on read", e);
    }
}
 
Example #19
Source File: HbaseMetadata.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
@Override
public ConnectorInsertTableHandle beginInsert(ConnectorSession session, ConnectorTableHandle tableHandle)
{
    checkNoRollback();
    HbaseTableHandle handle = (HbaseTableHandle) 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.getSchema(), handle.getTable()));
    });
    return handle;
}
 
Example #20
Source File: FSFactory.java    From paraflow with Apache License 2.0 6 votes vote down vote up
public List<Path> listFiles(Path dirPath)
{
    List<Path> files = new ArrayList<>();
    FileStatus[] fileStatuses;
    if (this.fileSystem == null) {
        return ImmutableList.of();
    }
    try {
        fileStatuses = this.fileSystem.listStatus(dirPath);
        if (fileStatuses != null) {
            for (FileStatus f : fileStatuses) {
                //avoid add empty file
                if (f.isFile() && f.getLen() > 0) {
                    files.add(f.getPath());
                }
            }
        }
    }
    catch (IOException e) {
        log.error(e);
        throw new PrestoException(PARAFLOW_HDFS_FILE_ERROR, e);
    }

    return files;
}
 
Example #21
Source File: ParaflowConnectorFactory.java    From paraflow with Apache License 2.0 6 votes vote down vote up
@Override
public Connector create(String connectorId, Map<String, String> config, ConnectorContext context)
{
    requireNonNull(config, "config is null");

    try {
        Bootstrap app = new Bootstrap(
                new JsonModule(),
                new ParaflowModule(connectorId, context.getTypeManager())
        );

        Injector injector = app
                .strictConfig()
                .doNotInitializeLogging()
                .setRequiredConfigurationProperties(config)
                .initialize();

        return injector.getInstance(ParaflowConnector.class);
    }
    catch (Exception e) {
        throw new PrestoException(CONNECTOR_INIT_ERROR, e);
    }
}
 
Example #22
Source File: ZooKeeperMetadataManager.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
public HbaseView getView(SchemaTableName stName)
{
    try {
        String tablePath = getTablePath(stName);
        if (curator.checkExists().forPath(tablePath) != null) {
            return toHbaseView(curator.getData().forPath(tablePath));
        }

        return null;
    }
    catch (Exception e) {
        // Capture race condition between checkExists and getData
        if (e instanceof KeeperException && ((KeeperException) e).code() == NONODE) {
            return null;
        }

        throw new PrestoException(ZOOKEEPER_ERROR, "Error fetching view", e);
    }
}
 
Example #23
Source File: ZooKeeperMetadataManager.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
public HbaseTable getTable(SchemaTableName stName)
{
    try {
        if (curator.checkExists().forPath(getTablePath(stName)) != null) {
            return toHbaseTable(curator.getData().forPath(getTablePath(stName)));
        }

        return null;
    }
    catch (Exception e) {
        // Capture race condition between checkExists and getData
        if (e instanceof KeeperException && ((KeeperException) e).code() == NONODE) {
            return null;
        }

        throw new PrestoException(ZOOKEEPER_ERROR, "Error fetching table", e);
    }
}
 
Example #24
Source File: NativeKuduClientSession.java    From presto-kudu with Apache License 2.0 6 votes vote down vote up
@Override
public void createSchema(String schemaName) {
    if (DEFAULT_SCHEMA.equals(schemaName)) {
        throw new SchemaAlreadyExistsException(schemaName);
    }
    else {
        try {
            KuduTable schemasTable = getSchemasTable();
            KuduSession session = client.newSession();
            try {
                Upsert upsert = schemasTable.newUpsert();
                fillSchemaRow(upsert.getRow(), schemaName);
                session.apply(upsert);
            }
            finally {
                session.close();
            }
        }
        catch (KuduException e) {
            throw new PrestoException(GENERIC_INTERNAL_ERROR, e);
        }
    }
}
 
Example #25
Source File: NativeKuduClientSession.java    From presto-kudu with Apache License 2.0 6 votes vote down vote up
@Override
public KuduTable createTable(ConnectorTableMetadata tableMetadata, boolean ignoreExisting) {
    try {
        SchemaTableName schemeTableName= tableMetadata.getTable();
        String rawName = toRawName(schemeTableName);
        if (ignoreExisting) {
            if (client.tableExists(rawName)) {
                return null;
            }
        }
        if(!schemaExists(schemeTableName.getSchemaName())){
            throw new SchemaNotFoundException(schemeTableName.getSchemaName());
        }
        List<ColumnMetadata> columns = tableMetadata.getColumns();
        Map<String, Object> properties = tableMetadata.getProperties();

        Schema schema = buildSchema(columns, properties);
        CreateTableOptions options = buildCreateTableOptions(schema, properties);
        return client.createTable(rawName, schema, options);
    } catch (KuduException e) {
        throw new PrestoException(GENERIC_INTERNAL_ERROR, e);
    }
}
 
Example #26
Source File: Elasticsearch5Client.java    From presto-connectors with Apache License 2.0 5 votes vote down vote up
@Override
public void createTable(ConnectorTableMetadata tableMetadata)
{
    XContentBuilder mapping = getMapping(tableMetadata.getColumns());
    String index = tableMetadata.getTable().getTableName();
    try {
        //TODO: default type value is presto
        client.admin().indices().prepareCreate(index)
                .addMapping("presto", mapping)
                .execute().actionGet();
    }
    catch (MapperParsingException e) {
        throw new PrestoException(ES_MAPPING_ERROR, "Failed create index:" + index, e);
    }
}
 
Example #27
Source File: RawKinesisFieldDecoder.java    From presto-kinesis with Apache License 2.0 5 votes vote down vote up
@Override
public double getDouble()
{
    if (isNull()) {
        return 0.0d;
    }
    switch (fieldType) {
        case FLOAT:
            return value.getFloat();
        case DOUBLE:
            return value.getDouble();
        default:
            throw new PrestoException(KINESIS_CONVERSION_NOT_SUPPORTED, format("conversion %s to double not supported", fieldType));
    }
}
 
Example #28
Source File: HbasePageSink.java    From presto-connectors with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Collection<Slice>> finish()
{
    try (Table table = hTable) {
        flush();
    }
    catch (IOException e) {
        throw new PrestoException(UNEXPECTED_HBASE_ERROR, "Error when htable closes", e);
    }
    // TODO Look into any use of the metadata for writing out the rows
    return completedFuture(ImmutableList.of());
}
 
Example #29
Source File: RawKinesisFieldDecoder.java    From presto-kinesis with Apache License 2.0 5 votes vote down vote up
@Override
public Slice getSlice()
{
    if (isNull()) {
        return Slices.EMPTY_SLICE;
    }

    if (fieldType == FieldType.BYTE) {
        return Slices.wrappedBuffer(value.slice());
    }

    throw new PrestoException(KINESIS_CONVERSION_NOT_SUPPORTED, format("conversion %s to Slice not supported", fieldType));
}
 
Example #30
Source File: HbasePageSink.java    From presto-connectors with Apache License 2.0 5 votes vote down vote up
private void flush()
{
    try {
        hTable.put(puts);
    }
    catch (IOException e) {
        throw new PrestoException(UNEXPECTED_HBASE_ERROR, "puts rejected by server on flush", e);
    }
    finally {
        puts.clear();
        numRows = 0;
    }
}