Example usage for org.apache.solr.common.cloud SolrZkClient getData

List of usage examples for org.apache.solr.common.cloud SolrZkClient getData

Introduction

In this page you can find the example usage for org.apache.solr.common.cloud SolrZkClient getData.

Prototype

public byte[] getData(final String path, final Watcher watcher, final Stat stat, boolean retryOnConnLoss)
        throws KeeperException, InterruptedException 

Source Link

Document

Returns node's data

Usage

From source file:alba.components.FilteredShowFileRequestHandler.java

License:Apache License

private void showFromZooKeeper(SolrQueryRequest req, SolrQueryResponse rsp, CoreContainer coreContainer)
        throws KeeperException, InterruptedException, UnsupportedEncodingException {

    SolrZkClient zkClient = coreContainer.getZkController().getZkClient();

    String adminFile = getAdminFileFromZooKeeper(req, rsp, zkClient, hiddenFiles);

    if (adminFile == null) {
        return;// w  ww  .  j  av a  2 s . com
    }

    // Show a directory listing
    List<String> children = zkClient.getChildren(adminFile, null, true);
    if (children.size() > 0) {

        NamedList<SimpleOrderedMap<Object>> files = new SimpleOrderedMap<>();
        for (String f : children) {
            if (isHiddenFile(req, rsp, f, false, hiddenFiles)) {
                continue;
            }

            SimpleOrderedMap<Object> fileInfo = new SimpleOrderedMap<>();
            files.add(f, fileInfo);
            List<String> fchildren = zkClient.getChildren(adminFile + "/" + f, null, true);
            if (fchildren.size() > 0) {
                fileInfo.add("directory", true);
            } else {
                // TODO? content type
                fileInfo.add("size", f.length());
            }
            // TODO: ?
            // fileInfo.add( "modified", new Date( f.lastModified() ) );
        }
        rsp.add("files", files);
    } else {
        // Include the file contents
        // The file logic depends on RawResponseWriter, so force its use.
        ModifiableSolrParams params = new ModifiableSolrParams(req.getParams());
        params.set(CommonParams.WT, "raw");
        req.setParams(params);

        ContentStreamBase content = new ContentStreamBase.ByteArrayStream(
                zkClient.getData(adminFile, null, null, true), adminFile);
        content.setContentType(req.getParams().get(USE_CONTENT_TYPE));

        // Velocity parsing here!

        // http://velocity.apache.org/engine/devel/developer-guide.html#The_Context
        Velocity.init();

        VelocityContext context = new VelocityContext();

        //add some vars??
        //context.put( "context", new String("Velocity") );

        for (int i = 0; i < rsp.getValues().size(); i++) {
            context.put(rsp.getValues().getName(i), rsp.getValues().getVal(i));
        }

        Template template = null;

        String fname = req.getParams().get("file", null);

        try {
            //TODO what if fname is null?
            template = Velocity.getTemplate(fname);
        } catch (ResourceNotFoundException rnfe) {
            // couldn't find the template, try to load it

            // TODO it should be fired only for SOME mimetypes (..through an annotation??)
            StringBuilder sb = this.getTemplate(content);

            RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
            StringReader reader = new StringReader(sb.toString());
            SimpleNode node = null;
            try {
                node = runtimeServices.parse(reader, fname);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                logger.error("error while parsing new template", e);
            }

            template = new Template();

            template.setRuntimeServices(runtimeServices);

            if (node != null) {
                template.setData(node);
            } else {
                logger.error("node null, can't set on template");
            }

            template.initDocument();

        } catch (ParseErrorException pee) {
            // syntax error: problem parsing the template
            logger.error("error while parsing template: ", pee);

        } catch (MethodInvocationException mie) {
            // something invoked in the template
            // threw an exception
            logger.error("error while parsing temaplate: ", mie);
        } catch (Exception e) {
            logger.error("error while parsing temaplate: ", e);
        }

        StringWriter sw = new StringWriter();

        template.merge(context, sw);

        // http://stackoverflow.com/questions/18571223/how-to-convert-java-string-into-byte
        content = new ContentStreamBase.ByteArrayStream(
                sw.getBuffer().toString().getBytes(Charset.forName("UTF-8")), adminFile);
        content.setContentType(req.getParams().get(USE_CONTENT_TYPE));

        rsp.add(RawResponseWriter.CONTENT, content);
    }
    rsp.setHttpCaching(false);
}

From source file:com.cloudera.cdk.morphline.solr.ZooKeeperDownloader.java

License:Apache License

/**
 * Returns config value given collection name
 * Borrowed heavily from Solr's ZKController.
 *//*from  ww  w. j  a v  a  2s  . c o m*/
