Java 如何从twitter返回的json中提取文本和用户名?

Java 如何从twitter返回的json中提取文本和用户名?,java,json,twitter,Java,Json,Twitter,我只想从Twitter返回的JSON数据中提取用户名和文本。我尝试了JSON解析器,但无法解决 我正在使用Twitter的HBCAPI检索用户推文。但是,这将返回JSON数据。我一直在Twitter的API中搜索,想找到一种只提取tweets文本和用户名而不是整个JSON的方法,但找不到一个好的解决方案。谁能帮帮忙吗 package abc; import com.google.common.collect.Lists; import com.twitter.hbc.ClientBuilder;

我只想从Twitter返回的JSON数据中提取用户名和文本。我尝试了JSON解析器,但无法解决

我正在使用Twitter的HBCAPI检索用户推文。但是,这将返回JSON数据。我一直在Twitter的API中搜索,想找到一种只提取tweets文本和用户名而不是整个JSON的方法,但找不到一个好的解决方案。谁能帮帮忙吗

package abc;
import com.google.common.collect.Lists;
import com.twitter.hbc.ClientBuilder;
import com.twitter.hbc.core.Client;
import com.twitter.hbc.core.Constants;
import com.twitter.hbc.core.Hosts;
import com.twitter.hbc.core.HttpHosts;
import com.twitter.hbc.core.endpoint.StatusesFilterEndpoint;
import com.twitter.hbc.core.processor.StringDelimitedProcessor;
import com.twitter.hbc.httpclient.auth.Authentication;
import com.twitter.hbc.httpclient.auth.OAuth1;
import twitter4j.internal.org.json.JSONException;
import org.apache.kafka.clients.producer.*;
import org.apache.kafka.common.serialization.StringSerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

public class TwitterProducer {

    Logger logger = LoggerFactory.getLogger(TwitterProducer.class.getName());

    // use your own credentials - don't share them with anyone
    String consumerKey = "xxxxxxxxxxxxxxxxxxxx";
    String consumerSecret = "xxxxxxxxxxxxxxxxx";
    String token = "xxxxxxxxxxxxxxxxxxxxxxxx";
    String secret = "xxxxxxxxxxxxxxxxxxxxxxx";

    List<String> terms = Lists.newArrayList("india");


    public TwitterProducer(){}

    public static void main(String[] args) throws Exception {
        new TwitterProducer().run();
    }

