/** * @param requestBuilder * @param rowMeta * @param row * @throws IOException */ private void addSourceFromRowFields(IndexRequestBuilder requestBuilder, RowMetaInterface rowMeta, Object[] row) throws IOException { XContentBuilder jsonBuilder = XContentFactory.jsonBuilder().startObject(); for (int i = 0; i < rowMeta.size(); i++) { if(idFieldIndex != null && i == idFieldIndex){//skip id continue; } ValueMetaInterface valueMeta = rowMeta.getValueMeta(i); String name = hasFields ? columnsToJson.get(valueMeta.getName()) : valueMeta.getName(); Object value = row[i]; if(value instanceof Date && value.getClass() != Date.class){ Date subDate = (Date) value; //create a genuine Date object, or jsonBuilder will not recognize it value = new Date(subDate.getTime()); } if (StringUtils.isNotBlank(name)) { jsonBuilder.field(name, value); } } jsonBuilder.endObject(); requestBuilder.setSource(jsonBuilder); }
/** * @param row * @param requestBuilder */ private void addSourceFromJsonString(Object[] row, IndexRequestBuilder requestBuilder) throws KettleStepException { Object jsonString = row[jsonFieldIdx]; if (jsonString instanceof byte[]) { requestBuilder.setSource((byte[]) jsonString); } else if (jsonString instanceof String) { requestBuilder.setSource(((String) jsonString).getBytes()); } else{ throw new KettleStepException(BaseMessages.getString("ElasticSearchBulk.Error.NoJsonFieldFormat")); } }