Skip to content

Commit

Permalink
Merge pull request #31 from albertocsm/pr_elasticsearch_connector
Browse files Browse the repository at this point in the history
Convert object to json when the field type is nested
  • Loading branch information
FlavioF authored and Flávio Ferreira committed Dec 12, 2016
2 parents c39ce1c + c074741 commit 1c328c0
Showing 1 changed file with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@

import com.facebook.presto.spi.RecordCursor;
import com.facebook.presto.spi.type.Type;
import io.airlift.log.Logger;
import io.airlift.slice.Slice;
import io.airlift.slice.Slices;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHitField;
import org.json.JSONArray;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -39,7 +40,6 @@
public class ElasticsearchRecordCursor
implements RecordCursor
{
private static final Logger log = Logger.get(ElasticsearchRecordCursor.class);
private final List<ElasticsearchColumnHandle> columnHandles;
private final Map<String, Integer> jsonPathToIndex;
private final Iterator<SearchHit> lines;
Expand Down Expand Up @@ -141,7 +141,14 @@ public double getDouble(int field)
public Slice getSlice(int field)
{
checkFieldType(field, VARCHAR);
return Slices.utf8Slice(String.valueOf(getFieldValue(field)));

Object value = getFieldValue(field);
if (value instanceof Collection) {
return Slices.utf8Slice(String.valueOf(toJson((List<Map<String, Object>>) value)));
}
else {
return Slices.utf8Slice(String.valueOf(value));
}
}

@Override
Expand Down Expand Up @@ -230,4 +237,9 @@ private void extractFromSource(SearchHit hit)
setFieldIfExists(jsonPath, entryValue);
}
}

private String toJson(Collection<Map<String, Object>> list)
{
return new JSONArray(list).toString();
}
}

0 comments on commit 1c328c0

Please sign in to comment.