    public void run() throws Exception{

        logger.info("Setup");

        /** Set up your blocking queues: Be sure to size these properly based on expected TPS of your stream */
        BlockingQueue<String> msgQueue = new LinkedBlockingQueue<String>(1000);

        // create a twitter client
        Client client = createTwitterClient(msgQueue);
        // Attempts to establish a connection.
        client.connect();
       // create a kafka producer
        KafkaProducer<String, String> producer = createKafkaProducer();

        // add a shutdown hook
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            logger.info("stopping application...");
            logger.info("shutting down client from twitter...");
            client.stop();
            logger.info("closing producer...");
            producer.close();
            logger.info("done!");
        }));

        // loop to send tweets to kafka
        // on a different thread, or multiple different threads....
        while (!client.isDone()) {
            String msg = null;
            try {
                msg = msgQueue.poll(5, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
                client.stop();
            }

            if (msg != null){
                logger.info(msg);
                producer.send(new ProducerRecord<>("course5i", null, msg), new Callback() {
                    @Override
                    public void onCompletion(RecordMetadata recordMetadata, Exception e) {
                        if (e != null) {
                            logger.error("Something bad happened", e);
                        }
                    }
                });
            }
        }
        logger.info("End of application");
    }

    public Client createTwitterClient(BlockingQueue<String> msgQueue){

       /** Declare the host you want to connect to, the endpoint, and authentication (basic auth or oauth) */
        Hosts hosebirdHosts = new HttpHosts(Constants.STREAM_HOST);
        StatusesFilterEndpoint hosebirdEndpoint = new StatusesFilterEndpoint();

        hosebirdEndpoint.trackTerms(terms);

        // These secrets should be read from a config file
        Authentication hosebirdAuth = new OAuth1(consumerKey, consumerSecret, token, secret);

        ClientBuilder builder = new ClientBuilder()
                .name("Hosebird-Client-01")                              // optional: mainly for the logs
                .hosts(hosebirdHosts)
                .authentication(hosebirdAuth)
                .endpoint(hosebirdEndpoint)
                .processor(new StringDelimitedProcessor(msgQueue));

               Client hosebirdClient = builder.build();
               return hosebirdClient;
    }

    public KafkaProducer<String, String> createKafkaProducer(){
        String bootstrapServers = "127.0.0.1:9092";

        // create Producer properties
        Properties properties = new Properties();
        properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());

        // create safe Producer
        properties.setProperty(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, "true");
        properties.setProperty(ProducerConfig.ACKS_CONFIG, "all");
        properties.setProperty(ProducerConfig.RETRIES_CONFIG, Integer.toString(Integer.MAX_VALUE));
        properties.setProperty(ProducerConfig.MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION, "5"); // kafka 2.0 >= 1.1 so we can keep this as 5. Use 1 otherwise.

        // high throughput producer (at the expense of a bit of latency and CPU usage)
        properties.setProperty(ProducerConfig.COMPRESSION_TYPE_CONFIG, "snappy");
        properties.setProperty(ProducerConfig.LINGER_MS_CONFIG, "20");
        properties.setProperty(ProducerConfig.BATCH_SIZE_CONFIG, Integer.toString(32*1024)); // 32 KB batch size

        // create the producer
        KafkaProducer<String, String> producer = new KafkaProducer<String, String>(properties);
        return producer;
    }
}
package-abc;
导入com.google.common.collect.list;
导入com.twitter.hbc.ClientBuilder;
导入com.twitter.hbc.core.Client;
导入com.twitter.hbc.core.Constants;
导入com.twitter.hbc.core.Hosts;
导入com.twitter.hbc.core.HttpHosts;
导入com.twitter.hbc.core.endpoint.StatusesFilterEndpoint;
导入com.twitter.hbc.core.processor.StringDelimitedProcessor;
导入com.twitter.hbc.httpclient.auth.Authentication;
导入com.twitter.hbc.httpclient.auth.OAuth1;
导入twitter4j.internal.org.json.JSONException;
导入org.apache.kafka.clients.producer.*;
导入org.apache.kafka.common.serialization.StringSerializer;
导入org.slf4j.Logger;
导入org.slf4j.LoggerFactory;
导入java.util.List;
导入java.util.Properties;
导入java.util.concurrent.BlockingQueue;
导入java.util.concurrent.LinkedBlockingQueue;
导入java.util.concurrent.TimeUnit;
公共类推特制作人{
Logger Logger=LoggerFactory.getLogger(TwitterProducer.class.getName());
//使用您自己的凭据-不要与任何人共享
字符串consumerKey=“XXXXXXXXXXXXXXXXX”;
字符串consumerSecret=“XXXXXXXXXXXXXXXXXX”;
字符串标记=“xxxxxxxxxxxxxxxxxxxx”;
String secret=“xxxxxxxxxxxxxxxxxxxx”;
列表术语=Lists.newArrayList(“印度”);
公共TwitterProducer(){}
公共静态void main(字符串[]args)引发异常{
新建TwitterProducer().run();
}
public void run()引发异常{
logger.info(“设置”);
/**设置阻塞队列:确保根据流的预期TPS适当调整这些队列的大小*/
BlockingQueue msgQueue=新的LinkedBlockingQueue(1000);
//创建一个twitter客户端
Client Client=createTwitterClient(msgQueue);
//尝试建立连接。
client.connect();
//创建卡夫卡制作人
KafkaProducer producer=createKafkaProducer();
//添加一个关机挂钩
Runtime.getRuntime().addShutdownHook(新线程(()->{
logger.info(“正在停止应用程序…”);
info(“从twitter关闭客户端…”);
client.stop();
logger.info(“关闭制作人…”);
producer.close();
logger.info(“完成!”);
}));
//循环向卡夫卡发送推文
//在不同的线程上,或在多个不同的线程上。。。。
而(!client.isDone()){
字符串msg=null;
试一试{
msg=msgQueue.poll(5,时间单位:秒);
}捕捉(中断异常e){
e、 printStackTrace();
client.stop();
}
如果(msg!=null){
logger.info(msg);
send(新ProducerRecord(“course5i”,null,msg),新回调(){
@凌驾
完成时的公共void(RecordMetadata RecordMetadata,异常e){
如果(e!=null){
logger.error(“发生了不好的事情”,e);
}
}
});
}
}
logger.info(“应用程序结束”);
}
公共客户端createTwitterClient(BlockingQueue msgQueue){
/**声明要连接到的主机、端点和身份验证(基本身份验证或oauth)*/
Hosts hosebirdHosts=新的HttpHosts(Constants.STREAM_HOST);
StatusesFilterEndpoint HoseBindPoint=新的StatusesFilterEndpoint();
hosebirdEndpoint.trackTerms(术语);
//这些秘密应该从配置文件中读取
身份验证hosebirdAuth=new OAuth1(consumerKey、consumerSecret、token、secret);
ClientBuilder=newclientbuilder()
.name(“Hosebird-Client-01”)//可选:主要用于日志
.hosts(hosebirdHosts)
.身份验证(hosebirdAuth)
.终点(hosebirdEndpoint)
.processor(新的StringDelimitedProcessor(msgQueue));
客户端hosebirdClient=builder.build();
返回医院客户;
}
公共卡夫卡制作人创建卡夫卡制作人(){
字符串bootstrapserver=“127.0.0.1:9092”;
//创建生产者属性
属性=新属性();
setProperty(ProducerConfig.BOOTSTRAP\u SERVERS\u CONFIG,bootstrapserver);
properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,StringSerializer.CLASS.getName());
properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.CLASS.getName());
//创建安全生产商
properties.setProperty(ProducerConfig.ENABLE_幂等性_CONFIG,“true”);
properties.setProperty(ProducerConfig.ACKS_CONFIG,“all”);
properties.setProperty(ProducerConfig.RETRIES_CONFIG,Integer.toString(Integer.MAX_VALUE));
properties.setProperty(ProducerConfig.MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION,“5”);//kafka 2.0>=1.1,因此我们可以将其保留为5。否则使用1。
//高吞吐量生产者(以牺牲一点延迟和CPU使用为代价)
setProperty(ProducerConfig.COMPRESSION_TYPE_CONFIG,“snappy”);
properties.setProperty(ProducerConfig.LINGER_MS_CONFIG,“20”);
properties.setProperty(ProducerConfig.BATCH_SIZE_CONFIG,Integer.toString(32*1024));//32 KB批大小
//创建制作人
卡夫卡制作人=新卡夫卡制作人
import org.primefaces.json.JSONObject;

String yourJsonString = msgQueue // Here you need to get your json from that msgQueue and save it as a String, or pass it directly to the argument of following JSONObject
JSONObject jsonObject = new JSONObject(yourJsonString);
// After you have the object created you can just access and store the fields you need like so:
// Im not sure if by username you ment the id but I can't see a field username on the json
String userName = jsonObject.getString("id");
String text = jsonObject.getString("text");