Tuesday, February 27, 2024

parsing json in chunks

 import com.fasterxml.jackson.core.JsonFactory;

import com.fasterxml.jackson.core.JsonParser;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;


public class JsonProcessor {


    public void processLargeJsonFile(String filePath) throws IOException {

        ObjectMapper objectMapper = new ObjectMapper();

        JsonFactory jsonFactory = objectMapper.getFactory();


        try (FileInputStream fis = new FileInputStream(new File(filePath))) {

            JsonParser jsonParser = jsonFactory.createParser(fis);


            // Move the parser to the start of the JSON array

            while (jsonParser.nextToken() != null) {

                if (jsonParser.currentToken().equals(JsonToken.START_ARRAY)) {

                    break;

                }

            }


            // Process JSON objects in chunks

            while (jsonParser.nextToken() != null) {

                // Process each JSON object

                MyPojo pojo = objectMapper.readValue(jsonParser, MyPojo.class);

                // Do something with the pojo

            }

        }

    }

}


while (parser.nextToken() != null) { if (JsonToken.FIELD_NAME.equals(parser.currentToken()) && "header".equals(parser.getCurrentName())) { parser.nextToken(); // Move to the value of "header" String headerValue = parser.getValueAsString(); System.out.println("Header Value: " + headerValue); break; // Exit loop once header is found } }


Sunday, February 18, 2024

gzip tar extract


import java.nio.file.*; import java.io.*; import org.apache.commons.compress.archivers.tar.*; import org.apache.commons.compress.compressors.gzip.*; public class TarGzExtractor { public static void main(String[] args) throws IOException { Path tarGzFilePath = Paths.get("path/to/your/file.tar.gz"); try (InputStream is = Files.newInputStream(tarGzFilePath); BufferedInputStream bis = new BufferedInputStream(is); GzipCompressorInputStream gzis = new GzipCompressorInputStream(bis); TarArchiveInputStream taris = new TarArchiveInputStream(gzis)) { TarArchiveEntry entry; while ((entry = taris.getNextTarEntry()) != null) { Path destPath = Paths.get("destination/directory", entry.getName()); if (entry.isDirectory()) { Files.createDirectories(destPath); } else { Files.copy(taris, destPath, StandardCopyOption.REPLACE_EXISTING); } } } } }