Search in sources :

Example 1 with ElasticSearchBulkMeta

use of org.pentaho.di.trans.steps.elasticsearchbulk.ElasticSearchBulkMeta in project pentaho-kettle by pentaho.

the class ElasticSearchBulkDialog method test.

private void test(TestType testType) {
    // Save off the thread's context class loader to restore after the test
    ClassLoader originalClassloader = Thread.currentThread().getContextClassLoader();
    // Now ensure that the thread's context class loader is the plugin's classloader
    Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
    Client client = null;
    Node node = null;
    try {
        ElasticSearchBulkMeta tempMeta = new ElasticSearchBulkMeta();
        toModel(tempMeta);
        Settings.Builder settingsBuilder = Settings.settingsBuilder();
        // keep default classloader
        settingsBuilder.put(Settings.Builder.EMPTY_SETTINGS);
        settingsBuilder.put(tempMeta.getSettingsMap());
        TransportClient.Builder tClientBuilder = TransportClient.builder().settings(settingsBuilder);
        if (!tempMeta.getServers().isEmpty()) {
            node = null;
            TransportClient tClient = tClientBuilder.build();
            for (ElasticSearchBulkMeta.Server s : tempMeta.getServers()) {
                tClient.addTransportAddress(s.getAddr());
            }
            client = tClient;
        } else {
            NodeBuilder nodeBuilder = NodeBuilder.nodeBuilder();
            nodeBuilder.settings(settingsBuilder);
            node = nodeBuilder.client(true).node();
            client = node.client();
            node.start();
        }
        AdminClient admin = client.admin();
        switch(testType) {
            case INDEX:
                if (StringUtils.isBlank(tempMeta.getIndex())) {
                    showError(BaseMessages.getString(PKG, "ElasticSearchBulk.Error.NoIndex"));
                    break;
                }
                // First check to see if the index exists
                IndicesExistsRequestBuilder indicesExistBld = admin.indices().prepareExists(tempMeta.getIndex());
                IndicesExistsResponse indicesExistResponse = indicesExistBld.execute().get();
                if (!indicesExistResponse.isExists()) {
                    showError(BaseMessages.getString(PKG, "ElasticSearchBulkDialog.Error.NoIndex"));
                    return;
                }
                RecoveryRequestBuilder indicesBld = admin.indices().prepareRecoveries(tempMeta.getIndex());
                ListenableActionFuture<RecoveryResponse> lafInd = indicesBld.execute();
                String shards = "" + lafInd.get().getSuccessfulShards() + "/" + lafInd.get().getTotalShards();
                showMessage(BaseMessages.getString(PKG, "ElasticSearchBulkDialog.TestIndex.TestOK", shards));
                break;
            case CLUSTER:
                ClusterStateRequestBuilder clusterBld = admin.cluster().prepareState();
                ListenableActionFuture<ClusterStateResponse> lafClu = clusterBld.execute();
                ClusterStateResponse cluResp = lafClu.actionGet();
                String name = cluResp.getClusterName().value();
                ClusterState cluState = cluResp.getState();
                int numNodes = cluState.getNodes().size();
                showMessage(BaseMessages.getString(PKG, "ElasticSearchBulkDialog.TestCluster.TestOK", name, numNodes));
                break;
            default:
                break;
        }
    } catch (NoNodeAvailableException e) {
        showError(BaseMessages.getString(PKG, "ElasticSearchBulkDialog.Error.NoNodesFound"));
    } catch (MasterNotDiscoveredException e) {
        showError(BaseMessages.getString(PKG, "ElasticSearchBulkDialog.Error.NoNodesFound"));
    } catch (Exception e) {
        showError(e.getLocalizedMessage());
    } finally {
        if (client != null) {
            client.close();
        }
        if (node != null) {
            node.close();
        }
    }
    // Restore the original classloader
    Thread.currentThread().setContextClassLoader(originalClassloader);
}
Also used : Node(org.elasticsearch.node.Node) ClusterStateRequestBuilder(org.elasticsearch.action.admin.cluster.state.ClusterStateRequestBuilder) NodeBuilder(org.elasticsearch.node.NodeBuilder) RecoveryResponse(org.elasticsearch.action.admin.indices.recovery.RecoveryResponse) IndicesExistsResponse(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse) RecoveryRequestBuilder(org.elasticsearch.action.admin.indices.recovery.RecoveryRequestBuilder) ElasticSearchBulkMeta(org.pentaho.di.trans.steps.elasticsearchbulk.ElasticSearchBulkMeta) AdminClient(org.elasticsearch.client.AdminClient) TransportClient(org.elasticsearch.client.transport.TransportClient) Client(org.elasticsearch.client.Client) Settings(org.elasticsearch.common.settings.Settings) ClusterState(org.elasticsearch.cluster.ClusterState) TransportClient(org.elasticsearch.client.transport.TransportClient) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) MasterNotDiscoveredException(org.elasticsearch.discovery.MasterNotDiscoveredException) MasterNotDiscoveredException(org.elasticsearch.discovery.MasterNotDiscoveredException) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) KettleException(org.pentaho.di.core.exception.KettleException) IndicesExistsRequestBuilder(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequestBuilder) AdminClient(org.elasticsearch.client.AdminClient)

Aggregations

ClusterStateRequestBuilder (org.elasticsearch.action.admin.cluster.state.ClusterStateRequestBuilder)1 ClusterStateResponse (org.elasticsearch.action.admin.cluster.state.ClusterStateResponse)1 IndicesExistsRequestBuilder (org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequestBuilder)1 IndicesExistsResponse (org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse)1 RecoveryRequestBuilder (org.elasticsearch.action.admin.indices.recovery.RecoveryRequestBuilder)1 RecoveryResponse (org.elasticsearch.action.admin.indices.recovery.RecoveryResponse)1 AdminClient (org.elasticsearch.client.AdminClient)1 Client (org.elasticsearch.client.Client)1 NoNodeAvailableException (org.elasticsearch.client.transport.NoNodeAvailableException)1 TransportClient (org.elasticsearch.client.transport.TransportClient)1 ClusterState (org.elasticsearch.cluster.ClusterState)1 Settings (org.elasticsearch.common.settings.Settings)1 MasterNotDiscoveredException (org.elasticsearch.discovery.MasterNotDiscoveredException)1 Node (org.elasticsearch.node.Node)1 NodeBuilder (org.elasticsearch.node.NodeBuilder)1 KettleException (org.pentaho.di.core.exception.KettleException)1 ElasticSearchBulkMeta (org.pentaho.di.trans.steps.elasticsearchbulk.ElasticSearchBulkMeta)1