Example usage for java.util LinkedHashMap put

List of usage examples for java.util LinkedHashMap put

Introduction

In this page you can find the example usage for java.util LinkedHashMap put.

Prototype

V put(K key, V value);

Source Link

Document

Associates the specified value with the specified key in this map (optional operation).

Usage

From source file:com.streamsets.pipeline.stage.destination.hive.HiveMetastoreTargetIT.java

@Test
public void testPartitionMismatch() throws Exception {
    HiveMetastoreTarget hiveTarget = new HiveMetastoreTargetBuilder().build();

    TargetRunner runner = new TargetRunner.Builder(HiveMetastoreTarget.class, hiveTarget)
            .setOnRecordError(OnRecordError.TO_ERROR).build();
    runner.runInit();/*from   w  w w  .j  a  v a  2  s .co m*/

    LinkedHashMap<String, HiveTypeInfo> columns = new LinkedHashMap<>();
    columns.put("name", HiveType.STRING.getSupport().generateHiveTypeInfoFromResultSet("STRING"));

    LinkedHashMap<String, HiveTypeInfo> partitions = new LinkedHashMap<>();
    partitions.put("dt1", HiveType.STRING.getSupport().generateHiveTypeInfoFromResultSet("STRING"));
    partitions.put("dt2", HiveType.STRING.getSupport().generateHiveTypeInfoFromResultSet("STRING"));

    Field newTableField = HiveMetastoreUtil.newSchemaMetadataFieldBuilder("default", "tbl", columns, partitions,
            true, BaseHiveIT.getDefaultWareHouseDir(), HiveMetastoreUtil.generateAvroSchema(columns, "tbl"));

    Record record = RecordCreator.create();
    record.set(newTableField);
    Assert.assertTrue(HiveMetastoreUtil.isSchemaChangeRecord(record));

    runner.runWrite(ImmutableList.of(record));
    Assert.assertEquals("There should be no error records", 0, runner.getErrorRecords().size());

    //More Partitions (3) than configured
    LinkedHashMap<String, String> partitionVals = new LinkedHashMap<>();
    partitionVals.put("dt1", "2016");
    partitionVals.put("dt2", "2017");
    partitionVals.put("dt3", "2018");

    Field newPartitionField1 = HiveMetastoreUtil.newPartitionMetadataFieldBuilder("default", "tbl",
            partitionVals, "/user/hive/warehouse/tbl/dt1=2016/dt2=2017/dt3=2018");
    record = RecordCreator.create();
    record.set(newPartitionField1);
    runner.runWrite(ImmutableList.of(record));
    Assert.assertEquals("There should be one error record", 1, runner.getErrorRecords().size());
    Record errorRecord = runner.getErrorRecords().get(0);
    Assert.assertEquals(errorRecord.getHeader().getErrorCode(), Errors.HIVE_27.name());

    //Resetting the runner
    runner.getErrorRecords().clear();
    //Remove 3 partition names, less number of partitions than configured.
    partitionVals.remove("dt2");
    partitionVals.remove("dt3");

    Field newPartitionField2 = HiveMetastoreUtil.newPartitionMetadataFieldBuilder("default", "tbl",
            partitionVals, "/user/hive/warehouse/tbl/dt1=2016/dt2=2017/dt3=2018");

    record = RecordCreator.create();
    record.set(newPartitionField2);
    runner.runWrite(ImmutableList.of(record));
    Assert.assertEquals("There should be one error record", 1, runner.getErrorRecords().size());
    errorRecord = runner.getErrorRecords().get(0);
    Assert.assertEquals(errorRecord.getHeader().getErrorCode(), Errors.HIVE_27.name());
}

From source file:com.house365.build.util.CsvUtil.java

/**
 * ?CSV./*ww  w  . ja  v a2s. c  om*/
 *
 * @param channelsFile
 * @param headerRow
 * @param isPrint      ??..
 * @return ?Csv?
 * @throws FileNotFoundException
 */