public String readConfigName(SolrZkClient zkClient, String collection)
        throws KeeperException, InterruptedException {
    if (collection == null) {
        throw new IllegalArgumentException("collection must not be null");
    }
    String configName = null;

    // first check for alias
    byte[] aliasData = zkClient.getData(ZkStateReader.ALIASES, null, null, true);
    Aliases aliases = ClusterState.load(aliasData);
    String alias = aliases.getCollectionAlias(collection);
    if (alias != null) {
        List<String> aliasList = StrUtils.splitSmart(alias, ",", true);
        if (aliasList.size() > 1) {
            throw new IllegalArgumentException(
                    "collection cannot be an alias that maps to multiple collections");
        }
        collection = aliasList.get(0);
    }

    String path = ZkStateReader.COLLECTIONS_ZKNODE + "/" + collection;
    if (LOG.isInfoEnabled()) {
        LOG.info("Load collection config from:" + path);
    }
    byte[] data = zkClient.getData(path, null, null, true);

    if (data != null) {
        ZkNodeProps props = ZkNodeProps.load(data);
        configName = props.getStr(ZkController.CONFIGNAME_PROP);
    }

    if (configName != null && !zkClient.exists(ZkController.CONFIGS_ZKNODE + "/" + configName, true)) {
        LOG.error("Specified config does not exist in ZooKeeper:" + configName);
        throw new IllegalArgumentException("Specified config does not exist in ZooKeeper:" + configName);
    }

    return configName;
}

From source file:com.doculibre.constellio.services.SolrServicesImpl.java

License:Open Source License

public static Document readXMLConfigInCloud(String collectionName, String fileName) {
    String realCollectionName;/*from   w  w w. j a v a2  s .  c o  m*/
    if (SolrServicesImpl.isAliasInCloud(collectionName)) {
        realCollectionName = SolrServicesImpl.getRealCollectionInCloud(collectionName);
    } else {
        realCollectionName = collectionName;
    }
    Document schemaDocument = null;
    try {
        SolrZkClient zkClient = SolrCoreContext.getSolrZkClient();
        byte[] data = zkClient.getData(ZkController.CONFIGS_ZKNODE + "/" + realCollectionName + "/" + fileName,
                null, null, true);
        schemaDocument = new SAXReader(false).read(new ByteArrayInputStream(data));
    } catch (DocumentException e) {
        throw new RuntimeException(e);
    } catch (KeeperException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return schemaDocument;
}

From source file:com.doculibre.constellio.services.SolrServicesImpl.java

License:Open Source License

public static File readPlainConfigInCloud(String collectionName, String fileName) {
    String realCollectionName;//from w  w w  . j a  v a2 s . c o  m
    if (SolrServicesImpl.isAliasInCloud(collectionName)) {
        realCollectionName = SolrServicesImpl.getRealCollectionInCloud(collectionName);
    } else {
        realCollectionName = collectionName;
    }
    File myTempFile = null;
    try {
        SolrZkClient zkClient = SolrCoreContext.getSolrZkClient();
        byte[] data = zkClient.getData(ZkController.CONFIGS_ZKNODE + "/" + realCollectionName + "/" + fileName,
                null, null, true);
        myTempFile = Files.createTempFile("config_", ".temp").toFile();
        FileOutputStream outputStream = new FileOutputStream(myTempFile);
        outputStream.write(data);
        outputStream.close();
    } catch (KeeperException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return myTempFile;
}

From source file:com.doculibre.constellio.services.SolrServicesImpl.java

License:Open Source License

public static byte[] readPlainConfigInCloud(String fileName) throws KeeperException, InterruptedException {
    SolrZkClient zkClient = SolrCoreContext.getSolrZkClient();
    if (zkClient.exists(ZkController.CONFIGS_ZKNODE + "/" + fileName, true)) {
        byte[] data = zkClient.getData(ZkController.CONFIGS_ZKNODE + "/" + fileName, null, null, true);
        return data;
    }/*from  w w w .j  av  a2 s . co m*/
    return null;
}

From source file:uk.bl.wa.apache.solr.hadoop.ZooKeeperInspector.java

License:Apache License

/**
 * Returns config value given collection name Borrowed heavily from Solr's
 * ZKController.//from w w  w.j  a  v a  2 s .c o m
 */
public String readConfigName(SolrZkClient zkClient, String collection)
        throws KeeperException, InterruptedException {
    if (collection == null) {
        throw new IllegalArgumentException("collection must not be null");
    }
    String configName = null;

    // first check for alias
    collection = checkForAlias(zkClient, collection);

    String path = ZkStateReader.COLLECTIONS_ZKNODE + "/" + collection;
    if (LOG.isInfoEnabled()) {
        LOG.info("Load collection config from:" + path);
    }
    byte[] data = zkClient.getData(path, null, null, true);

    if (data != null) {
        ZkNodeProps props = ZkNodeProps.load(data);
        configName = props.getStr(ZkController.CONFIGNAME_PROP);
    }

    if (configName != null && !zkClient.exists(ZkController.CONFIGS_ZKNODE + "/" + configName, true)) {
        LOG.error("Specified config does not exist in ZooKeeper:" + configName);
        throw new IllegalArgumentException("Specified config does not exist in ZooKeeper:" + configName);
    }

    return configName;
}

From source file:uk.bl.wa.apache.solr.hadoop.ZooKeeperInspector.java

License:Apache License

private String checkForAlias(SolrZkClient zkClient, String collection)
        throws KeeperException, InterruptedException {
    byte[] aliasData = zkClient.getData(ZkStateReader.ALIASES, null, null, true);
    Aliases aliases = ClusterState.load(aliasData);
    String alias = aliases.getCollectionAlias(collection);
    if (alias != null) {
        List<String> aliasList = StrUtils.splitSmart(alias, ",", true);
        if (aliasList.size() > 1) {
            throw new IllegalArgumentException(
                    "collection cannot be an alias that maps to multiple collections");
        }/*from w ww  .  ja  va  2s.c  o m*/
        collection = aliasList.get(0);
    }
    return collection;
}