用java在客户端读取mongodb中的数据并发送至服务器
使用Java自带的socket端口来实现,程序如下:
Client.java
package com.cn.gao;
import java.net.*;
import java.io.*; import com.mongodb.*;
/**
* 客户端发送消息给服务器
* @author hadoop
*
*/
public class Client {
private Socket client;
private boolean connected;
//客户端构造函数
public Client(String host,int port){
try {
client = new Socket(host,port);
System.out.println("连接服务器成功!");
this.connected = true;
} catch (UnknownHostException e) {
System.out.println("无法解析主机名!");
this.connected = false;
} catch (IOException e) {
System.out.println("输入输出错误!");
this.connected = false;
closeSocket();
}
}
//判断是否连接
public boolean isConnected(){
return connected;
}
//设置连接状态
public void setConnected(boolean connected){
this.connected = connected;
}
/**
* 发送数据到端口
* @param dbname mongodb数据库名字
* @param collectionName 该数据库中要发送数据的collection名
*/
public void sendFile(String dbname,String collectionName){
DataOutputStream dos = null;
DataInputStream dis = null;
if(client==null) return;
//从mongodb数据库中读取数据
Mongo connection = new Mongo("localhost:27017");
DB db = connection.getDB(dbname);
DBCollection input = db.getCollection(collectionName);
/* BasicDBObject condition=new BasicDBObject();//条件
BasicDBObject key=new BasicDBObject("vtext",2);//指定需要显示列
DBCursor cur = input.find(condition,key);*/
DBCursor cur = input.find();
try {
while(cur.hasNext()){
DBObject document = cur.next();
dis = new DataInputStream(new ByteArrayInputStream(document.toString().getBytes()));
dos = new DataOutputStream(client.getOutputStream());
int bufferSize = 10240;
byte[] buf = new byte[bufferSize];
int num =0;
while((num=dis.read(buf))!=-1){
System.out.println(new String(buf));
dos.write(buf, 0, num);
}
dos.flush();
}
System.out.println("传输成功!");
} catch (IOException e) {
e.printStackTrace();
// System.out.println("输入输出错误!");
closeSocket();
} finally{
try {
if(dis!=null) dis.close();
if(dos!=null) dos.close();
} catch (IOException e) {
e.printStackTrace();
// System.out.println("输入输出错误!");
}
} } //关闭客户端
public void closeSocket(){
if(client!=null){
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* @param args
* 参数格式如下
* hostname dbname collectionName
*/
public static void main(String[] args){
//默认端口为8888
if(args.length!=3){
System.out.println("参数格式不对!");
return;
}
String hostName = args[0];
int port = 8888;
Client client = null;
client = new Client(hostName,port);
String dbname = args[1];
String collectionName = args[2];
if(client.isConnected()){
client.sendFile(dbname, collectionName);
client.closeSocket();
} }
}
Server.java
package com.cn.gao;
import java.io.*;
import java.net.*; /**
* 服务器端
* @author hadoop
*
*/
public class Server {
private int port;
private String host;
private static ServerSocket server; public Server(int port){
this.port = port;
this.server = null;
} public void run(){
if(server==null){
try {
server = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("服务已启动...");
while(true){
try {
Socket client = server.accept();
if(client==null) continue;
new SocketConnection(client).run();
} catch (IOException e) {
e.printStackTrace();
}
}
} public class SocketConnection extends Thread{
private Socket client;
public SocketConnection(Socket client){
this.client = client;
} public void run(){
if(client==null) return;
DataInputStream in= null;
boolean flag = true;
try {
while(flag){
in = new DataInputStream(new BufferedInputStream(client.getInputStream()));
int bufferSize = 10240;
byte[] buf = new byte[bufferSize];
int num =0;
while((num=in.read(buf))!=-1){
System.out.println(new String(buf));
}
}
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
if(in!=null) in.close();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} public static void main(String[] args){
int port = 8888;
new Server(port).run();
}
}
用java在客户端读取mongodb中的数据并发送至服务器的更多相关文章
- java读取请求中body数据
java读取请求中body数据 /** * 获取request中body数据 * * @author lifq * * 2017年2月24日 下午2:29:06 * @throws IOExcepti ...
- java.util.Properties 读取配置文件中的参数
用法 getProperty方法的返回值是String类型. java.util.Properties 读取配置文件中的参数 //读取配置文件 FileInputStream inStream = n ...
- 编写SqlHelper使用,在将ExecuteReader方法封装进而读取数据库中的数据时会产生Additional information: 阅读器关闭时尝试调用 Read 无效问题,解决方法与解释
在自学杨中科老师的视频教学时,拓展编写SqlHelper使用,在将ExecuteReader方法封装进而读取数据库中的数据时 会产生Additional information: 阅读器关闭时尝试调用 ...
- 读取redis中的数据时出现:MISCONF Redis is configured to save RDB snapshots
读取redis中的数据时出现:MISCONF Redis is configured to save RDB snapshots 以下为异常详细信息: Exception in thread &q ...
- 使用Hive读取ElasticSearch中的数据
本文将介绍如何通过Hive来读取ElasticSearch中的数据,然后我们可以像操作其他正常Hive表一样,使用Hive来直接操作ElasticSearch中的数据,将极大的方便开发人员.本文使用的 ...
- MongoDB中导入数据命令的使用(mongoimport)
MongoDB中导入数据命令的使用(mongoimport) 制作人:全心全意 语法: mongoimport <options> <file> 介绍: 该命令可以将CSV,T ...
- Spark读取Hbase中的数据
大家可能都知道很熟悉Spark的两种常见的数据读取方式(存放到RDD中):(1).调用parallelize函数直接从集合中获取数据,并存入RDD中:Java版本如下: JavaRDD<Inte ...
- Python中如何读取xls中的数据
要想读取EXCEL中的数据,首先得下载xlrd包,地址:https://pypi.python.org/pypi/xlrd 安装方法:下载解压后,利用windows dos命令进入解压目录eg,c ...
- sql 读取excel中的数据
select 列名 as 字段名 from openBowSet('MSDASQL.1','driver=Microsoft Excel Driver(*.xls);dbq=文件存放地址','sele ...
随机推荐
- 9-4 Unidirectional TSP uva116 (DP)
题意:给一个n行m列矩阵 从第一列任意一个位置出发 每次往右 右上 右下三个方向走一格 直到最后一列 输出所类和的最小值和路径!! 最小值相同则输出字典序最小路径 很像一开始介绍的三角形dp ...
- 关于ueditor的使用心得
http://blog.csdn.net/baronyang/article/details/45640181 1.取编辑器内的内容: <span style="font-size:1 ...
- @react-native-community/async-storage在Android上的手动link问题
PS C:\Users\linjin\Desktop\RN_APP> react-native link @react-native-community/async-storage error ...
- Java 中的异常
前段时间集合的整理真的是给我搞得心力交瘁啊,现在可以整理一些稍微简单一点的,搭配学习 ~ 突然想到一个问题,这些东西我之前就整理过,现在再次整理有什么区别嘛?我就自问自答一下,可能我再次整理会看到不一 ...
- Android调用C#的WebService
Android调用C#写的WebService 学习自: http://www.cnblogs.com/kissazi2/p/3406662.html 运行环境 Win10 VS 2015 Andro ...
- [leetcode greedy]45. Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- JavaScript基础-DAY1
JavaScript介绍 你不知道它是什么就学?这就是一个网页嵌入式脚本语言...仅此而已 JavaScript组成 一个完整的 JavaScript 实现是由以下 3 个不同部分组成的: 核心(EC ...
- anaconda安装tensorflow后pip安装jieba出错的问题
安装jieba出错,参考https://www.cnblogs.com/minsons/p/7872647.html TypeError: parse() got an unexpected keyw ...
- 面向对象设计原则 开放封闭原则(Open Closed Principle)
开放封闭原则(OCP,Open Closed Principle) 开放封闭原则是所有面向对象原则的核心. 软件设计本身所追求的目标就是封装变化.降低耦合,而开放封闭原则正是对这一目标的最直接体现. ...
- 02-c#基础之01-基础语法(一)
1.注释符 1)注销 2) 解释 2.C#中的3种注释符 1)单行注释// 2)多行注释/*要注释的内容*/ 3)文档注释///多用来解释类或者方法 2.VS中的快捷键