Search code examples
javacurlapache-kafkaprocessbuilderconfluent-schema-registry

Java process builder issue with schema


i am using java process builder to execute the curl command to publish the schema to schema registry but getting error. problem with schema format, not sure how to pass as argument for process builder. please provide any suggestions

sourceSchema = "{"additionalProperties":false,"properties":{"age":{"type":"integer"},"firstname":{"type":"string"},"lastname":{"type":"string","minLength":2,"maxLength":4},"email":{"type":"string","format":"email"},"designation":{"default":"","type":"string"},"mobile":{"type":"string","pattern":"^(\\\\([0-9]{3}\\\\))?[0-9]{3}-[0-9]{4}$"}},"required":["firstname","lastname","age"],"title":"customer","type":"object"}";

String convertSourceSchema = sourceSchema.replace("\"", "\\\"");
        
        String constructSourceSchema = "{\"schema\":\""+convertSourceSchema+"\",\"schemaType\":\"JSON\"}";
        
            
        String[] command = {"curl", "-k", "-X", "POST", "-H", "Content-Type: application/vnd.schemaregistry.v1+json", "--data", ""+constructSourceSchema+"", ""+url+""};        

        ProcessBuilder process = new ProcessBuilder(command); 
        Process p;
        try
        {
            process.redirectErrorStream(true);
            p = process.start();
                        
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            StringBuilder builder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                //builder.append(",");
            }
            String result = builder.toString();
            System.out.print(result);

        }
        catch (Exception e)
        {   System.out.print("error");
            e.printStackTrace();
        }

> Blockquote

error am getting

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                 Dload  Upload   Total   Spent    Left  Speed  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0100   635  100   208  100   427    208    427  0:00:01 --:--:--  0:00:01  2396Unexpected character ('s' (code 115)): was expecting double-quote to start field name at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 1, column: 3]2021-02-24 11:19:16.409  INFO 18428 --- [nio-8080-exec-1] c.c.e.s.u.c.SchemaPublisherUtility  



Solution

  • 
    [curl, -k, -X, POST, -H, Content-Type: application/vnd.schemaregistry.v1+json, --data, {"schema":"{\"additionalProperties\":false,\"properties\":{\"age\":{\"type\":\"integer\"},\"firstname\":{\"type\":\"string\"},\"lastname\":{\"type\":\"string\",\"minLength\":2,\"maxLength\":4},\"email\":{\"type\":\"string\",\"format\":\"email\"},\"designation\":{\"default\":\"\",\"type\":\"string\"},\"mobile\":{\"type\":\"string\",\"pattern\":\"^(\\\\([0-9]{3}\\\\))?[0-9]{3}-[0-9]{4}$\"}},\"required\":[\"firstname\",\"lastname\",\"age\"],\"title\":\"customer\",\"type\":\"object\"}","schemaType":"JSON"}, https://hostname/topicname-value/versions]
    
    @RequestBody schema -->  {"schema":"{\"additionalProperties\":false,\"properties\":{\"age\":{\"type\":\"integer\"},\"firstname\":{\"type\":\"string\"},\"lastname\":{\"type\":\"string\",\"minLength\":2,\"maxLength\":4},\"email\":{\"type\":\"string\",\"format\":\"email\"},\"designation\":{\"default\":\"\",\"type\":\"string\"},\"mobile\":{\"type\":\"string\",\"pattern\":\"^(\\\\([0-9]{3}\\\\))?[0-9]{3}-[0-9]{4}$\"}},\"required\":[\"firstname\",\"lastname\",\"age\"],\"title\":\"customer\",\"type\":\"object\"}","schemaType":"JSON"}
    
    public class SchemaPublisherUtility {
    
        private final Logger LOG = LoggerFactory.getLogger(getClass());
    
        String url = "https://hostname/topicname-value/versions";
    
        String Topic = "dev.json.schemaregistry";
    
        ObjectMapper objectMapper = new ObjectMapper();
    
        @PostMapping(path = "/publishschema", consumes = "application/json", produces = "application/json")
        public void addSchema(@RequestBody String sourceSchema) throws IOException {
    
            LOG.info("Schema Publish Started");
    
            String convertSourceSchema = sourceSchema.replace("\"", "\\\"");
    
            String constructSourceSchema = "{\"schema\":\"" + convertSourceSchema + "\",\"schemaType\":\"JSON\"}";
    
            try {
                URL requestUrl = new URL(url);
    
                HttpsURLConnection conn = (HttpsURLConnection) requestUrl.openConnection();
    
                conn.setRequestProperty("Content-Type", "application/json");
                conn.setDoOutput(true);
                conn.setRequestMethod("POST");
    
                OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
                out.write(constructSourceSchema);
                out.close();
    
                new InputStreamReader(conn.getInputStream());
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            LOG.info("Schema Publish Completed");
    
        }
    }