accumulo-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From z11373 <z11...@outlook.com>
Subject sample c++ code for scanner
Date Mon, 02 May 2016 15:04:01 GMT
Hi,
Anybody has sample code of using client proxy to read Accumulo data in C++?
Below is our wrapper code (snippets from 3 wrapper code files), which it
always throws 'accumulo::UnknownScanner' exception when calling
client->createBatchScanner (I mark it in the code below), and I couldn't
figure it out :-(


// snippet from wrapper_connector.cpp
//
Connector::Connector(const std::string& host,
                     int port,
                     const std::string& username,
                     const std::string& password)
: d_socket(new apache::thrift::transport::TSocket(host, port))
, d_transport(new apache::thrift::transport::TFramedTransport(d_socket))
, d_protocol(new apache::thrift::protocol::TCompactProtocol(d_transport))
{
    d_transport->open();
    const std::string passkey("password");

    std::map<std::string, std::string> m;
    m.insert(std::make_pair(passkey, password));

    d_client.reset(new ::accumulo::AccumuloProxyClient(d_protocol));
    d_client->login(d_login, username, m);

    d_tableOperations.reset(new TableOperations(d_client, d_login));
}

BatchScanner
Connector::createBatchScanner(const std::string& tableName,
                              const Authorizations& authorizations,
                              const int32_t numThreads)
{
    return BatchScanner(d_client, d_login, tableName, authorizations,
numThreads);
}


// snippet from wrapper_batchscanner.cpp
//
BatchScanner::BatchScanner(std::shared_ptr<::accumulo::AccumuloProxyClient>
proxyClient,
                           const std::string& login,
                           const std::string& tableName,
                           const Authorizations& authorizations,
                           const int32_t numThreads)
: d_client(proxyClient)
, d_login(login)
, d_tableName(tableName)
, d_iterators()
{
    d_options.__set_authorizations(authorizations.getAuthorizations());
    d_options.__set_threads(numThreads);
}

void BatchScanner::setRanges(const std::vector<::accumulo::Range>& ranges)
{
    d_options.__set_ranges(ranges);
}

void BatchScanner::fetchColumn(const std::string& colFamily,
                               const std::string& colQual)
{
    ::accumulo::ScanColumn scanColumn;
    scanColumn.__set_colFamily(colFamily);
    scanColumn.__set_colQualifier(colQual);

    d_columns.push_back(scanColumn);
}

void BatchScanner::fetchColumnFamily(const std::string& colFamily)
{
    ::accumulo::ScanColumn scanColumn;
    scanColumn.__set_colFamily(colFamily);

    d_columns.push_back(scanColumn);
}

void BatchScanner::attachScanIterator(const ::accumulo::IteratorSetting&
iteratorSetting)
{
    d_iterators.push_back(iteratorSetting);
}

const ::accumulo::BatchScanOptions& BatchScanner::getOptions() const
{
    return d_options;
}

BatchScannerIterator BatchScanner::begin()
{
    d_options.__set_columns(d_columns);
    d_options.__set_iterators(d_iterators);
    BatchScannerIterator iterator(d_client, d_login, d_tableName,
d_options);
    return iterator;
}

BatchScannerIterator BatchScanner::end()
{
    return BatchScannerIterator();
}


// snippet from wrapper_batchscanneriterator.cpp
//
    struct BatchScannerIterator::Impl
    {
        std::shared_ptr<::accumulo::AccumuloProxyClient> d_client;
        std::string d_login;
        std::string d_tableName;
        ::accumulo::BatchScanOptions d_options;
        std::string d_scannerToken;
        ::accumulo::KeyValueAndPeek d_kpeek;
        boost::optional<::accumulo::KeyValue> d_value;

        Impl(
            std::shared_ptr<::accumulo::AccumuloProxyClient> proxyClient,
            const std::string& login,
            const std::string& tableName,
            ::accumulo::BatchScanOptions& options) :
            d_client(proxyClient),
            d_login(login),
            d_tableName(tableName),
            d_options(options),
            d_value()
        {

        }

        ~Impl()
        {
            close();
        }

        void close()
        {
            if(d_client)
            {
                d_client->closeScanner(d_scannerToken);
            }
            d_client.reset();
        }
    };

    BatchScannerIterator::BatchScannerIterator()
    : d_impl()
    {

    }

    BatchScannerIterator::BatchScannerIterator(
        std::shared_ptr<::accumulo::AccumuloProxyClient> proxyClient,
        const std::string& login,
        const std::string& tableName,
        ::accumulo::BatchScanOptions& options) :
        d_impl(new Impl(proxyClient, login, tableName, options))
    {
        d_impl->d_client->createBatchScanner(
            d_impl->d_scannerToken,
            d_impl->d_login,
            d_impl->d_tableName,
            d_impl->d_options
        );                //  -------------> this line always throw
exception :-(

        increment();
    }

    void BatchScannerIterator::increment()
    {
        if(d_impl->d_client->hasNext(d_impl->d_scannerToken))
        {
            d_impl->d_client->nextEntry(d_impl->d_kpeek,
d_impl->d_scannerToken);
            d_impl->d_value.reset();
        }
        else {
            d_impl.reset(); // if there are no more elements
        }
    }


Thanks,
Z




--
View this message in context: http://apache-accumulo.1065345.n5.nabble.com/sample-c-code-for-scanner-tp16823.html
Sent from the Developers mailing list archive at Nabble.com.

Mime
View raw message