用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 ...
随机推荐
- Django实战(16):Django+jquery
现在我们有了一个使用json格式的RESTful API,可以实现这样的功能了:为了避免在产品列表和购物车之间来回切换,需要在产品列表界面显示购物车,并且通过ajax的方式不刷新界面就更新购物车的显示 ...
- python二叉树简单实现
二叉树简单实现: class Node: def __init__(self,item): self.item = item self.child1 = None self.child2 = None ...
- Dubbo的静态服务
Dubbo中,dubbo可以自动搜索到服务上线,并注册,同时,也可以知道服务下线,自动从注册中心去掉服务. 但是静态服务就可以做到人工管理. 1.场景 有时候希望人工管理服务提供者的上线和下线,此时需 ...
- Tomcat --> Cannot create a server using the selected type
今天在eclipse想把之前的Tomcat 6删掉,重新配置一个,不料没有下一步 Cannot create a server using the selected type 这句话出现在窗口上面,应 ...
- JAVAEE——SpringMVC第二天:高级参数绑定、@RequestMapping、方法返回值、异常处理、图片上传、Json交互、实现RESTful、拦截器
1. 课前回顾 https://www.cnblogs.com/xieyupeng/p/9093661.html 2. 课程计划 1.高级参数绑定 a) 数组类型的参数绑定 b) List类型的绑定 ...
- C# 字符串处理小工具
之前刚上大学时沉迷于安全方面,当时一直想写一个处理字符串的小程序. 无奈当时没有太多时间,一直拖延到这寒假. 寒假闲来无事,所以就写写小程序来练手,顺便复习一下窗体和基础. 实现的功能有以下: 转换为 ...
- [BZOJ4569][SCOI2016]萌萌哒(倍增+并查集)
首先有一个显然的$O(n^2)$暴力做法,将每个位置看成点,然后将所有限制相等的数之间用并查集合并,最后答案就是9*(10^连通块的个数).(特判n=1时就是10). 然后比较容易想到的是,由于每次合 ...
- hdu 4549 矩阵快速幂
题意: M斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在给出a, b, n,你能求出F ...
- IDEA JSP项目构建及学习心得
近期学习的东西比较杂乱,导致了很多东西都有些忘却.在这里记录一份心得. 简而言之JSP也就是Java代码在页面上的一种呈现方式,用于Web项目的前台展示. 在这里不做过多的阐述. MVC设计模式,Se ...
- 【Codeforces 949D】Shake It! 【动态规划】
参考: http://blog.csdn.net/gjghfd/article/details/77824901 所求的是满足条件的图中“不同构”的数量,意味着操作的顺序是可以忽略的.考虑若干次操作后 ...