How to filter json data in stream
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
|
show 3 more comments
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
post your json here
– Ganesh Gudghe
Nov 12 '18 at 8:03
2
in which way your question relates toKafka?
– 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
|
show 3 more comments
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
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
java apache-kafka kafka-producer-api
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 toKafka?
– 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
|
show 3 more comments
post your json here
– Ganesh Gudghe
Nov 12 '18 at 8:03
2
in which way your question relates toKafka?
– 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
|
show 3 more comments
1 Answer
1
active
oldest
votes
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
edited Nov 12 '18 at 14:00
answered Nov 12 '18 at 13:23
cricket_007cricket_007
79.9k1142110
79.9k1142110
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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