Package org.apache.blur.console.util

Source Code of org.apache.blur.console.util.TableUtil

/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.blur.console.util;

import org.apache.blur.thirdparty.thrift_0_9_0.TException;
import org.apache.blur.thrift.BlurClient;
import org.apache.blur.thrift.generated.Blur.Iface;
import org.apache.blur.thrift.generated.ColumnDefinition;
import org.apache.blur.thrift.generated.Schema;
import org.apache.blur.thrift.generated.TableDescriptor;
import org.apache.blur.thrift.generated.TableStats;

import java.io.IOException;
import java.util.*;

public class TableUtil {

  @SuppressWarnings("rawtypes")
  public static Map<String, List> getTableSummaries() throws IOException, TException {
    Iface client = BlurClient.getClient(Config.getConnectionString());

    List<Map<String, Object>> summaries = new ArrayList<Map<String, Object>>();

    List<String> clusters = client.shardClusterList();

    for (String cluster : clusters) {
      List<String> tables = client.tableListByCluster(cluster);
      for (String table : tables) {
        Map<String, Object> tableInfo = new HashMap<String, Object>();
        TableDescriptor descriptor = client.describe(table);

        tableInfo.put("cluster", cluster);
        tableInfo.put("name", table);
        tableInfo.put("enabled", descriptor.isEnabled());

        if (descriptor.isEnabled()) {
          TableStats stats = client.tableStats(table);
          tableInfo.put("rows", stats.getRowCount());
          tableInfo.put("records", stats.getRecordCount());

          Schema schema = client.schema(table);
          tableInfo.put("families", new ArrayList<String>(schema.getFamilies().keySet()));
        } else {
          tableInfo.put("rows", "?");
          tableInfo.put("records", "?");
          tableInfo.put("families", new ArrayList<String>());
        }

        summaries.add(tableInfo);
      }
    }

    Map<String, List> data = new HashMap<String, List>();
    data.put("tables", summaries);
    data.put("clusters", clusters);

    return data;
  }

  public static Map<String, Map<String, Map<String, Object>>> getSchema(String table) throws IOException, TException {
    Iface client = BlurClient.getClient(Config.getConnectionString());

    Schema schema = client.schema(table);

    Map<String, Map<String, Map<String, Object>>> schemaInfo = new TreeMap<String, Map<String, Map<String, Object>>>();
    for (Map.Entry<String, Map<String, ColumnDefinition>> famEntry : schema.getFamilies().entrySet()) {
      Map<String, Map<String, Object>> columns = new TreeMap<String, Map<String, Object>>();
      for (Map.Entry<String, ColumnDefinition> colEntry : famEntry.getValue().entrySet()) {
        Map<String, Object> info = new HashMap<String, Object>();
        ColumnDefinition def = colEntry.getValue();
        info.put("fieldLess", def.isFieldLessIndexed());
        info.put("type", def.getFieldType());
        info.put("extra", def.getProperties());
        columns.put(colEntry.getKey(), info);
      }
      schemaInfo.put(famEntry.getKey(), columns);
    }

    return schemaInfo;
  }

  public static List<String> getTerms(String table, String family, String column, String startWith) throws IOException, TException {
    Iface client = BlurClient.getClient(Config.getConnectionString());

    return client.terms(table, family, column, startWith, (short) 10);
  }

  public static void disableTable(String table) throws TException, IOException {
    Iface client = BlurClient.getClient(Config.getConnectionString());

    client.disableTable(table);
  }

  public static void enableTable(String table) throws TException, IOException {
    Iface client = BlurClient.getClient(Config.getConnectionString());

    client.enableTable(table);
  }

  public static void deleteTable(String table, boolean includeFiles) throws TException, IOException {
    Iface client = BlurClient.getClient(Config.getConnectionString());

    client.removeTable(table, includeFiles);
  }
}
TOP

Related Classes of org.apache.blur.console.util.TableUtil

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.