Example usage for org.apache.thrift TDeserializer TDeserializer

List of usage examples for org.apache.thrift TDeserializer TDeserializer

Introduction

In this page you can find the example usage for org.apache.thrift TDeserializer TDeserializer.

Prototype

public TDeserializer() 

Source Link

Document

Create a new TDeserializer that uses the TBinaryProtocol by default.

Usage

From source file:ezbake.services.graph.archive.AccumuloTransactionArchive.java

License:Apache License

/**
 * Constructor -- create the connection to Accumulo, set the table name used for transaction archive to be default.
 * Thrift serializer and deserializer are also initialized.
 *
 * @param config Spring Framework Configuration that contains Accumulo Login info.
 * @throws TransactionArchiveException Exception may throw when connection to Accumulo fails.
 */// ww w  .  j a  v  a2 s .com
public AccumuloTransactionArchive(EzConfiguration config) throws TransactionArchiveException {
    tableName = TABLENAME_DEFAULT;

    try {
        final AccumuloHelper helper = new AccumuloHelper(config.getProperties());
        connector = helper.getConnector(false);

        if (!connector.tableOperations().exists(tableName)) {
            connector.tableOperations().create(tableName);
        }

        final BatchWriterConfig bwc = new BatchWriterConfig();
        bwc.setMaxMemory(MAX_BATCHWRITER_MEMORY);
        bwc.setMaxWriteThreads(MAX_BATCHWRITER_THREADS);
        bwc.setMaxLatency(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
        writer = connector.createBatchWriter(tableName, bwc);
    } catch (final TableNotFoundException ex) {
        throw new TransactionArchiveException(ex);
    } catch (final AccumuloException ex) {
        throw new TransactionArchiveException(ex);
    } catch (final AccumuloSecurityException ex) {
        throw new TransactionArchiveException(ex);
    } catch (final TableExistsException ex) {
        throw new TransactionArchiveException(ex);
    } catch (final IOException ex) {
        throw new TransactionArchiveException(ex);
    }

    tSerializer = new TSerializer();
    tDeserializer = new TDeserializer();
}

From source file:ezbake.thrift.serializer.BinarySerializer.java

License:Apache License

@Override
public <U extends TBase<?, ?>> U deserialize(Class<U> thriftClass, byte[] serializedObject) throws TException {
    final TDeserializer deserializer = new TDeserializer();
    try {/*  w  w w. j  ava2s.co m*/
        final U object = thriftClass.newInstance();
        deserializer.deserialize(object, serializedObject);
        return object;
    } catch (final TException e) {
        throw e;
    } catch (final Exception e) {
        throw new TException(e);
    }
}

From source file:ezbake.thrift.ThriftUtils.java

License:Apache License

/**
 * Deserialize a thrift object//  w  w  w  . j  a  v  a  2  s . c om
 *
 * @param type The type of object
 * @param bytes The bytes of the object
 * @param <T> The type of object
 * @return The object
 */
public static <T extends TBase<?, ?>> T deserialize(Class<T> type, byte[] bytes) throws TException {
    final TDeserializer deserializer = new TDeserializer();
    try {
        final T object = type.newInstance();
        deserializer.deserialize(object, bytes);
        return object;
    } catch (final Exception ex) {
        throw new TException(ex);
    }
}

From source file:ezbake.warehaus.tools.WarehausExport.java

License:Apache License

public ByteBuffer exportTestData(String urn, DateTime start, DateTime finish,
        DocumentClassification classification, EzSecurityToken security) throws TException {
    if (urn == null || "".equals(urn.trim())) {
        throw new TException("Cannot export a null or empty urn");
    }//from w w w.  j av a 2  s.c  o m

    ExportImportHelper.confirmToken(security, securityClient);
    String clearance = classification.getClassification();
    ByteBuffer forReturn;
    HashMap<String, Map<Long, ExportFile>> uriTimestamps = Maps.newHashMap();
    BatchScanner scanner = null;
    try {
        scanner = createScanner(clearance);

        long startTime = 0, endTime = Long.MAX_VALUE;
        if (start != null) {
            startTime = TimeUtil.convertFromThriftDateTime(start);
        }
        if (finish != null) {
            endTime = TimeUtil.convertFromThriftDateTime(finish);
        }
        scanner.setRanges(Lists.newArrayList(new Range())); // empty range

        IteratorSetting iterator = new IteratorSetting(10, "regex", RegExFilter.class);
        RegExFilter.setRegexs(iterator, WarehausUtils.getPatternForURI(urn).toString(), null, null, null,
                false);
        scanner.addScanIterator(iterator);

        IteratorSetting is = new IteratorSetting(5, "version", VersioningIterator.class);
        VersioningIterator.setMaxVersions(is, Integer.MAX_VALUE);
        scanner.addScanIterator(is);

        for (Entry<Key, Value> entry : scanner) {

            long ts = entry.getKey().getTimestamp();
            if (startTime <= ts && ts <= endTime) {
                String uri = WarehausUtils.getUriFromKey(entry.getKey());
                Map<Long, ExportFile> repoMap = uriTimestamps.get(uri);
                if (repoMap == null) {
                    repoMap = Maps.newHashMap();
                }
                ExportFile ef = new ExportFile();
                Repository data = new Repository();
                VersionControl versionData = new VersionControl();
                new TDeserializer().deserialize(versionData, entry.getValue().get());

                if (repoMap.containsKey(ts)) {
                    ef = repoMap.get(ts);
                    data = ef.getData();
                } else {
                    data.setUri(uri);
                    ef.setVisibility(new Visibility()
                            .setFormalVisibility(entry.getKey().getColumnVisibility().toString()));
                    ef.setName(versionData.getName());
                    ef.setTimestamp(entry.getKey().getTimestamp());
                }

                if (entry.getKey().getColumnQualifier().toString()
                        .equals(ExportImportHelper.DataType.PARSED.toString())) {
                    data.setParsedData(versionData.getPacket());
                } else if (entry.getKey().getColumnQualifier().toString()
                        .equals(ExportImportHelper.DataType.RAW.toString())) {
                    data.setRawData(versionData.getPacket());
                }
                versionData.clear();
                ef.setData(data);
                repoMap.put(ts, ef);
                uriTimestamps.put(uri, repoMap);
            }
        }
    } finally {
        if (scanner != null) {
            scanner.close();
        }
    }

    int count = 1;
    TSerializer serializer = new TSerializer();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    TarOutputStream tar = new TarOutputStream(new BufferedOutputStream(baos));

    try {
        for (Map<Long, ExportFile> files : uriTimestamps.values()) {
            for (ExportFile ef : files.values()) {

                byte[] bytes = serializer.serialize(ef);
                TarEntry tarFile = new TarEntry(Integer.toString(count) + ".whe");
                tarFile.setSize(bytes.length);
                tar.putNextEntry(tarFile);
                tar.write(bytes);
                tar.closeEntry();
                tar.flush();
                baos.flush();
                if (count == 100) {
                    break;
                }
                count++;
            }
        }
    } catch (IOException e) {
        throw new TException(e);
    } finally {
        try {
            tar.finish();
            tar.close();
        } catch (IOException e) {
            throw new TException(e);
        }
        forReturn = ByteBuffer.wrap(baos.toByteArray());
    }

    return forReturn;
}

From source file:ezbake.warehaus.tools.WarehausImport.java

License:Apache License

public void importTestData(ByteBuffer tArchive, EzSecurityToken security) throws TException {
    TDeserializer deserializer = new TDeserializer();
    TarInputStream tar = new TarInputStream(new ByteArrayInputStream(tArchive.array()));
    TarEntry entry;/*from ww  w . ja v  a  2 s.  c  o m*/

    List<String> uriList = Lists.newArrayList();
    Map<String, VersionControl> parsed = Maps.newHashMap();
    Map<String, VersionControl> raw = Maps.newHashMap();
    Map<String, ColumnVisibility> visibilityMap = Maps.newHashMap();

    try {
        entry = tar.getNextEntry();
        while (entry != null) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int value = tar.read();
            while (value > -1) {
                baos.write(value);
                value = tar.read();
            }
            ExportFile file = new ExportFile();
            deserializer.deserialize(file, baos.toByteArray());

            String uri = file.getData().getUri();
            uriList.add(uri);
            raw.put(uri, new VersionControl(ByteBuffer.wrap(file.getData().getRawData()), file.getName()));
            parsed.put(uri,
                    new VersionControl(ByteBuffer.wrap(file.getData().getParsedData()), file.getName()));
            visibilityMap.put(uri, new ColumnVisibility(file.getVisibility().getFormalVisibility()));

            entry = tar.getNextEntry();
        }

        insertData(uriList, parsed, raw, visibilityMap, security);
    } catch (IOException e) {
        throw new TException(e);
    } finally {
        try {
            tar.close();
        } catch (IOException e) {
            throw new TException(e);
        }
    }
}

