Skip to content

[QST]List<Struct<String, String>> is same as List<Map<String, String>> ? #1554

Description

@chenrui17

What is your question?
I am tyring to implement my udf jsonToArray , and my cpu udf function is

public List<Map<String, String>> evaluate(String jsonArrayStr) {
        List<Map<String, String>> result = new ArrayList<Map<String, String>>();
        try {
            Object json = new JSONTokener(jsonArrayStr).nextValue();
            if (json instanceof JSONObject) {
                Map<String, String> newMap = new HashMap<String, String>();
                JSONObject newItem = new JSONObject(jsonArrayStr);
                Iterator keys = newItem.keys();
                while (keys != null && keys.hasNext()) {
                    String key = String.valueOf(keys.next());
                    String value = ((JSONObject) json).get(key).toString();
                    if (StringUtils.isNotBlank(key))
                        newMap.put(key, value);
                }
                result.add(newMap);
            } else if (json instanceof JSONArray) {
                JSONArray jsonArray = new JSONArray(jsonArrayStr);
                for (int j = 0; j < jsonArray.length(); j++) {
                    if (jsonArray.get(j) instanceof JSONObject) {
                        JSONObject item = jsonArray.getJSONObject(j);
                        if (item != null) {
                            Map<String, String> newItem = new HashMap<String, String>();
                            Iterator keys = item.keys();
                            while (keys != null && keys.hasNext()) {
                                String key = String.valueOf(keys.next());
                                String value = item.get(key).toString();
                                if (StringUtils.isNotBlank(key))
                                    newItem.put(key, value);
                            }
                            result.add(newItem);
                        }
                    }
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        return result;
    }

I am ready to finish this udf in udf-example reference stringWordCount, but i am confused about return type , cpu udf return List<Map<String, String>>, while cudf only can make column type List<Struct<String, String>, i think they are same for me , right ? if i am wrong , please correct me.

In addition , for jsonToArray udf , my thinking step is

  1. read json string object by cudf_io::read_json
  2. make column by call cudf::make_structs_column and cudf::make_List_column in cudf
  3. return std::unique_ptr<cudf::column>
    please give me some advise if it's wrong, thanks a lot ! @jlowe

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions