How to filter json data in stream










-1















I tried the code below and I searched a lot from google, but still can't find my answer.



I can you help me please?



This is my source code:



public static void main( String args ) throws IOException

String fileName = "path\src\Samp-le\Data1.json";

try (Stream<String> Data = Files.lines( Paths.get( fileName ) ))

Stream<String> results = Data.Stream < String > ( )
.filter( str -> str.length() > 6 )// filter the stream to create a new stream
.collect( Collectors.toList() );
results.forEach( System.out::println );




And here I want to post the json data into kafka producer.



String topicName="test-topic";
String key="sample key";
String value=Data.toString();
Properties prop=new Properties();
prop.put("bootstrap.servers", "localhost:9092,localhost:9093");
prop.put("key.serializer",
"org.apache.kafka.common.serialization.StringSerializer");
prop.put("value.serializer",
"org.apache.kafka.common.serialization.StringSerializer");
KafkaProducer<String,String> sampleProducer=new KafkaProducer<String,String>(prop);
ProducerRecord<String,String>record=new ProducerRecord<String,String>(topicName,value.toString());
sampleProducer.send(record);
sampleProducer.close();


Following is the json.



 
"employee":
"firstName":"Lokesh",
"lastName":"Gupta",
"website":"howtodoinjava.com"

,

"employee":
"firstName":"Brian",
"lastName":"Schultz",
"website":"example.com"











share|improve this question
























  • post your json here

    – Ganesh Gudghe
    Nov 12 '18 at 8:03






  • 2





    in which way your question relates to Kafka?

    – Vasiliy Sarzhynskyi
    Nov 12 '18 at 8:09











  • @VasiliySarzhynskyi I read a JSON data and filter in the stream and I want to post into Kafka producer

    – Paunraj
    Nov 12 '18 at 8:11











  • @GaneshPatil "employee": "firstName": "Lokesh", "lastName": "Gupta", "website": "howtodoinjava.com" , "employee": "firstName": "Brian", "lastName": "Schultz", "website": "example.com"

    – Paunraj
    Nov 12 '18 at 8:15











  • Do you want to filter on which key?

    – Ganesh Gudghe
    Nov 12 '18 at 8:18















-1















I tried the code below and I searched a lot from google, but still can't find my answer.



I can you help me please?



This is my source code:



public static void main( String args ) throws IOException

String fileName = "path\src\Samp-le\Data1.json";

try (Stream<String> Data = Files.lines( Paths.get( fileName ) ))

Stream<String> results = Data.Stream < String > ( )
.filter( str -> str.length() > 6 )// filter the stream to create a new stream
.collect( Collectors.toList() );
results.forEach( System.out::println );




And here I want to post the json data into kafka producer.



String topicName="test-topic";
String key="sample key";
String value=Data.toString();
Properties prop=new Properties();
prop.put("bootstrap.servers", "localhost:9092,localhost:9093");
prop.put("key.serializer",
"org.apache.kafka.common.serialization.StringSerializer");
prop.put("value.serializer",
"org.apache.kafka.common.serialization.StringSerializer");
KafkaProducer<String,String> sampleProducer=new KafkaProducer<String,String>(prop);
ProducerRecord<String,String>record=new ProducerRecord<String,String>(topicName,value.toString());
sampleProducer.send(record);
sampleProducer.close();


Following is the json.



 
"employee":
"firstName":"Lokesh",
"lastName":"Gupta",
"website":"howtodoinjava.com"

,

"employee":
"firstName":"Brian",
"lastName":"Schultz",
"website":"example.com"











share|improve this question
























  • post your json here

    – Ganesh Gudghe
    Nov 12 '18 at 8:03






  • 2





    in which way your question relates to Kafka?

    – Vasiliy Sarzhynskyi
    Nov 12 '18 at 8:09











  • @VasiliySarzhynskyi I read a JSON data and filter in the stream and I want to post into Kafka producer

    – Paunraj
    Nov 12 '18 at 8:11











  • @GaneshPatil "employee": "firstName": "Lokesh", "lastName": "Gupta", "website": "howtodoinjava.com" , "employee": "firstName": "Brian", "lastName": "Schultz", "website": "example.com"

    – Paunraj
    Nov 12 '18 at 8:15











  • Do you want to filter on which key?

    – Ganesh Gudghe
    Nov 12 '18 at 8:18













-1












-1








-1








I tried the code below and I searched a lot from google, but still can't find my answer.



I can you help me please?



This is my source code:



public static void main( String args ) throws IOException

String fileName = "path\src\Samp-le\Data1.json";

try (Stream<String> Data = Files.lines( Paths.get( fileName ) ))

Stream<String> results = Data.Stream < String > ( )
.filter( str -> str.length() > 6 )// filter the stream to create a new stream
.collect( Collectors.toList() );
results.forEach( System.out::println );




And here I want to post the json data into kafka producer.



String topicName="test-topic";
String key="sample key";
String value=Data.toString();
Properties prop=new Properties();
prop.put("bootstrap.servers", "localhost:9092,localhost:9093");
prop.put("key.serializer",
"org.apache.kafka.common.serialization.StringSerializer");
prop.put("value.serializer",
"org.apache.kafka.common.serialization.StringSerializer");
KafkaProducer<String,String> sampleProducer=new KafkaProducer<String,String>(prop);
ProducerRecord<String,String>record=new ProducerRecord<String,String>(topicName,value.toString());
sampleProducer.send(record);
sampleProducer.close();


Following is the json.



 
"employee":
"firstName":"Lokesh",
"lastName":"Gupta",
"website":"howtodoinjava.com"

,

"employee":
"firstName":"Brian",
"lastName":"Schultz",
"website":"example.com"











share|improve this question
















I tried the code below and I searched a lot from google, but still can't find my answer.



I can you help me please?



This is my source code:



public static void main( String args ) throws IOException

String fileName = "path\src\Samp-le\Data1.json";

try (Stream<String> Data = Files.lines( Paths.get( fileName ) ))

Stream<String> results = Data.Stream < String > ( )
.filter( str -> str.length() > 6 )// filter the stream to create a new stream
.collect( Collectors.toList() );
results.forEach( System.out::println );




And here I want to post the json data into kafka producer.



String topicName="test-topic";
String key="sample key";
String value=Data.toString();
Properties prop=new Properties();
prop.put("bootstrap.servers", "localhost:9092,localhost:9093");
prop.put("key.serializer",
"org.apache.kafka.common.serialization.StringSerializer");
prop.put("value.serializer",
"org.apache.kafka.common.serialization.StringSerializer");
KafkaProducer<String,String> sampleProducer=new KafkaProducer<String,String>(prop);
ProducerRecord<String,String>record=new ProducerRecord<String,String>(topicName,value.toString());
sampleProducer.send(record);
sampleProducer.close();


Following is the json.



 
"employee":
"firstName":"Lokesh",
"lastName":"Gupta",
"website":"howtodoinjava.com"

,

"employee":
"firstName":"Brian",
"lastName":"Schultz",
"website":"example.com"








java apache-kafka kafka-producer-api






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 21:58









Matthias J. Sax

28.6k35075




28.6k35075










asked Nov 12 '18 at 8:00









PaunrajPaunraj

76




76












  • post your json here

    – Ganesh Gudghe
    Nov 12 '18 at 8:03






  • 2





    in which way your question relates to Kafka?

    – Vasiliy Sarzhynskyi
    Nov 12 '18 at 8:09











  • @VasiliySarzhynskyi I read a JSON data and filter in the stream and I want to post into Kafka producer

    – Paunraj
    Nov 12 '18 at 8:11











  • @GaneshPatil "employee": "firstName": "Lokesh", "lastName": "Gupta", "website": "howtodoinjava.com" , "employee": "firstName": "Brian", "lastName": "Schultz", "website": "example.com"

    – Paunraj
    Nov 12 '18 at 8:15











  • Do you want to filter on which key?

    – Ganesh Gudghe
    Nov 12 '18 at 8:18

















  • post your json here

    – Ganesh Gudghe
    Nov 12 '18 at 8:03






  • 2





    in which way your question relates to Kafka?

    – Vasiliy Sarzhynskyi
    Nov 12 '18 at 8:09











  • @VasiliySarzhynskyi I read a JSON data and filter in the stream and I want to post into Kafka producer

    – Paunraj
    Nov 12 '18 at 8:11











  • @GaneshPatil "employee": "firstName": "Lokesh", "lastName": "Gupta", "website": "howtodoinjava.com" , "employee": "firstName": "Brian", "lastName": "Schultz", "website": "example.com"

    – Paunraj
    Nov 12 '18 at 8:15











  • Do you want to filter on which key?

    – Ganesh Gudghe
    Nov 12 '18 at 8:18
















