Mongodb java api操作

本篇文章主要介绍了mongodb对应java的常用增删改查的api,以及和spring集成后mongoTemplate的常用方法使用,废话不多说,直接上代码:

1.首先上需要用到的两个实体类User和Home,对应用户和家乡

import java.util.List;
import org.springframework.data.mongodb.core.mapping.Document;
/**
 * java类转换为mongodb的文档,它有以下几种注释:
 * [email protected] - 文档的唯一标识,在mongodb中为ObjectId,它是唯一的,通过时间戳+机器标识+进程ID+自增计数器(确保同一秒内产生的Id不会冲突)构成。
 * 2.@Document - 把一个java类声明为mongodb的文档,可以通过collection参数指定这个类对应的文档。
 * [email protected] - 声明该字段需要索引,建索引可以大大的提高查询效率。
 * 4.@Transient - 映射忽略的字段,该字段不会保存到MongoDB
 * [email protected] - 复合索引的声明,建复合索引可以有效地提高多字段的查询效率。
 * [email protected] - 声明构造函数,作用是把从数据库取出的数据实例化为对象。该构造函数传入的值为从DBObject中取出的数据。
 * @author zhangguochen
 *
 */
@Document(collection="user")
public class User {

private String id;
private String name;
private int age;
private List<String> interest;
private String wife;
private Home home;

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName1(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<String> getInterest() {
return interest;
}
public void setInterest(List<String> interest) {
this.interest = interest;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Home getHome() {
return home;
}
public void setHome(Home home) {
this.home = home;
}
}

public class Home {

private String address;

public Home(String address) {
super();
this.address = address;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}

2.以下类MongoDBTest.java展示了mongodb的java api常用的增删改查的方法的使用

import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import junit.framework.TestCase;
import org.bson.types.ObjectId;
import org.junit.Before;
import org.junit.BeforeClass;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.QueryBuilder;
import com.mongodb.QueryOperators;

public class MongoDBTest extends TestCase{

Mongo mongo = null;
DB db = null;
DBCollection user = null;

@BeforeClass
public static void setUpBeforeClass() throws Exception {
}

@Before
public void setUp() throws Exception {
//创建一个MongoDB的数据库连接对象,无参数的话它默认连接到当前机器的localhost地址,端口是27017。
mongo = new Mongo("192.168.225.101",27017);
//得到一个test的数据库,如果mongoDB中没有这个数据库,当向此库中添加数据的时候会自动创建
db = mongo.getDB("test");
db.authenticate("test", "test".toCharArray());
//获取到一个叫做"user"的集合,相当于关系型数据库中的"表"
user = db.getCollection("user");
}

/**
 * 查询所有的集合名称
 */
public void testGetAllCollections() {
Set<String> collectionNames = db.getCollectionNames();
for(String name:collectionNames) {
System.out.println("collectionName:"+name);
}
}

/**
 * 查询所有的用户信息
 */
public void testFind() {
testInitTestData();
//find方法查询所有的数据并返回一个游标对象
DBCursor cursor = user.find();

while(cursor.hasNext()) {
print(cursor.next());
}
//获取数据总条数
int sum = cursor.count();
System.out.println("sum==="+sum);
}

/**
 * 查询第一条数据
 */
public void testFindOne() {
testInitTestData();
//只查询第一条数据
DBObject oneUser = user.findOne();
print(oneUser);
}

/**
 * 条件查询
 */
public void testConditionQuery() {
testInitTestData();
//查询id=50a1ed9965f413fa025166db
DBObject oneUser = user.findOne(new BasicDBObject("_id",new ObjectId("50a1ed9965f413fa025166db")));
print(oneUser);

//查询age=24
List<DBObject> userList1 = user.find(new BasicDBObject("age",24)).toArray();
print("        find age=24: ");
printList(userList1);

//查询age>=23
List<DBObject> userList2 = user.find(new BasicDBObject("age",new BasicDBObject("$gte",23))).toArray();
print("        find age>=23: ");
printList(userList2);

//查询age<=20
List<DBObject> userList3 = user.find(new BasicDBObject("age",new BasicDBObject("$lte",20))).toArray();
print("        find age<=20: ");
printList(userList3);

//查询age!=25
List<DBObject> userList4 = user.find(new BasicDBObject("age",new BasicDBObject("$ne",25))).toArray();
print("        find age!=25: ");
printList(userList4);

//查询age in[23,24,27]
List<DBObject> userList5 = user.find(new BasicDBObject("age",new BasicDBObject(QueryOperators.IN,new int[]{23,24,27}))).toArray();
print("        find agein[23,24,27]: ");
printList(userList5);

//查询age not in[23,24,27]
List<DBObject> userList6 = user.find(new BasicDBObject("age",new BasicDBObject(QueryOperators.NIN,new int[]{23,24,27}))).toArray();
print("        find age not in[23,24,27]: ");
printList(userList6);

//查询29>age>=20
List<DBObject> userList7 = user.find(new BasicDBObject("age",new BasicDBObject("$gte",20).append("$lt", 29))).toArray();
print("        find 29>age>=20: ");
printList(userList7);

//查询age>24 and name="zhangguochen"
BasicDBObject query = new BasicDBObject();
query.put("age", new BasicDBObject("$gt",24));
query.put("name", "zhangguochen");
List<DBObject> userList8 = user.find(query).toArray();
print("        find age>24 and name=‘zhangguochen‘:");
printList(userList8);

//和上面的查询一样,用的是QueryBuilder对象
QueryBuilder queryBuilder = new QueryBuilder();
queryBuilder.and("age").greaterThan(24);
queryBuilder.and("name").equals("zhangguochen");
List<DBObject> userList82 = user.find(queryBuilder.get()).toArray();
print("        QueryBuilder find age>24 and name=‘zhangguochen‘:");
printList(userList82);

//查询所有的用户,并按照年龄升序排列
List<DBObject> userList9 = user.find().sort(new BasicDBObject("age",1)).toArray();
print("        find all sort age asc: ");
printList(userList9);

//查询特定字段
DBObject query1 = new BasicDBObject();//要查的条件 
        query.put("age", new BasicDBObject("$gt",20)); 
        DBObject field = new BasicDBObject();//要查的哪些字段 
        field.put("name", true); 
        field.put("age", true); 
        List<DBObject> userList10=user.find(query1,field).toArray();
        print("        select name,age where age>20");
        printList(userList10);
        
//查询部分数据
        DBObject query2 = new BasicDBObject();//查询条件
        query2.put("age", new BasicDBObject("$lt",27));
        DBObject fields = new BasicDBObject();//查询字段
        fields.put("name",true);
        fields.put("age", true);
        List<DBObject> userList11 = user.find(query2, fields, 1, 1).toArray();
print("        select age,name from user skip 1 limit 1:");
printList(userList11);

//模糊查询
DBObject fuzzy_query=new BasicDBObject();  
        String keyWord="zhang";  
        Pattern pattern = Pattern.compile("^" + keyWord + ".*$", Pattern.CASE_INSENSITIVE);  
        fuzzy_query.put("name", pattern);
        //根据name like zhang%查询
        List<DBObject> userList12 = user.find(fuzzy_query).toArray();
        print("        select * from user where name like ‘zhang*‘");
        printList(userList12);

}

/**
 * 删除用户数据
 */
public void testRemoveUser() {
testInitTestData();
DBObject query=new BasicDBObject();  
//删除age>24的数据
query.put("age", new BasicDBObject("$gt",24));
user.remove(query);
printList(user.find().toArray());
}

/**
 * 修改用户数据
 */
public void testUpdateUser() {

//update(query,set,false,true);
//query:需要修改的数据查询条件,相当于关系型数据库where后的语句
//set:需要设的值,相当于关系型数据库的set语句
//false:需要修改的数据如果不存在,是否插入新数据,false不插入,true插入
//true:如果查询出多条则不进行修改,false:只修改第一条

testInitTestData();

//整体更新
DBObject query=new BasicDBObject(); 
query.put("age", new BasicDBObject("$gt",15));
DBObject set=user.findOne(query);//一定是查询出来的DBObject,否则会丢掉一些列,整体更新
set.put("name", "Abc");
set.put("age", 19);
set.put("interest", new String[]{"hadoop","study","mongodb"});
DBObject zhangguochenAddress = new BasicDBObject();
zhangguochenAddress.put("address", "henan");
set.put("home", zhangguochenAddress);
user.update(query, //需要修改的数据条件
set,//需要赋的值
false,//数据如果不存在,是否新建
false);//false只修改第一条,true如果有多条就不修改
printList(user.find().toArray());

//局部更新,只更改某些列
//加上$set会是局部更新,不会丢掉某些列,只把name更新为"jindazhong",年龄更新为123
BasicDBObject set1 = new BasicDBObject("$set", new BasicDBObject("name","jindazhong").append("age", 123));
user.update(query, //需要修改的数据条件
set1,//需要赋的值
false,//数据如果不存在,是否新建
false);//false只修改第一条,true如果有多条就不修改
printList(user.find().toArray());

//批量更新
// user.updateMulti(new BasicDBObject("age",new BasicDBObject("$gt",16)), 
// new BasicDBObject("$set", new BasicDBObject("name","jindazhong").append("age", 123)));
// printList(user.find().toArray());

}

/**
 * 初始化测试数据
 */
public void testInitTestData() {
user.drop();
DBObject zhangguochen = new BasicDBObject();
zhangguochen.put("name", "zhangguochen");
zhangguochen.put("age", 25);
zhangguochen.put("interest", new String[]{"hadoop","study","mongodb"});
DBObject zhangguochenAddress = new BasicDBObject();
zhangguochenAddress.put("address", "henan");
zhangguochen.put("home", zhangguochenAddress);

DBObject jindazhong = new BasicDBObject();
jindazhong.put("name", "jindazhong");
jindazhong.put("age", 21);
jindazhong.put("interest", new String[]{"hadoop","mongodb"});
jindazhong.put("wife", "小龙女");
DBObject jindazhongAddress = new BasicDBObject();
jindazhongAddress.put("address", "shanghai");
jindazhong.put("home", jindazhongAddress);

DBObject yangzhi = new BasicDBObject();
yangzhi.put("name", "yangzhi");
yangzhi.put("age", 22);
yangzhi.put("interest", new String[]{"shopping","sing","hadoop"});
DBObject yangzhiAddress = new BasicDBObject();
yangzhiAddress.put("address", "hubei");
yangzhi.put("home", yangzhiAddress);

DBObject diaoyouwei = new BasicDBObject();
diaoyouwei.put("name", "diaoyouwei");
diaoyouwei.put("age", 23);
diaoyouwei.put("interest", new String[]{"notejs","sqoop"});
DBObject diaoyouweiAddress = new BasicDBObject();
diaoyouweiAddress.put("address", "shandong");
diaoyouwei.put("home", diaoyouweiAddress);

DBObject cuichongfei = new BasicDBObject();
cuichongfei.put("name", "cuichongfei");
cuichongfei.put("age", 24);
cuichongfei.put("interest", new String[]{"ebsdi","dq"});
cuichongfei.put("wife", "凤姐");
DBObject cuichongfeiAddress = new BasicDBObject();
cuichongfeiAddress.put("address", "shanxi");
cuichongfei.put("home", cuichongfeiAddress);

DBObject huanghu = new BasicDBObject();
huanghu.put("name", "huanghu");
huanghu.put("age", 25);
huanghu.put("interest", new String[]{"shopping","study"});
huanghu.put("wife", "黄蓉");
DBObject huanghuAddress = new BasicDBObject();
huanghuAddress.put("address", "guangdong");
huanghu.put("home", huanghuAddress);

DBObject houchangren = new BasicDBObject();
houchangren.put("name", "houchangren");
houchangren.put("age", 26);
houchangren.put("interest", new String[]{"dota","dq"});
DBObject houchangrenAddress = new BasicDBObject();
houchangrenAddress.put("address", "shandong");
houchangren.put("home", houchangrenAddress);

DBObject wangjuntao = new BasicDBObject();
wangjuntao.put("name", "wangjuntao");
wangjuntao.put("age", 27);
wangjuntao.put("interest", new String[]{"sport","study"});
wangjuntao.put("wife", "王语嫣");
DBObject wangjuntaoAddress = new BasicDBObject();
wangjuntaoAddress.put("address", "hebei");
wangjuntao.put("home", wangjuntaoAddress);

DBObject miaojiagui = new BasicDBObject();
miaojiagui.put("name", "miaojiagui");
miaojiagui.put("age", 28);
miaojiagui.put("interest", new String[]{"hadoop","study","linux"});
miaojiagui.put("wife", null);
DBObject miaojiaguiAddress = new BasicDBObject();
miaojiaguiAddress.put("address", "未知");
miaojiagui.put("home", miaojiaguiAddress);

DBObject longzhen = new BasicDBObject();
longzhen.put("name", "longzhen");
longzhen.put("age", 29);
longzhen.put("interest", new String[]{"study","cook"});
longzhen.put("wife", null);
DBObject longzhenAddress = new BasicDBObject();
longzhenAddress.put("address", "sichuan");
longzhen.put("home", longzhenAddress);

user.insert(zhangguochen);
user.insert(jindazhong);
user.insert(yangzhi);
user.insert(diaoyouwei);
user.insert(cuichongfei);
user.insert(huanghu);
user.insert(houchangren);
user.insert(wangjuntao);
user.insert(miaojiagui);
user.insert(longzhen);
}

public void testRemove() {
user.drop();
}

/**
 * 打印数据
 * @param object
 */
public void print(Object object){
System.out.println(object);
}

/**
 * 打印列表
 * @param objectList
 */
public void printList(List<DBObject> objectList) {
for(Object object:objectList) {
print(object);
}
}
}

以上代码小伙伴们直接拷贝到相关工具就可以测试运行哦!!!!

3.以下代码展示了mongodb和spring的集成的使用,主要是mongoTemplate类的使用。

import static org.springframework.data.mongodb.core.query.Criteria.where;
import java.util.ArrayList;
import java.util.List;
import com.jd.bse.incubator.mongo.entity.Home;
import com.jd.bse.incubator.mongo.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:/spring/mongodb-config.xml"})//这里要用到mongodb的配置文件
public class MongoSpringTest{

@Autowired
private MongoTemplate mongoTemplate;

/**
 * 插入用户信息
 */
@Test
public void testAddUser() {
User zhanggc = new User();
zhanggc.setName1("zhangguochen");
zhanggc.setAge(29);
List<String> interests = new ArrayList<String>();
interests.add("stuty");
interests.add("hadoop");
zhanggc.setInterest(interests);
Home home = new Home("henan");
zhanggc.setHome(home);

//插入数据
mongoTemplate.insert(zhanggc);
}

/**
 * 查询用户信息
 */
@Test
public void testQueryUser() {
//查询主要用到Query和Criteria两个对象
Query query = new Query();
Criteria criteria = where("age").gt(22);

// criteria.and("name").is("cuichongfei");等于
// List<String> interests = new ArrayList<String>();
// interests.add("study");
// interests.add("linux");
// criteria.and("interest").in(interests);   in查询
// criteria.and("home.address").is("henan"); 内嵌文档查询
// criteria.and("").exists(false);           列存在
// criteria.and("").lte();                   小于等于
// criteria.and("").regex("");               正则表达式
// criteria.and("").ne("");                  不等于

query.addCriteria(criteria);
List<User> userList1 = mongoTemplate.find(query, User.class);
printList(userList1);

//排序查询sort方法,按照age降序排列
// query.sort().on("age", Order.DESCENDING);
// List<User> userList2 = mongoTemplate.find(query, User.class);
// printList(userList2);

//指定字段查询,只查询age和name两个字段
// query.fields().include("age").include("name");
// List<User> userList3 = mongoTemplate.find(query, User.class);
// printList(userList3);

//分页查询
// query.skip(2).limit(3);
// List<User> userList4 = mongoTemplate.find(query, User.class);
// printList(userList4);

//查询所有
// printList(mongoTemplate.findAll(User.class));

//统计数据量
// System.out.println(mongoTemplate.count(query, User.class));

}

/**
 * 更新用户数据
 */
@Test
public void testUpdateUser() {
//update(query,update,class)
//Query query:需要更新哪些用户,查询参数
//Update update:操作符,需要对数据做什么更新
//Class class:实体类

//更新age大于24的用户信息
Query query = new Query();
query.addCriteria(where("age").gt(24));

Update update = new Update();
//age值加2
update.inc("age", 2);
// update.set("name", "zhangsan"); 直接赋值
// update.unset("name");           删去字段   
// update.push("interest", "java"); 把java追加到interest里面,interest一定得是数组
// update.pushAll("interest", new String[]{".net","mq"}) 用法同push,只是pushAll一定可以追加多个值到一个数组字段内
// update.pull("interest", "study"); 作用和push相反,从interest字段中删除一个等于value的值
// update.pullAll("interest", new String[]{"sing","dota"})作用和pushAll相反
// update.addToSet("interest", "study") 把一个值添加到数组字段中,而且只有当这个值不在数组内的时候才增加
// update.rename("oldName", "newName") 字段重命名

//只更新第一条记录,age加2,name值更新为zhangsan
mongoTemplate.updateFirst(query, new Update().inc("age", 2).set("name", "zhangsan"), User.class);

//批量更新,更新所有查询到的数据
mongoTemplate.updateMulti(query, update, User.class);

}

/**
 * 测试删除数据
 */
@Test
public void testRemoveUser() {
Query query = new Query();
// query.addCriteria(where("age").gt(22));
Criteria criteria = where("age").gt(22);
//删除年龄大于22岁的用户
query.addCriteria(criteria);
mongoTemplate.remove(query, User.class);
}

public MongoTemplate getMongoTemplate() {
return mongoTemplate;
}

public void setMongoTemplate(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}

public void printList(List<User> userList) {
System.out.println("**********************************************************************************************************");
for(User user:userList) {
System.out.println(user);
}
System.out.println("**********************************************************************************************************");
}
}

4.以下是/mongodb-config.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">

<context:component-scan base-package="com.jd.mongo"/>

<context:annotation-config />

<!-- config db property file location -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 
      <property name="locations">  
           <list>  
              <value>classpath:conf/mongo.properties</value>  
         </list>  
      </property>  
   </bean> 
   
    <!-- 定义mongo对象,对应的是mongodb官方jar包中的Mongo -->
<mongo:mongo host="${mongo.host.job}" port="${mongo.port.job}"/>

<!-- 一些连接属性的设置,属性意义详见http://www.cnblogs.com/zsuxiong/archive/2012/09/05/2671960.html --> 
<!--<mongo:mongo id="mongo" replica-set="localhost:27017">
       <mongo:options
             connections-per-host="${mongo.connectionsPerHost}"
             threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
             connect-timeout="${mongo.connectTimeout}"
             max-wait-time="${mongo.maxWaitTime}"
             auto-connect-retry="${mongo.autoConnectRetry}"
             socket-keep-alive="${mongo.socketKeepAlive}"
             socket-timeout="${mongo.socketTimeout}"
             slave-ok="${mongo.slaveOk}"
             write-number="1"
             write-timeout="0"
             write-fsync="true"/>        
        </mongo:mongo>-->

<!-- mongo的工厂,通过它来取得mongo实例,dbname为mongodb的数据库名,没有的话会自动创建 -->
<mongo:db-factory id="mongoDbFactory" dbname="${mongo.dbname.job}" username="${mongo.username.job}" password="${mongo.password.job}" mongo-ref="mongo"/>

<!-- mongodb的主要操作对象,所有对mongodb的增删改查的操作都是通过它完成 -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>
</beans>


mongo.properties,也就是mongodb的连接信息

mongo.host.job=192.168.232.62:27017
mongo.dbname.job=erp
mongo.needauth.job=true
mongo.username.job=erp
mongo.password.job=erp

时间: 2024-10-15 01:05:34

Mongodb java api操作的相关文章

MongoDB Java API操作很全的整理

MongoDB 是一个基于分布式文件存储的 数据库.由 C++ 语言编写,一般生产上建议以共享分片的形式来部署. 但是MongoDB官方也提供了其它语言的客户端操作API.如下图所示: 提供了C.C++.C#..net.GO. java.Node.js.PHP.python.scala等各种语言的版本. MongoDB的操作分为同步操作和异步操作一.同步操作API 官方 JAVA API的路径:https://docs.mongodb.com/ecosystem/drivers/java/ 我们

Java API操作HDFS

HDFS是存储数据的分布式文件系统,对HDFS的操作,就是对文件系统的操作,除了用HDFS的shell命令对文件系统进行操作,我们也可以利用Java API对文件系统进行操作,比如文件的创建.删除.修改权限等等,还有文件夹的创建.删除.重命名等等. 使用Java API对文件系统进行操作主要涉及以下几个类: 1.Configuration类:该类的对象封装了客户端或者服务端的配置. 2.FileSystem类:该类的对象是一个文件系统对象,可以利用该对象的一些方法来对文件进行操作,FileSys

Hadoop读书笔记(三)Java API操作HDFS

Hadoop读书笔记(一)Hadoop介绍:http://blog.csdn.net/caicongyang/article/details/39898629 Hadoop读书笔记(二)HDFS的shell操作:http://blog.csdn.net/caicongyang/article/details/41253927 JAVA URL 操作HDFS OperateByURL.java package hdfs; import java.io.InputStream; import jav

HDFS基础和java api操作

1. 概括 适合一次写入多次查询情况,不支持并发写情况 通过hadoop shell 上传的文件存放在DataNode的block中,通过linux shell只能看见block,看不见文件(HDFS将客户端的大文件存放在很多节点的数据块中,Block本质上是一个逻辑概念,它是hdfs读写数据的基本单位) HDFS中,如果一个文件小于一个数据块的大小,并不占用整个数据块存储空间 2. fs 可以使用hdfs shell操作hdfs,常用 fs命令如下: eg: hadoop fs -cat fi

使用java api操作Hadoop文件 Robbin

1 package cn.hadoop.fs; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.net.URI; 6 import java.net.URISyntaxException; 7 8 import org.apache.hadoop.conf.Configuration; 9 import org.apache.hadoop.fs.FSDataOutputStream; 10 i

hive-通过Java API操作

通过Java API操作hive,算是测试hive第三种对外接口 测试hive 服务启动 1 package org.admln.hive; 2 3 import java.sql.SQLException; 4 import java.sql.Connection; 5 import java.sql.ResultSet; 6 import java.sql.Statement; 7 import java.sql.DriverManager; 8 9 public class testHiv

大数据技术之_20_Elasticsearch学习_01_概述 + 快速入门 + Java API 操作 + 创建、删除索引 + 新建、搜索、更新删除文档 + 条件查询 + 映射操作

一 概述1.1 什么是搜索?1.2 如果用数据库做搜索会怎么样?1.3 什么是全文检索和 Lucene?1.4 什么是 Elasticsearch?1.5 Elasticsearch 的适用场景1.6 Elasticsearch 的特点1.7 Elasticsearch 的核心概念1.7.1 近实时1.7.2 Cluster(集群)1.7.3 Node(节点)1.7.4 Index(索引 --> 数据库)1.7.5 Type(类型 --> 表)1.7.6 Document(文档 -->

HBase 6、用Phoenix Java api操作HBase

开发环境准备:eclipse3.5.jdk1.7.window8.hadoop2.2.0.hbase0.98.0.2.phoenix4.3.0 1.从集群拷贝以下文件:core-site.xml.hbase-site.xml.hdfs-site.xml文件放到工程src下 2.把phoenix的phoenix-4.3.0-client.jar和phoenix-core-4.3.0.jar添加到工程classpath 3.配置集群中各节点的hosts文件,把客户端的hostname:IP添加进去

Java API操作ZooKeeper

创建会话 1 package org.zln.zk; 2 3 import org.apache.zookeeper.WatchedEvent; 4 import org.apache.zookeeper.Watcher; 5 import org.apache.zookeeper.ZooKeeper; 6 7 import java.io.IOException; 8 9 /** 10 * Created by sherry on 16/8/27. 11 */ 12 public class