Skip to content
Advertisement

How create a JSONArray in java

I have 2 java functions: listeFilesHdfs return a list of files that stored in HDFS, for example:

If you remark, the files that stored in HDFS there content is a JSON format, for example:

{
"name":"Name",
"type":"string"
},
{
"name":"Version",
"type":"string"
},
{
"name":"r_service",
"type":"string"
},
{
"name":"r_timestamp",
"type":"long"
},

I created the below function to call both function above (one return list of files and the second open a path):

How can I modify my function to read the files content and to add each file content to the JSON array and return an array of JSON ? Thanks

Advertisement

Answer

As by your comment answer, you are looking for a way to parse the json content of the files into a javax.json-JsonArray.

The answer is the class JsonReader. As per the documentation:

JsonReaderFactory factory = Json.createReaderFactory(...);
JsonReader reader = factory.createReader(...);
JsonStructure content = Jsonreader.read();

That JsonStructure could then be a JsonArray (and you may cast to it, after checking the class).

It might work like this (though I can’t test it):

public JSONArray getSchema()
{
    String avroSchemaHDFSDir = "hdfs://hadoopcluster/schemas";

    try(HdfsClient hdfsClient = new HdfsClient(nameNodeHosts, hadoopZks))
    {
        for(int i = 0; i < hdfsClient.listeFilesHdfs(avroSchemaHDFSDir).size(); i++)
        {
            String fileContent = hdfsClient.listeFilesHdfs(avroSchemaHDFSDir).get(i).toString();
            hdfsClient.openHdfsPath(file);
            JsonReaderFactory factory = Json.createReaderFactory(...);
            JsonReader reader = factory.createReader(...);
            JsonStructure content = Jsonreader.read();
            if (content instanceof JsonArray) {
                return (JsonArray) content;
            }
        }
    }
    catch(Exception e)
    {
        logger.debug("get the specified schema ", e.getMessage());
    }

    return null;     
} 
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement