Java Code Examples for org.codehaus.jackson.JsonNode#getFieldNames()
The following examples show how to use
org.codehaus.jackson.JsonNode#getFieldNames() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InjectConfigTest.java From attic-apex-core with Apache License 2.0 | 6 votes |
public static JsonNode merge(JsonNode mainNode, JsonNode updateNode) { Iterator<String> fieldNames = updateNode.getFieldNames(); while (fieldNames.hasNext()) { String fieldName = fieldNames.next(); JsonNode jsonNode = mainNode.get(fieldName); // if field doesn't exist or is an embedded object if (jsonNode != null && jsonNode.isObject()) { merge(jsonNode, updateNode.get(fieldName)); } else { if (mainNode instanceof ObjectNode) { // Overwrite field JsonNode value = updateNode.get(fieldName); ((ObjectNode)mainNode).put(fieldName, value); } } } return mainNode; }
Example 2
Source File: MySQLConfigSchema.java From pinlater with Apache License 2.0 | 6 votes |
public static MySQLConfigSchema read(InputStream json, String shardNamePrefix) throws Exception { JsonNode shardsJson = OBJECT_MAPPER.readTree(json); MySQLConfigSchema mySQLConfigSchema = new MySQLConfigSchema(); mySQLConfigSchema.shards = new ArrayList<Shard>(); Iterator<String> shardNameIter = shardsJson.getFieldNames(); while (shardNameIter.hasNext()) { String shardName = shardNameIter.next(); if (shardName.startsWith(shardNamePrefix)) { Shard shard = new Shard(); shard.id = Integer.parseInt(shardName.replaceFirst(shardNamePrefix, "")); shard.shardConfig = OBJECT_MAPPER.readValue(shardsJson.get(shardName), ShardConfig.class); mySQLConfigSchema.shards.add(shard); } } return mySQLConfigSchema; }
Example 3
Source File: ConvertJSONToSQL.java From localization_nifi with Apache License 2.0 | 5 votes |
private Set<String> getNormalizedColumnNames(final JsonNode node, final boolean translateFieldNames) { final Set<String> normalizedFieldNames = new HashSet<>(); final Iterator<String> fieldNameItr = node.getFieldNames(); while (fieldNameItr.hasNext()) { normalizedFieldNames.add(normalizeColumnName(fieldNameItr.next(), translateFieldNames)); } return normalizedFieldNames; }
Example 4
Source File: YobitExchange.java From libdynticker with GNU Lesser General Public License v3.0 | 5 votes |
@Override protected List<Pair> getPairsFromAPI() throws IOException { List<Pair> pairs = new ArrayList<Pair>(); JsonNode elements = readJsonFromUrl("https://yobit.net/api/3/info").get("pairs"); Iterator<String> fieldNames = elements.getFieldNames(); String[] split; String element; while(fieldNames.hasNext()) { element = fieldNames.next(); split = element.split("_"); pairs.add(new Pair(split[0].toUpperCase(), split[1].toUpperCase())); } return pairs; }
Example 5
Source File: TezFetcher.java From dr-elephant with Apache License 2.0 | 5 votes |
private Properties getProperties(URL url) throws IOException, AuthenticationException { Properties jobConf = new Properties(); JsonNode rootNode = ThreadContextMR2.readJsonNode(url); JsonNode configs = rootNode.path("otherinfo").path("config"); Iterator<String> keys = configs.getFieldNames(); String key = ""; String value = ""; while (keys.hasNext()) { key = keys.next(); value = configs.get(key).getTextValue(); jobConf.put(key, value); } return jobConf; }
Example 6
Source File: TBEExchange.java From libdynticker with GNU Lesser General Public License v3.0 | 5 votes |
@Override protected List<Pair> getPairsFromAPI() throws IOException { List<Pair> pairs = new ArrayList<Pair>(); JsonNode node = readJsonFromUrl("https://bx.in.th/api/pairing/"); Iterator<String> fieldNames = node.getFieldNames(); while(fieldNames.hasNext()) { String id = fieldNames.next(); pairs.add(new Pair(node.get(id).get("secondary_currency").asText(), node.get(id).get("primary_currency").asText(), id)); } return pairs; }
Example 7
Source File: SemanticAnalyzer.java From Cubert with Apache License 2.0 | 5 votes |
@Override public void enterProgram(JsonNode json) { if (json.has("input") && !json.get("input").isNull()) { JsonNode programInput = json.get("input"); Iterator<String> it = programInput.getFieldNames(); while (it.hasNext()) { String input = it.next(); JsonNode inputJson = programInput.get(input); BlockSchema schema = new BlockSchema(inputJson.get("schema")); PostCondition condition = null; if (inputJson.has("partitionKeys") && inputJson.has("sortKeys")) condition = new PostCondition(schema, JsonUtils.asArray(inputJson.get("partitionKeys")), JsonUtils.asArray(inputJson.get("sortKeys"))); else condition = new PostCondition(schema, null, null); datasetConditions.put(input, condition); } } }
Example 8
Source File: BlockgenLineageAnalyzer.java From Cubert with Apache License 2.0 | 5 votes |
@Override public JsonNode rewrite(JsonNode plan, Set<String> namesUsed, boolean debugMode, boolean revisit) throws IOException { conf = new JobConf(); // first get the blockgen id from global input cubert files JsonNode inputs = plan.get("input"); if (inputs != null && !inputs.isNull()) { Iterator<String> inputsIt = inputs.getFieldNames(); while (inputsIt.hasNext()) { String input = inputsIt.next(); JsonNode json = inputs.get(input); String type = getText(json, "type"); if (type.equalsIgnoreCase("RUBIX")) { try { blockgenIdMap.put(input, getBlockgenId(input)); } catch (ClassNotFoundException e) { throw new PlanRewriteException(e); } } } } new PhysicalPlanWalker(plan, this).walk(); return plan; }
Example 9
Source File: JobExecutor.java From Cubert with Apache License 2.0 | 5 votes |
protected void setHadoopConf() { if (!root.has("hadoopConf")) return; JsonNode node = get(root, "hadoopConf"); Iterator<String> it = node.getFieldNames(); while (it.hasNext()) { String name = it.next(); String value = getText(node, name); conf.set(name, value); } }
Example 10
Source File: ConvertJSONToSQL.java From nifi with Apache License 2.0 | 5 votes |
private Set<String> getNormalizedColumnNames(final JsonNode node, final boolean translateFieldNames) { final Set<String> normalizedFieldNames = new HashSet<>(); final Iterator<String> fieldNameItr = node.getFieldNames(); while (fieldNameItr.hasNext()) { normalizedFieldNames.add(normalizeColumnName(fieldNameItr.next(), translateFieldNames)); } return normalizedFieldNames; }
Example 11
Source File: LocalBitcoinsExchange.java From libdynticker with GNU Lesser General Public License v3.0 | 5 votes |
@Override protected List<Pair> getPairsFromAPI() throws IOException { JsonNode node = readJsonFromUrl("https://localbitcoins.com/bitcoinaverage/ticker-all-currencies/"); List<Pair> pairs = new ArrayList<Pair>(); Iterator<String> exchanges = node.getFieldNames(); while(exchanges.hasNext()) { // LocalBitcoins only deals with BTC to others currencies pairs.add(new Pair("BTC", exchanges.next())); } return pairs; }
Example 12
Source File: CustomFieldDeSerializer.java From jira-rest-client with Apache License 2.0 | 5 votes |
@Override public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { logger.info("Deserialize..."); ObjectCodec oc = jp.getCodec(); JsonNode node = oc.readTree(jp); for (int i = 0; i < node.size(); i++) { JsonNode child = node.get(i); if (child == null) { logger.info(i + "th Child node is null"); continue; } //String if (child.isTextual()) { Iterator<String> it = child.getFieldNames(); while (it.hasNext()) { String field = it.next(); logger.info("in while loop " + field); if (field.startsWith("customfield")) { } } } } return null; }
Example 13
Source File: AbstractSiteToSiteReportingTask.java From nifi with Apache License 2.0 | 4 votes |
protected Object getRawNodeValue(final JsonNode fieldNode, final DataType dataType) throws IOException { if (fieldNode == null || fieldNode.isNull()) { return null; } if (fieldNode.isNumber()) { return fieldNode.getNumberValue(); } if (fieldNode.isBinary()) { return fieldNode.getBinaryValue(); } if (fieldNode.isBoolean()) { return fieldNode.getBooleanValue(); } if (fieldNode.isTextual()) { return fieldNode.getTextValue(); } if (fieldNode.isArray()) { final ArrayNode arrayNode = (ArrayNode) fieldNode; final int numElements = arrayNode.size(); final Object[] arrayElements = new Object[numElements]; int count = 0; final DataType elementDataType; if (dataType != null && dataType.getFieldType() == RecordFieldType.ARRAY) { final ArrayDataType arrayDataType = (ArrayDataType) dataType; elementDataType = arrayDataType.getElementType(); } else { elementDataType = null; } for (final JsonNode node : arrayNode) { final Object value = getRawNodeValue(node, elementDataType); arrayElements[count++] = value; } return arrayElements; } if (fieldNode.isObject()) { RecordSchema childSchema; if (dataType != null && RecordFieldType.RECORD == dataType.getFieldType()) { final RecordDataType recordDataType = (RecordDataType) dataType; childSchema = recordDataType.getChildSchema(); } else { childSchema = null; } if (childSchema == null) { childSchema = new SimpleRecordSchema(Collections.emptyList()); } final Iterator<String> fieldNames = fieldNode.getFieldNames(); final Map<String, Object> childValues = new HashMap<>(); while (fieldNames.hasNext()) { final String childFieldName = fieldNames.next(); final Object childValue = getRawNodeValue(fieldNode.get(childFieldName), dataType); childValues.put(childFieldName, childValue); } final MapRecord record = new MapRecord(childSchema, childValues); return record; } return null; }
Example 14
Source File: ConvertJSONToSQL.java From nifi with Apache License 2.0 | 4 votes |
private String generateDelete(final JsonNode rootNode, final Map<String, String> attributes, final String tableName, final TableSchema schema, final boolean translateFieldNames, final boolean ignoreUnmappedFields, final boolean failUnmappedColumns, final boolean warningUnmappedColumns, boolean escapeColumnNames, boolean quoteTableName, final String attributePrefix) { final Set<String> normalizedFieldNames = getNormalizedColumnNames(rootNode, translateFieldNames); for (final String requiredColName : schema.getRequiredColumnNames()) { final String normalizedColName = normalizeColumnName(requiredColName, translateFieldNames); if (!normalizedFieldNames.contains(normalizedColName)) { String missingColMessage = "JSON does not have a value for the Required column '" + requiredColName + "'"; if (failUnmappedColumns) { getLogger().error(missingColMessage); throw new ProcessException(missingColMessage); } else if (warningUnmappedColumns) { getLogger().warn(missingColMessage); } } } final StringBuilder sqlBuilder = new StringBuilder(); int fieldCount = 0; sqlBuilder.append("DELETE FROM "); if (quoteTableName) { sqlBuilder.append(schema.getQuotedIdentifierString()) .append(tableName) .append(schema.getQuotedIdentifierString()); } else { sqlBuilder.append(tableName); } sqlBuilder.append(" WHERE "); // iterate over all of the elements in the JSON, building the SQL statement by adding the column names, as well as // adding the column value to a "<sql>.args.N.value" attribute and the type of a "<sql>.args.N.type" attribute add the // columns that we are inserting into final Iterator<String> fieldNames = rootNode.getFieldNames(); while (fieldNames.hasNext()) { final String fieldName = fieldNames.next(); final ColumnDescription desc = schema.getColumns().get(normalizeColumnName(fieldName, translateFieldNames)); if (desc == null && !ignoreUnmappedFields) { throw new ProcessException("Cannot map JSON field '" + fieldName + "' to any column in the database"); } if (desc != null) { if (fieldCount++ > 0) { sqlBuilder.append(" AND "); } if (escapeColumnNames) { sqlBuilder.append(schema.getQuotedIdentifierString()) .append(desc.getColumnName()) .append(schema.getQuotedIdentifierString()); } else { sqlBuilder.append(desc.getColumnName()); } sqlBuilder.append(" = ?"); final int sqlType = desc.getDataType(); attributes.put(attributePrefix + ".args." + fieldCount + ".type", String.valueOf(sqlType)); final Integer colSize = desc.getColumnSize(); final JsonNode fieldNode = rootNode.get(fieldName); if (!fieldNode.isNull()) { String fieldValue = fieldNode.asText(); if (colSize != null && fieldValue.length() > colSize) { fieldValue = fieldValue.substring(0, colSize); } attributes.put(attributePrefix + ".args." + fieldCount + ".value", fieldValue); } } } if (fieldCount == 0) { throw new ProcessException("None of the fields in the JSON map to the columns defined by the " + tableName + " table"); } return sqlBuilder.toString(); }
Example 15
Source File: WorkFlowService.java From FoxBPM with Apache License 2.0 | 4 votes |
public Object executeTaskCommandJson(Map<String,Object> formData) { String taskCommandJson = StringUtil.getString(formData.get("flowCommandInfo")); Map<String,Object> transVariable = new HashMap<String, Object>(); transVariable.put("formData", formData); ObjectMapper objectMapper = new ObjectMapper(); JsonNode params = null; try { params = objectMapper.readTree(taskCommandJson); } catch (Exception e) { throw new FoxBPMException("任务命令参数格式不正确",e); } JsonNode taskIdNode = params.get("taskId"); JsonNode commandIdNode = params.get("commandId"); JsonNode processDefinitionKeyNode = params.get("processDefinitionKey"); JsonNode businessKeyNode = params.get("bizKey"); JsonNode taskCommentNode = params.get("taskComment"); // 参数校验 // 命令类型 JsonNode commandTypeNode = params.get("commandType"); JsonNode commandParamsNode = params.get("commandParams"); if (commandTypeNode == null) { throw new FoxBPMException("commandType is null !"); } // 命令Id if (commandIdNode == null) { throw new FoxBPMException("commandId is null !"); } ExpandTaskCommand expandTaskCommand = new ExpandTaskCommand(); expandTaskCommand.setCommandType(commandTypeNode.getTextValue()); // 设置命令的id,需和节点上配置的按钮编号对应,会执行按钮中的脚本。 expandTaskCommand.setTaskCommandId(commandIdNode.getTextValue()); if(taskCommentNode != null){ expandTaskCommand.setTaskComment(taskCommentNode.getTextValue()); } expandTaskCommand.setTransientVariables(transVariable); //设置任务命令参数 Map<String,Object> taskParams = new HashMap<String, Object>(); if(commandParamsNode != null){ Iterator<String> it = commandParamsNode.getFieldNames(); while(it.hasNext()){ String tmp = it.next(); taskParams.put(tmp, commandParamsNode.get(tmp).getTextValue()); } } expandTaskCommand.setParamMap(taskParams); if (taskIdNode != null && StringUtil.isNotEmpty(taskIdNode.getTextValue())&& !StringUtil.equals(taskIdNode.getTextValue(),"null")) { expandTaskCommand.setTaskId(taskIdNode.getTextValue()); } else { String userId = Authentication.getAuthenticatedUserId(); expandTaskCommand.setInitiator(userId); if(businessKeyNode == null){ throw new RuntimeException("启动流程时关联键不能为null"); } if(processDefinitionKeyNode == null){ throw new RuntimeException("启动流程时流程Key不能为null"); } expandTaskCommand.setBusinessKey(businessKeyNode.getTextValue()); expandTaskCommand.setProcessDefinitionKey(processDefinitionKeyNode.getTextValue()); } return taskService.expandTaskComplete(expandTaskCommand, Object.class); }
Example 16
Source File: ConvertJSONToSQL.java From nifi with Apache License 2.0 | 4 votes |
private String generateInsert(final JsonNode rootNode, final Map<String, String> attributes, final String tableName, final TableSchema schema, final boolean translateFieldNames, final boolean ignoreUnmappedFields, final boolean failUnmappedColumns, final boolean warningUnmappedColumns, boolean escapeColumnNames, boolean quoteTableName, final String attributePrefix) { final Set<String> normalizedFieldNames = getNormalizedColumnNames(rootNode, translateFieldNames); for (final String requiredColName : schema.getRequiredColumnNames()) { final String normalizedColName = normalizeColumnName(requiredColName, translateFieldNames); if (!normalizedFieldNames.contains(normalizedColName)) { String missingColMessage = "JSON does not have a value for the Required column '" + requiredColName + "'"; if (failUnmappedColumns) { getLogger().error(missingColMessage); throw new ProcessException(missingColMessage); } else if (warningUnmappedColumns) { getLogger().warn(missingColMessage); } } } final StringBuilder sqlBuilder = new StringBuilder(); int fieldCount = 0; sqlBuilder.append("INSERT INTO "); if (quoteTableName) { sqlBuilder.append(schema.getQuotedIdentifierString()) .append(tableName) .append(schema.getQuotedIdentifierString()); } else { sqlBuilder.append(tableName); } sqlBuilder.append(" ("); // iterate over all of the elements in the JSON, building the SQL statement by adding the column names, as well as // adding the column value to a "<sql>.args.N.value" attribute and the type of a "<sql>.args.N.type" attribute add the // columns that we are inserting into final Iterator<String> fieldNames = rootNode.getFieldNames(); while (fieldNames.hasNext()) { final String fieldName = fieldNames.next(); final ColumnDescription desc = schema.getColumns().get(normalizeColumnName(fieldName, translateFieldNames)); if (desc == null && !ignoreUnmappedFields) { throw new ProcessException("Cannot map JSON field '" + fieldName + "' to any column in the database"); } if (desc != null) { if (fieldCount++ > 0) { sqlBuilder.append(", "); } if(escapeColumnNames){ sqlBuilder.append(schema.getQuotedIdentifierString()) .append(desc.getColumnName()) .append(schema.getQuotedIdentifierString()); } else { sqlBuilder.append(desc.getColumnName()); } final int sqlType = desc.getDataType(); attributes.put(attributePrefix + ".args." + fieldCount + ".type", String.valueOf(sqlType)); final Integer colSize = desc.getColumnSize(); final JsonNode fieldNode = rootNode.get(fieldName); if (!fieldNode.isNull()) { String fieldValue = createSqlStringValue(fieldNode, colSize, sqlType); attributes.put(attributePrefix + ".args." + fieldCount + ".value", fieldValue); } } } // complete the SQL statements by adding ?'s for all of the values to be escaped. sqlBuilder.append(") VALUES ("); for (int i=0; i < fieldCount; i++) { if (i > 0) { sqlBuilder.append(", "); } sqlBuilder.append("?"); } sqlBuilder.append(")"); if (fieldCount == 0) { throw new ProcessException("None of the fields in the JSON map to the columns defined by the " + tableName + " table"); } return sqlBuilder.toString(); }
Example 17
Source File: DictionaryDecodeOperator.java From Cubert with Apache License 2.0 | 4 votes |
@Override public void setInput(Map<String, Block> input, JsonNode json, BlockProperties props) throws IOException, InterruptedException { // Get the dictionary Map<String, CodeDictionary> dictionaryMap = null; if (json.has("path")) { // load the dictionary from file String dictionaryName = json.get("path").getTextValue(); String dictionaryPath = FileCache.get(dictionaryName); dictionaryPath = dictionaryPath + "/part-r-00000.avro"; dictionaryMap = GenerateDictionary.loadDictionary(dictionaryPath, false, null); } else { // this is inline dictionary JsonNode dictionary = json.get("dictionary"); Iterator<String> nameIterator = dictionary.getFieldNames(); dictionaryMap = new HashMap<String, CodeDictionary>(); while (nameIterator.hasNext()) { String name = nameIterator.next(); ArrayNode values = (ArrayNode) dictionary.get(name); CodeDictionary codeDictionary = new CodeDictionary(); for (JsonNode value : values) { codeDictionary.addKey(value.getTextValue()); } dictionaryMap.put(name, codeDictionary); } } dataBlock = input.values().iterator().next(); BlockSchema inputSchema = dataBlock.getProperties().getSchema(); numColumns = inputSchema.getNumColumns(); decodedTuple = TupleFactory.getInstance().newTuple(numColumns); // create dictionary array dictionaries = new CodeDictionary[numColumns]; for (int i = 0; i < numColumns; i++) { String colName = inputSchema.getName(i); if (dictionaryMap.containsKey(colName)) { dictionaries[i] = dictionaryMap.get(colName); } else { dictionaries[i] = null; } } if (json.has("replaceUnknownCodes")) { replaceUnknownCodes = JsonUtils.getText(json, "replaceUnknownCodes"); } }
Example 18
Source File: DictionaryEncodeOperator.java From Cubert with Apache License 2.0 | 4 votes |
@Override public void setInput(Map<String, Block> input, JsonNode json, BlockProperties props) throws IOException, InterruptedException { // load the dictionaries Map<String, CodeDictionary> dictionaryMap = null; if (json.has("path")) { String dictionaryName = json.get("path").getTextValue(); String dictionaryPath = FileCache.get(dictionaryName); dictionaryPath = dictionaryPath + "/part-r-00000.avro"; dictionaryMap = GenerateDictionary.loadDictionary(dictionaryPath, false, null); } else { // this is inline dictionary JsonNode dictionary = json.get("dictionary"); Iterator<String> nameIterator = dictionary.getFieldNames(); dictionaryMap = new HashMap<String, CodeDictionary>(); while (nameIterator.hasNext()) { String name = nameIterator.next(); ArrayNode values = (ArrayNode) dictionary.get(name); CodeDictionary codeDictionary = new CodeDictionary(); for (JsonNode value : values) { codeDictionary.addKey(value.getTextValue()); } dictionaryMap.put(name, codeDictionary); } } dataBlock = input.values().iterator().next(); BlockSchema inputSchema = dataBlock.getProperties().getSchema(); // assign the dictionaries in the array dictionaries = new CodeDictionary[inputSchema.getNumColumns()]; for (int i = 0; i < dictionaries.length; i++) { String colName = inputSchema.getName(i); if (dictionaryMap.containsKey(colName)) { dictionaries[i] = dictionaryMap.get(colName); } else { dictionaries[i] = null; } } // create output tuple encodedTuple = TupleFactory.getInstance().newTuple(inputSchema.getNumColumns()); if (json.has("replaceNull")) { replaceNull = JsonUtils.getText(json, "replaceNull"); } if (json.has("replaceUnknownCodes")) { replaceUnknownCodes = Integer.parseInt(JsonUtils.getText(json, "replaceUnknownCodes")); } }
Example 19
Source File: PutHBaseJSON.java From localization_nifi with Apache License 2.0 | 4 votes |
@Override protected PutFlowFile createPut(final ProcessSession session, final ProcessContext context, final FlowFile flowFile) { final String tableName = context.getProperty(TABLE_NAME).evaluateAttributeExpressions(flowFile).getValue(); final String rowId = context.getProperty(ROW_ID).evaluateAttributeExpressions(flowFile).getValue(); final String rowFieldName = context.getProperty(ROW_FIELD_NAME).evaluateAttributeExpressions(flowFile).getValue(); final String columnFamily = context.getProperty(COLUMN_FAMILY).evaluateAttributeExpressions(flowFile).getValue(); final boolean extractRowId = !StringUtils.isBlank(rowFieldName); final String complexFieldStrategy = context.getProperty(COMPLEX_FIELD_STRATEGY).getValue(); final String fieldEncodingStrategy = context.getProperty(FIELD_ENCODING_STRATEGY).getValue(); final String rowIdEncodingStrategy = context.getProperty(ROW_ID_ENCODING_STRATEGY).getValue(); // Parse the JSON document final ObjectMapper mapper = new ObjectMapper(); final AtomicReference<JsonNode> rootNodeRef = new AtomicReference<>(null); try { session.read(flowFile, new InputStreamCallback() { @Override public void process(final InputStream in) throws IOException { try (final InputStream bufferedIn = new BufferedInputStream(in)) { rootNodeRef.set(mapper.readTree(bufferedIn)); } } }); } catch (final ProcessException pe) { getLogger().error("Failed to parse {} as JSON due to {}; routing to failure", new Object[]{flowFile, pe.toString()}, pe); return null; } final JsonNode rootNode = rootNodeRef.get(); if (rootNode.isArray()) { getLogger().error("Root node of JSON must be a single document, found array for {}; routing to failure", new Object[]{flowFile}); return null; } final Collection<PutColumn> columns = new ArrayList<>(); final AtomicReference<String> rowIdHolder = new AtomicReference<>(null); // convert each field/value to a column for the put, skip over nulls and arrays final Iterator<String> fieldNames = rootNode.getFieldNames(); while (fieldNames.hasNext()) { final String fieldName = fieldNames.next(); final AtomicReference<byte[]> fieldValueHolder = new AtomicReference<>(null); final JsonNode fieldNode = rootNode.get(fieldName); if (fieldNode.isNull()) { getLogger().debug("Skipping {} because value was null", new Object[]{fieldName}); } else if (fieldNode.isValueNode()) { // for a value node we need to determine if we are storing the bytes of a string, or the bytes of actual types if (STRING_ENCODING_VALUE.equals(fieldEncodingStrategy)) { final byte[] valueBytes = clientService.toBytes(fieldNode.asText()); fieldValueHolder.set(valueBytes); } else { fieldValueHolder.set(extractJNodeValue(fieldNode)); } } else { // for non-null, non-value nodes, determine what to do based on the handling strategy switch (complexFieldStrategy) { case FAIL_VALUE: getLogger().error("Complex value found for {}; routing to failure", new Object[]{fieldName}); return null; case WARN_VALUE: getLogger().warn("Complex value found for {}; skipping", new Object[]{fieldName}); break; case TEXT_VALUE: // use toString() here because asText() is only guaranteed to be supported on value nodes // some other types of nodes, like ArrayNode, provide toString implementations fieldValueHolder.set(clientService.toBytes(fieldNode.toString())); break; case IGNORE_VALUE: // silently skip break; default: break; } } // if we have a field value, then see if this is the row id field, if so store the value for later // otherwise add a new column where the fieldName and fieldValue are the column qualifier and value if (fieldValueHolder.get() != null) { if (extractRowId && fieldName.equals(rowFieldName)) { rowIdHolder.set(fieldNode.asText()); } else { columns.add(new PutColumn(columnFamily.getBytes(StandardCharsets.UTF_8), fieldName.getBytes(StandardCharsets.UTF_8), fieldValueHolder.get())); } } } // if we are expecting a field name to use for the row id and the incoming document doesn't have it // log an error message so the user can see what the field names were and return null so it gets routed to failure if (extractRowId && rowIdHolder.get() == null) { final String fieldNameStr = StringUtils.join(rootNode.getFieldNames(), ","); getLogger().error("Row ID field named '{}' not found in field names '{}'; routing to failure", new Object[] {rowFieldName, fieldNameStr}); return null; } final String putRowId = (extractRowId ? rowIdHolder.get() : rowId); byte[] rowKeyBytes = getRow(putRowId,context.getProperty(ROW_ID_ENCODING_STRATEGY).getValue()); return new PutFlowFile(tableName, rowKeyBytes, columns, flowFile); }
Example 20
Source File: ConvertJSONToSQL.java From localization_nifi with Apache License 2.0 | 4 votes |
private String generateInsert(final JsonNode rootNode, final Map<String, String> attributes, final String tableName, final TableSchema schema, final boolean translateFieldNames, final boolean ignoreUnmappedFields, final boolean failUnmappedColumns, final boolean warningUnmappedColumns, boolean escapeColumnNames, boolean quoteTableName) { final Set<String> normalizedFieldNames = getNormalizedColumnNames(rootNode, translateFieldNames); for (final String requiredColName : schema.getRequiredColumnNames()) { final String normalizedColName = normalizeColumnName(requiredColName, translateFieldNames); if (!normalizedFieldNames.contains(normalizedColName)) { String missingColMessage = "JSON does not have a value for the Required column '" + requiredColName + "'"; if (failUnmappedColumns) { getLogger().error(missingColMessage); throw new ProcessException(missingColMessage); } else if (warningUnmappedColumns) { getLogger().warn(missingColMessage); } } } final StringBuilder sqlBuilder = new StringBuilder(); int fieldCount = 0; sqlBuilder.append("INSERT INTO "); if (quoteTableName) { sqlBuilder.append(schema.getQuotedIdentifierString()) .append(tableName) .append(schema.getQuotedIdentifierString()); } else { sqlBuilder.append(tableName); } sqlBuilder.append(" ("); // iterate over all of the elements in the JSON, building the SQL statement by adding the column names, as well as // adding the column value to a "sql.args.N.value" attribute and the type of a "sql.args.N.type" attribute add the // columns that we are inserting into final Iterator<String> fieldNames = rootNode.getFieldNames(); while (fieldNames.hasNext()) { final String fieldName = fieldNames.next(); final ColumnDescription desc = schema.getColumns().get(normalizeColumnName(fieldName, translateFieldNames)); if (desc == null && !ignoreUnmappedFields) { throw new ProcessException("Cannot map JSON field '" + fieldName + "' to any column in the database"); } if (desc != null) { if (fieldCount++ > 0) { sqlBuilder.append(", "); } if(escapeColumnNames){ sqlBuilder.append(schema.getQuotedIdentifierString()) .append(desc.getColumnName()) .append(schema.getQuotedIdentifierString()); } else { sqlBuilder.append(desc.getColumnName()); } final int sqlType = desc.getDataType(); attributes.put("sql.args." + fieldCount + ".type", String.valueOf(sqlType)); final Integer colSize = desc.getColumnSize(); final JsonNode fieldNode = rootNode.get(fieldName); if (!fieldNode.isNull()) { String fieldValue = fieldNode.asText(); if (colSize != null && fieldValue.length() > colSize) { fieldValue = fieldValue.substring(0, colSize); } attributes.put("sql.args." + fieldCount + ".value", fieldValue); } } } // complete the SQL statements by adding ?'s for all of the values to be escaped. sqlBuilder.append(") VALUES ("); for (int i=0; i < fieldCount; i++) { if (i > 0) { sqlBuilder.append(", "); } sqlBuilder.append("?"); } sqlBuilder.append(")"); if (fieldCount == 0) { throw new ProcessException("None of the fields in the JSON map to the columns defined by the " + tableName + " table"); } return sqlBuilder.toString(); }