티스토리 뷰

공부

[kotlin] avroUtils (/avsc)

승가비 2022. 6. 20. 00:20
728x90
object AvroUtils {
    private const val PATH = "/avsc"

    private val parser = Schema.Parser()

    fun define(name: String): Schema {
        return parser.parse(
            AvroUtils.javaClass
                .getResourceAsStream("${PATH}/${name}.avsc")
        )
    }

    fun convert(schema: Schema, json: String): GenericRecord {
        val reader: DatumReader<GenericData.Record> = GenericDatumReader(schema)
        val decoder: Decoder = DecoderFactory().jsonDecoder(schema, json)

        return reader.read(null, decoder)
    }

    fun convert(record: GenericRecord): ByteArray? {
        return toBytes(record)
    }

    @Throws(IOException::class)
    private fun toBytes(genericRecord: GenericRecord): ByteArray? {
        ByteArrayOutputStream().use { outputStream ->
            val writer: DatumWriter<GenericContainer> = GenericDatumWriter(genericRecord.schema)
            writer.setSchema(genericRecord.schema)

            val encoder: BinaryEncoder = EncoderFactory.get().binaryEncoder(outputStream, null)
            writer.write(genericRecord, encoder)
            encoder.flush()
            return outputStream.toByteArray()
        }
    }
}

resources/avsc/data-api.avsc

https://seongtak-yoon.tistory.com/27

 

[Gradle] 파일 복사, Jar에 파일 포함하기 (task copy)

// << 는 바로 수행시키않는 역할을 함 task copySample() << { println file("src/main/resources/sample.properties") println file("build/classes/") copy {  from "src/main/resources/sample.properties"..

seongtak-yoon.tistory.com

https://stackoverflow.com/questions/20389255/reading-a-resource-file-from-within-jar

 

Reading a resource file from within jar

I would like to read a resource from within my jar like so: File file; file = new File(getClass().getResource("/file.txt").toURI()); BufferedReader reader = new BufferedReader(new FileRea...

stackoverflow.com

https://toolslick.com/generation/metadata/avro-schema-from-json

 

Avro Schema From JSON Generator - Tool Slick

JavaScript Object Notation (JSON), pronounced as Jason, is the most common data interchange format on the web. Douglas Crockford first released the JSON specification in the early 2000s. It is a simple format that is easier to comprehend than XML. It is al

toolslick.com

 

728x90

'공부' 카테고리의 다른 글

[Shell] concat string arrays  (0) 2022.06.20
[java] org.apache.commons.lang3.CharEncoding.UTF_8  (0) 2022.06.20
[kotlin] avroUtils (/avsc)  (0) 2022.06.20
[kotlin] merge json  (0) 2022.06.20
[Shell]join string array  (0) 2022.06.20
[Kotlin] add list + list  (0) 2022.06.19
댓글
댓글쓰기 폼