public static ArrayList<LinkedHashMap<String, String>> readCsvChannels(File channelsFile, int headerRow,
        boolean isPrint) throws Exception {
    try {
        ArrayList<LinkedHashMap<String, String>> csvContent = new ArrayList<>();
        LinkedHashMap<String, String> rowContent;
        ArrayList<String> headerNames = null;
        if (headerRow > 0) {
            String[] csvHeader = getCsvHeader(channelsFile, headerRow);
            List<String> list = Arrays.asList(csvHeader);
            headerNames = new ArrayList<>(list);
        }
        AutoDetectReader reader = null;
        try {
            reader = new AutoDetectReader(new FileInputStream(channelsFile));
            Iterable<CSVRecord> csvRecords = CSVFormat.EXCEL.parse(reader);
            int i = 0;
            for (CSVRecord record : csvRecords) {
                i++;
                if (i <= headerRow) {
                    continue;
                }
                if (headerNames == null) {
                    headerNames = new ArrayList<>();
                    for (i = 0; i < record.size(); i++) {
                        headerNames.add(i + "");
                    }
                }
                rowContent = new LinkedHashMap<>();
                for (i = 0; i < record.size(); i++) {
                    rowContent.put(headerNames.get(i), record.get(i));
                }
                csvContent.add(rowContent);
            }
        } finally {
            try {
                if (reader != null)
                    reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (isPrint) {
            printlnCsv(csvContent);
        }
        return csvContent;
    } catch (IOException e) {
        throw e;
    } catch (TikaException e) {
        throw e;
    }
}

From source file:com.intel.iotkitlib.LibModules.RuleManagement.RuleManagement.java

public boolean updateStatusOfRule(String ruleId, String status) throws JSONException {
    if (ruleId == null || status == null) {
        Log.d(TAG, "rule id or status cannot be null");
        return false;
    }/*from  w ww. jav a2  s. c  o m*/
    String body;
    if ((body = createBodyForUpdateOfRuleStatus(status)) == null) {
        return false;
    }
    //initiating put for rule status update
    HttpPutTask createInvitation = new HttpPutTask(new HttpTaskHandler() {
        @Override
        public void taskResponse(int responseCode, String response) {
            Log.d(TAG, String.valueOf(responseCode));
            Log.d(TAG, response);
            statusHandler.readResponse(responseCode, response);
        }
    });
    createInvitation.setHeaders(basicHeaderList);
    createInvitation.setRequestBody(body);
    LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<String, String>();
    linkedHashMap.put("rule_id", ruleId);
    String url = objIotKit.prepareUrl(objIotKit.updateStatusOfRule, linkedHashMap);
    return super.invokeHttpExecuteOnURL(url, createInvitation, "update status of rule");
}

From source file:com.intel.iotkitlib.LibModules.RuleManagement.RuleManagement.java

public boolean updateARule(CreateRule updateRuleObj, String ruleId) throws JSONException {
    if (updateRuleObj == null) {
        Log.d(TAG, "rule Object cannot be null");
        return false;
    }//from   w w  w .  ja  v  a 2s .  c om
    String body;
    if ((body = createBodyForRuleCreation(updateRuleObj)) == null) {
        return false;
    }
    //initiating put for rule updation
    HttpPutTask updateRule = new HttpPutTask(new HttpTaskHandler() {
        @Override
        public void taskResponse(int responseCode, String response) {
            Log.d(TAG, String.valueOf(responseCode));
            Log.d(TAG, response);
            statusHandler.readResponse(responseCode, response);
        }
    });
    updateRule.setHeaders(basicHeaderList);
    updateRule.setRequestBody(body);
    LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<String, String>();
    linkedHashMap.put("rule_id", ruleId);
    String url = objIotKit.prepareUrl(objIotKit.updateRule, linkedHashMap);
    return super.invokeHttpExecuteOnURL(url, updateRule, "update a rule");
}

From source file:com.amazon.android.utils.JsonHelperTest.java

private Map<String, Object> getData2Map() {

    LinkedHashMap<String, Object> foo2 = new LinkedHashMap<>();
    LinkedHashMap<String, Object> foo1 = new LinkedHashMap<>();
    foo2.put("foo3", new LinkedHashMap<>());
    foo1.put("foo2", foo2);

    LinkedHashMap<String, Object> map = new LinkedHashMap<>();
    map.put("foo1", foo1);

    return map;/*from   w w  w. j  av  a2s.c  o m*/
}

From source file:com.qwazr.scripts.ScriptManager.java

Map<String, ScriptRunStatus> getRunsStatus() {
    runsMapLock.r.lock();//from  ww  w. ja v a 2s.  com
    try {
        LinkedHashMap<String, ScriptRunStatus> runStatusMap = new LinkedHashMap<String, ScriptRunStatus>();
        for (Map.Entry<String, RunThreadAbstract> entry : runsMap.entrySet())
            runStatusMap.put(entry.getKey(), entry.getValue().getStatus());
        return runStatusMap;
    } finally {
        runsMapLock.r.unlock();
    }
}

From source file:io.hops.hopsworks.api.jupyter.URITemplateProxyServlet.java

@Override
protected void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
        throws ServletException, IOException {

    //First collect params
    /*/*from w  ww.ja  v  a2s  . c o m*/
     * Do not use servletRequest.getParameter(arg) because that will
     * typically read and consume the servlet InputStream (where our
     * form data is stored for POST). We need the InputStream later on.
     * So we'll parse the query string ourselves. A side benefit is
     * we can keep the proxy parameters in the query string and not
     * have to add them to a URL encoded form attachment.
     */
    String queryString = "?" + servletRequest.getQueryString();//no "?" but might have "#"
    int hash = queryString.indexOf('#');
    if (hash >= 0) {
        queryString = queryString.substring(0, hash);
    }
    List<NameValuePair> pairs;
    try {
        //note: HttpClient 4.2 lets you parse the string without building the URI
        pairs = URLEncodedUtils.parse(new URI(queryString), "UTF-8");
    } catch (URISyntaxException e) {
        throw new ServletException("Unexpected URI parsing error on " + queryString, e);
    }
    LinkedHashMap<String, String> params = new LinkedHashMap<>();
    for (NameValuePair pair : pairs) {
        params.put(pair.getName(), pair.getValue());
    }

    //Now rewrite the URL
    StringBuffer urlBuf = new StringBuffer();//note: StringBuilder isn't supported by Matcher
    Matcher matcher = TEMPLATE_PATTERN.matcher(targetUriTemplate);
    while (matcher.find()) {
        String arg = matcher.group(1);
        String replacement = params.remove(arg);//note we remove
        if (replacement != null) {
            matcher.appendReplacement(urlBuf, replacement);
            port = replacement;
        } else if (port != null) {
            matcher.appendReplacement(urlBuf, port);
        } else {
            throw new ServletException("Missing HTTP parameter " + arg + " to fill the template");
        }
    }
    matcher.appendTail(urlBuf);
    String newTargetUri = urlBuf.toString();
    servletRequest.setAttribute(ATTR_TARGET_URI, newTargetUri);
    try {
        targetUriObj = new URI(newTargetUri);
    } catch (Exception e) {
        throw new ServletException("Rewritten targetUri is invalid: " + newTargetUri, e);
    }
    servletRequest.setAttribute(ATTR_TARGET_HOST, URIUtils.extractHost(targetUriObj));

    //Determine the new query string based on removing the used names
    StringBuilder newQueryBuf = new StringBuilder(queryString.length());
    for (Map.Entry<String, String> nameVal : params.entrySet()) {
        if (newQueryBuf.length() > 0) {
            newQueryBuf.append('&');
        }
        newQueryBuf.append(nameVal.getKey()).append('=');
        if (nameVal.getValue() != null) {
            newQueryBuf.append(nameVal.getValue());
        }
    }
    servletRequest.setAttribute(ATTR_QUERY_STRING, newQueryBuf.toString());

    // Create Exchange object with targetUriObj
    // create transport object
    //    ServiceProxy sp = new ServiceProxy();
    //    sp.setTargetUrl(ATTR_TARGET_URI);
    //    super.service(servletRequest, servletResponse);

    //              RouterUtil.initializeRoutersFromSpringWebContext(appCtx, config.
    //              getServletContext(), getProxiesXmlLocation(config));
}

From source file:org.kitodo.sruimport.SRUImport.java

private SearchResult search(String catalogId, Map<String, String> searchParameters, int numberOfRecords) {
    // TODO: check how the fields of hits from SRU interfaces can be configured via CQL (need only title and id!)
    loadOPACConfiguration(catalogId);/*  ww w  .  j  a  va  2 s .  c om*/
    if (searchFieldMapping.keySet().containsAll(searchParameters.keySet())) {

        // Query parameters for HTTP request
        LinkedHashMap<String, String> queryParameters = new LinkedHashMap<>(parameters);

        // Search fields and terms of query
        LinkedHashMap<String, String> searchFieldMap = new LinkedHashMap<>();
        for (Map.Entry<String, String> entry : searchParameters.entrySet()) {
            searchFieldMap.put(searchFieldMapping.get(entry.getKey()), entry.getValue());
        }

        try {
            URI queryURL = createQueryURI(queryParameters);
            return performQuery(queryURL.toString() + "&maximumRecords=" + numberOfRecords + "&query="
                    + createSearchFieldString(searchFieldMap));
        } catch (URISyntaxException | UnsupportedEncodingException e) {
            logger.error(e.getLocalizedMessage());
        }
    }
    return null;
}

From source file:org.jasig.portlet.conference.program.dao.ConferenceSessionDao.java

@Scheduled(fixedRate = 900000)
protected void retrieveProgram() {
    log.debug("Requesting program data from " + this.programUrl);
    final ConferenceProgram program = restTemplate.getForObject(programUrl, ConferenceProgram.class,
            Collections.<String, String>emptyMap());

    if (program != null) {
        cache.put(new Element(PROGRAM_CACHE_KEY, program));

        final List<String> tracks = new ArrayList<String>();
        final List<String> types = new ArrayList<String>();
        final List<String> levels = new ArrayList<String>();
        final List<DateMidnight> dates = new ArrayList<DateMidnight>();

        final DateTimeFormatter providedDF = new DateTimeFormatterBuilder().appendPattern("dd-MMM-yyyy")
                .toFormatter();/*ww w.  ja v a 2 s. co m*/
        final DateTimeFormatter displayDF = new DateTimeFormatterBuilder().appendPattern("EEE MMMM d")
                .toFormatter();
        for (final ConferenceSession session : getProgram().getSessions()) {
            final String track = session.getTrack();
            if (shouldAddToList(track, tracks)) {
                tracks.add(track);
            }

            final String type = session.getType();
            if (shouldAddToList(type, types)) {
                types.add(type);
            }

            final String level = session.getLevel();
            if (shouldAddToList(level, levels)) {
                levels.add(level);
            }

            final String value = session.getDate();
            final DateMidnight date = providedDF.parseDateTime(value).toDateMidnight();
            if (!dates.contains(date)) {
                dates.add(date);
            }

        }
        Collections.sort(tracks);
        Collections.sort(levels);
        Collections.sort(types);

        Collections.sort(dates);
        LinkedHashMap<String, String> dateMap = new LinkedHashMap<String, String>();
        for (DateMidnight date : dates) {
            dateMap.put(providedDF.print(date), displayDF.print(date));
        }

        cache.put(new Element(PROGRAM_CACHE_KEY, program));
        cache.put(new Element(TRACK_LIST_KEY, tracks));
        cache.put(new Element(LEVEL_LIST_KEY, levels));
        cache.put(new Element(TYPE_LIST_KEY, types));
        cache.put(new Element(DATE_MAP_KEY, dateMap));

    }

}

From source file:ca.sfu.federation.model.util.Selection.java

/**
 * Parse the input Query.//from  w  ww  . j  av  a2s.c  o m
 * @param Query A selection Query given in the selection syntax.
 * @param MyContext The context in which the selection operation should occur.
 * @throws IllegalArgumentException sThe selection query is not well formed.
 */
private void parse(String Query, IContext MyContext) throws IllegalArgumentException {
    // init
    this.rule = Query.trim();
    // split rule string into atoms; statement should be atleast 4 atoms long to be valid
    // ex. SELECT * WHERE name=*
    String[] atoms = rule.split(" ");
    if (atoms.length < 4) {
        throw new IllegalArgumentException("Required SELECT or WHERE directives missing.");
    }
    // SELECT directive must be the first atom, WHERE must be third
    if (!atoms[0].equals("SELECT") && !atoms[2].equals("WHERE")) {
        throw new IllegalArgumentException("Required SELECT or WHERE directive malformed.");
    }
    // SCOPE (aka context) is second atom; try to get the collection of objects in the scope
    this.scope = atoms[1].trim();
    if (this.scope.equals("*")) {
        this.scopeobjects = (LinkedHashMap) MyContext.getElementMap();
    } else {
        // scope not in the current context, so try to get the named context
        int selectall = this.scope.lastIndexOf(".*");
        if (selectall != -1) {
            scope = this.scope.substring(0, selectall);
        }
        try {
            Object obj = MyContext.lookup(scope);
            if (selectall != -1) {
                if (obj instanceof IContext) {
                    IContext context = (IContext) obj;
                    this.scopeobjects = (LinkedHashMap) context.getElementMap();
                } else {
                    // TODO: can not do a subselection on this object. throw error
                }
            } else {
                if (obj instanceof INamed) {
                    INamed named = (INamed) obj;
                    LinkedHashMap objs = new LinkedHashMap();
                    objs.put(named.getName(), named);
                    this.scopeobjects = objs;
                } else {
                    // TODO: reference must have been to a property, so throw an error
                }
            }
        } catch (IllegalArgumentException ex) {
            String stack = ExceptionUtils.getFullStackTrace(ex);
            logger.log(Level.WARNING, "{0}", stack);
        }
    }
    // CONDITIONS go from third atom to the next directive in the list, if one is present
    // The tree is built in units of Boolean statements, from the leaf nodes to the root.  
    // The last statement in the input string becomes the root of the tree.

    // identify the subsegment of the query that has the conditions and post processing directives
    ArrayList conditionsv = new ArrayList();
    ArrayList postprocessingv = new ArrayList();
    int index = 3; // start from WHERE directive
    boolean found = false;
    while (!found && index < atoms.length) {
        int in = 0;
        while (!found && in < Selection.POSTPROCESSING_DIRECTIVES.length) {
            if (Selection.POSTPROCESSING_DIRECTIVES[in].equals(atoms[index])) {
                found = true;
            }
            in++;
        }
        if (!found) {
            index++;
        }
    }
    for (int i = 3; i < index && i < atoms.length; i++) {
        conditionsv.add(atoms[i]);
    }
    String[] conditions = new String[conditionsv.size()];
    conditionsv.toArray(conditions);

    // Build the selection filter tree from the conditions list
    found = false;
    int j = 0;
    if (conditions.length == 1) {
        // single condition
        this.filter = new SelectionFilter(conditions[0]);
    } else {
        // multiple conditions
        while (j < conditions.length) {
            // find the first boolean statement in the remaining input string.  
            // the elements to the left and right become the leaf nodes of the first tree.
            String statement = conditions[j];
            boolean isBool = SelectionFilter.isBoolean(statement);
            if (isBool && this.filter == null) {
                // this is the first boolean statement in the list
                String[] sub = new String[3];
                sub[0] = conditions[j - 1];
                sub[1] = conditions[j];
                sub[2] = conditions[j + 1];
                this.filter = new SelectionFilter(sub);
                j += 2;
            } else if (isBool) {
                // any following boolean statement
                String[] sub = new String[2];
                sub[0] = conditions[j];
                sub[1] = conditions[j + 1];
                this.filter = new SelectionFilter(this.filter, sub);
                j += 2;
            }
            j++;
        }
    }

    // POST-PROCESSING DIRECTIVES follow the selection conditions.
    // identify the subsegment of the query that has post processing directives
    for (int i = index; i < atoms.length; i++) {
        postprocessingv.add(atoms[i]);
    }
    String[] postprocessing = new String[postprocessingv.size()];
    postprocessingv.toArray(postprocessing);
    // TODO: post processing
}