post your json here

– Ganesh Gudghe
Nov 12 '18 at 8:03





post your json here

– Ganesh Gudghe
Nov 12 '18 at 8:03




2




2





in which way your question relates to Kafka?

– Vasiliy Sarzhynskyi
Nov 12 '18 at 8:09





in which way your question relates to Kafka?

– Vasiliy Sarzhynskyi
Nov 12 '18 at 8:09













@VasiliySarzhynskyi I read a JSON data and filter in the stream and I want to post into Kafka producer

– Paunraj
Nov 12 '18 at 8:11





@VasiliySarzhynskyi I read a JSON data and filter in the stream and I want to post into Kafka producer

– Paunraj
Nov 12 '18 at 8:11













@GaneshPatil "employee": "firstName": "Lokesh", "lastName": "Gupta", "website": "howtodoinjava.com" , "employee": "firstName": "Brian", "lastName": "Schultz", "website": "example.com"

– Paunraj
Nov 12 '18 at 8:15





@GaneshPatil "employee": "firstName": "Lokesh", "lastName": "Gupta", "website": "howtodoinjava.com" , "employee": "firstName": "Brian", "lastName": "Schultz", "website": "example.com"

– Paunraj
Nov 12 '18 at 8:15













Do you want to filter on which key?

– Ganesh Gudghe
Nov 12 '18 at 8:18





Do you want to filter on which key?

– Ganesh Gudghe
Nov 12 '18 at 8:18












1 Answer
1






active

oldest

votes


















0














If you want to produce to Kafka, replace System.out::println with producer.send(record) after you map a line into a ProducerRecord



There should be no difference in sending a single string message and what you're trying to do. Only the source of your data is different, and you happen to be filtering it on some arbitrary conditions... (rather than length, you should make sure the JSON is actually valid and contains the information you need in some consumer)



Note: If you have more than one partition in your Kafka topic, you'll lose consumption ordering with your current code because you're using a null key in your messages






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257944%2fhow-to-filter-json-data-in-stream%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    If you want to produce to Kafka, replace System.out::println with producer.send(record) after you map a line into a ProducerRecord



    There should be no difference in sending a single string message and what you're trying to do. Only the source of your data is different, and you happen to be filtering it on some arbitrary conditions... (rather than length, you should make sure the JSON is actually valid and contains the information you need in some consumer)



    Note: If you have more than one partition in your Kafka topic, you'll lose consumption ordering with your current code because you're using a null key in your messages






    share|improve this answer





























      0














      If you want to produce to Kafka, replace System.out::println with producer.send(record) after you map a line into a ProducerRecord



      There should be no difference in sending a single string message and what you're trying to do. Only the source of your data is different, and you happen to be filtering it on some arbitrary conditions... (rather than length, you should make sure the JSON is actually valid and contains the information you need in some consumer)



      Note: If you have more than one partition in your Kafka topic, you'll lose consumption ordering with your current code because you're using a null key in your messages






      share|improve this answer



























        0












        0








        0







        If you want to produce to Kafka, replace System.out::println with producer.send(record) after you map a line into a ProducerRecord



        There should be no difference in sending a single string message and what you're trying to do. Only the source of your data is different, and you happen to be filtering it on some arbitrary conditions... (rather than length, you should make sure the JSON is actually valid and contains the information you need in some consumer)



        Note: If you have more than one partition in your Kafka topic, you'll lose consumption ordering with your current code because you're using a null key in your messages






        share|improve this answer















        If you want to produce to Kafka, replace System.out::println with producer.send(record) after you map a line into a ProducerRecord



        There should be no difference in sending a single string message and what you're trying to do. Only the source of your data is different, and you happen to be filtering it on some arbitrary conditions... (rather than length, you should make sure the JSON is actually valid and contains the information you need in some consumer)



        Note: If you have more than one partition in your Kafka topic, you'll lose consumption ordering with your current code because you're using a null key in your messages







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 '18 at 14:00

























        answered Nov 12 '18 at 13:23









        cricket_007cricket_007

        79.9k1142110




        79.9k1142110



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257944%2fhow-to-filter-json-data-in-stream%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kleinkühnau

            Makov (Slowakei)

            Deutsches Schauspielhaus