From source file:ezbake.warehaus.tools.WarehausInsert.java

License:Apache License

public void process() throws TException, IOException, EzConfigurationLoaderException {
    Properties config;/*w  w  w. ja va 2s . c  om*/
    try {
        config = new EzConfiguration().getProperties();
    } catch (EzConfigurationLoaderException e) {
        throw new RuntimeException(e);
    }
    ThriftClientPool pool = new ThriftClientPool(config);
    WarehausService.Client client = ToolHelper.createClient(pool);
    EzSecurityToken token = ToolHelper.importToken(pool);
    Repository whp = new Repository();
    new TDeserializer().deserialize(whp, ToolHelper.importFile(file));
    System.out.println(
            client.insert(whp, new Visibility().setFormalVisibility(classification), token).getTimestamp());
    pool.returnToPool(client);
    pool.close();

}

From source file:org.apache.accumulo.core.security.CredentialHelper.java

License:Apache License

public static TCredentials fromByteArray(byte[] serializedCredential) throws AccumuloSecurityException {
    if (serializedCredential == null)
        return null;
    TDeserializer td = new TDeserializer();
    try {//from   ww w.j  av  a2s  .c o  m
        TCredentials toRet = new TCredentials();
        td.deserialize(toRet, serializedCredential);
        return toRet;
    } catch (TException e) {
        // This really shouldn't happen
        log.error(e, e);
        throw new AccumuloSecurityException("unknown", SecurityErrorCode.SERIALIZATION_ERROR);
    }
}

