簡體   English   中英

使用thrift在Cassandra中插入SuperColumn

[英]Insert SuperColumn in Cassandra using thrift

我需要一些清晰的例子,說明如何使用thrift和java在超級列中插入超級列。

@Override
public void insertAllReportsByHost(Map<String, List<IReport>> hostReports) throws DatabaseException {
    try {
        Cassandra.Client client = getClient();

        Map<ByteBuffer, Map<String, List<Mutation>>> mutationsMap = new HashMap<ByteBuffer, Map<String, List<Mutation>>>();

        for (Map.Entry<String, List<IReport>> entryReport : hostReports.entrySet()) {

            ByteBuffer host = toByteBuffer(entryReport.getKey());
            List<IReport> reports = entryReport.getValue();
            Map<String, List<Mutation>> keyMutations = new HashMap<String, List<Mutation>>();
            List<Mutation> mutations = new ArrayList<Mutation>();

            for (IReport report : reports) {
                report.getProperties();
                Column reportDataColumn = new Column(toByteBuffer("Report Data"));
                reportDataColumn.setValue(toByteBuffer(report.toString()));//TODO make another way
                reportDataColumn.setTimestamp(System.currentTimeMillis());

                Long nano = System.nanoTime();
                SuperColumn superColumn = new SuperColumn(toByteBuffer(report.getReportId().toString()), Arrays.asList(reportDataColumn));
                ColumnOrSuperColumn col = new ColumnOrSuperColumn();
                col.super_column = superColumn;

                Mutation m = new Mutation();
                m.setColumn_or_supercolumn(col);

                mutations.add(m);
            }


            keyMutations.put(COLUMN_FAMILY, mutations);

            mutationsMap.put(host, keyMutations);
        }

        client.batch_mutate(mutationsMap, ConsistencyLevel.ONE);

    } catch (UnsupportedEncodingException e) {
        LOGGER.error(e.getMessage(), e);
        throw new DatabaseException(e);
    } catch (UnavailableException e) {
        LOGGER.error(e.getMessage(), e);
        throw new DatabaseException(e);
    } catch (TException e) {
        LOGGER.error(e.getMessage(), e);
        throw new DatabaseException(e);
    } catch (InvalidRequestException e) {
        LOGGER.error(e.getMessage(), e);
        throw new DatabaseException(e);
    } catch (TimedOutException e) {
        LOGGER.error(e.getMessage(), e);
        throw new DatabaseException(e);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM