Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  * Copyright (c) 2013, Cloudera, Inc. All Rights Reserved.
  * Cloudera, Inc. 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
 * This software 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 com.cloudera.oryx.kmeans.serving.web;

Responds to POST request to /add. The input is one or more data points to add to the clustering, one for each line of the request body. Each data point is a delimited line of input like "1,-4,3.0". Also, one data point can be supplied by POSTing to /train/[datum]. The clusters update to learn in some way from the new data. The response is empty.

Sean Owen
public final class AddServlet extends AbstractKMeansServlet {
  protected void doPost(HttpServletRequest requestHttpServletResponse responsethrows IOException {
    String pathInfo = request.getPathInfo();
    if (pathInfo == null || pathInfo.isEmpty() || "/".equals(pathInfo)) {
      doAdd(CharStreams.readLines(request.getReader()), response);
    } else {
      doAdd(Collections.singletonList(pathInfo.substring(1)), response);
  private void doAdd(List<StringlinesHttpServletResponse responsethrows IOException {
    KMeansGenerationManager generationManager = getGenerationManager();
    Generation generation = generationManager.getCurrentGeneration();
    if (generation == null) {
                         "API method unavailable until model has been built and loaded");
    for (CharSequence line : lines) {
      RealVector vec = generation.toVector(DelimitedDataUtils.decode(line));
      if (vec == null) {
        response.sendError(."Wrong column count");
      // TODO update the centers, along the lines of Meyerson et al.
New to GrepCode? Check out our FAQ X