protected List<Object> parse(String string) {
   // break it down into fields
   List<Object> template = new ArrayList<Object>();
   while (string.contains("{")) {
     int startPattern = string.indexOf("{");
     template.add(string.substring(0, startPattern));
     int endPattern = string.indexOf("}");
     Assert.isTrue(endPattern > startPattern + 1, "Invalid pattern given " + string);
     String nestedString = string.substring(startPattern + 1, endPattern);
     int separator = nestedString.indexOf(":");
     if (separator > 0) {
       Assert.isTrue(
           nestedString.length() > separator + 1, "Invalid format given " + nestedString);
       String format = nestedString.substring(separator + 1);
       nestedString = nestedString.substring(0, separator);
       template.add(wrapWithFormatter(format, createFieldExtractor(nestedString)));
     } else {
       template.add(createFieldExtractor(nestedString));
     }
     string = string.substring(endPattern + 1).trim();
   }
   if (StringUtils.hasText(string)) {
     template.add(string);
   }
   return template;
 }
Exemplo n.º 2
0
  /** Saves the settings state after validating them. */
  public void save() {
    String targetUri = getTargetUri();
    String resource = getTargetResource();

    Assert.hasText(targetUri, "No address specified");
    Assert.hasText(
        resource,
        String.format("No resource (index/query/location) ['%s'] specified", ES_RESOURCE));

    setProperty(INTERNAL_ES_TARGET_URI, targetUri);
    setProperty(INTERNAL_ES_TARGET_RESOURCE, resource);
  }
  @Override
  public void compile(String pattern) {
    this.pattern = pattern;
    // break it down into index/type
    String[] split = pattern.split("/");
    Assert.isTrue(!ObjectUtils.isEmpty(split), "invalid pattern given " + pattern);
    Assert.isTrue(split.length == 2, "invalid pattern given " + pattern);

    // check pattern
    hasPattern = pattern.contains("{") && pattern.contains("}");
    index = parse(split[0].trim());
    type = parse(split[1].trim());
  }
  @Override
  protected String extractField(Object target) {
    if (target instanceof HiveType) {
      HiveType type = (HiveType) target;
      ObjectInspector inspector = type.getObjectInspector();
      if (inspector instanceof StructObjectInspector) {
        StructObjectInspector soi = (StructObjectInspector) inspector;
        StructField field = soi.getStructFieldRef(fieldName);
        ObjectInspector foi = field.getFieldObjectInspector();
        Assert.isTrue(
            foi.getCategory() == ObjectInspector.Category.PRIMITIVE,
            String.format(
                "Field [%s] needs to be a primitive; found [%s]", fieldName, foi.getTypeName()));

        // expecting a writeable - simply do a toString
        Object data = soi.getStructFieldData(type.getObject(), field);
        if (data == null || data instanceof NullWritable) {
          return StringUtils.EMPTY;
        }
        return data.toString();
      }
    }

    return null;
  }
  /** Saves the settings state after validating them. */
  public void save() {
    String resource = getTargetResource();
    String hosts = getTargetHosts();

    Assert.hasText(
        resource,
        String.format("No resource (index/query/location) ['%s'] specified", ES_RESOURCE));
    setProperty(INTERNAL_ES_TARGET_RESOURCE, resource);
    setProperty(INTERNAL_ES_HOSTS, hosts);
  }
  @Override
  public void setSettings(Settings settings) {
    this.settings = settings;

    String paramString = settings.getUpdateScriptParams();
    List<String> fields = StringUtils.tokenize(paramString);
    for (String string : fields) {
      List<String> param = StringUtils.tokenize(string, ":");
      Assert.isTrue(param.size() == 2, "Invalid param definition " + string);

      params.put(param.get(0), createFieldExtractor(param.get(1)));
    }
  }
  public NetworkClient(Settings settings, TransportFactory transportFactory) {
    this.settings = settings.copy();
    this.nodes = SettingsUtils.discoveredOrDeclaredNodes(settings);
    this.transportFactory = transportFactory;

    // shuffle the list of nodes so in case of failures, the fallback is spread
    Collections.shuffle(nodes);

    if (SettingsUtils.hasPinnedNode(settings)) {
      // move pinned node in front to be selected (only once)
      String pinnedNode = SettingsUtils.getPinnedNode(settings);

      if (log.isDebugEnabled()) {
        log.debug("Opening (pinned) network client to " + pinnedNode);
      }

      nodes.remove(pinnedNode);
      nodes.add(0, pinnedNode);
    }

    selectNextNode();

    Assert.notNull(currentTransport, "no node information provided");
  }
 private ContentBuilder(Generator generator, ValueWriter writer) {
   Assert.notNull(generator);
   this.generator = generator;
   this.writer = writer;
 }