From source file:org.apache.airavata.common.utils.ThriftUtils.java

License:Apache License

public static void createThriftFromBytes(byte[] bytes, TBase object) throws TException {
    new TDeserializer().deserialize(object, bytes);
}

From source file:org.apache.cassandra.hadoop.hive.metastore.MetaStorePersister.java

License:Apache License

@SuppressWarnings("unchecked")
public TBase load(TBase base, String databaseName) throws CassandraHiveMetaStoreException, NotFoundException {
    databaseName = databaseName.toLowerCase();
    if (log.isDebugEnabled())
        log.debug("in load with class: {} dbname: {}", base.getClass().getName(), databaseName);
    deserializer = new TDeserializer();
    try {//from   ww w.j av a2  s  .  c  o m
        ColumnPath columnPath = new ColumnPath(cassandraClientHolder.getColumnFamily());
        columnPath.setColumn(ByteBufferUtil.bytes(buildEntityColumnName(base)));
        ColumnOrSuperColumn cosc = cassandraClientHolder.getClient().get(ByteBufferUtil.bytes(databaseName),
                columnPath, cassandraClientHolder.getReadCl());
        deserializer.deserialize(base, cosc.getColumn().getValue());
    } catch (NotFoundException nfe) {
        throw nfe;
    } catch (Exception e) {
        // TODO same exception handling wrapper as above
        throw new CassandraHiveMetaStoreException(e.getMessage(), e);
    }
    return base;
}

From source file:org.apache.cassandra.hadoop.hive.metastore.MetaStorePersister.java

License:Apache License

@SuppressWarnings("unchecked")
public List<TBase> find(TBase base, String databaseName, String prefix, int count)
        throws CassandraHiveMetaStoreException {
    databaseName = databaseName.toLowerCase();
    if (log.isDebugEnabled())
        log.debug("in find with class: {} dbname: {} prefix: {} and count: {}",
                new Object[] { base.getClass().getName(), databaseName, prefix, count });
    if (count < 0) {
        // TODO make this a config parameter.
        count = 100;//from   w  w  w  . j  a  va2  s.c  o m
    }
    deserializer = new TDeserializer();

    List<TBase> resultList;
    try {
        SlicePredicate predicate = new SlicePredicate();
        predicate.setSlice_range(buildEntitySlicePrefix(base, prefix, count));
        List<ColumnOrSuperColumn> cols = cassandraClientHolder.getClient().get_slice(
                ByteBufferUtil.bytes(databaseName), new ColumnParent(cassandraClientHolder.getColumnFamily()),
                predicate, cassandraClientHolder.getReadCl());
        resultList = new ArrayList<TBase>(cols.size());
        for (ColumnOrSuperColumn cosc : cols) {
            TBase other = base.getClass().newInstance();
            deserializer.deserialize(other, cosc.getColumn().getValue());
            resultList.add(other);
        }
    } catch (Exception e) {
        // TODO same exception handling wrapper as above
        throw new CassandraHiveMetaStoreException(e.getMessage(), e);
    }
    return resultList;
}