package com.djhu.redis.test; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class JedisFactory { // 最大可用连接数,默认值为8,如果赋值为-1则表示不限制 private static int MAX_TOTAL = 256; // 最大空闲连接数,默认值为8 private static int MAX_IDLE = 32; // 最小空闲连接数 private static int MIN_IDLE = 4; // 最大等待连接毫秒数,默认值为-1表示永不超时 private static int MAX_WAIT = 3000; // 连接redis超时时间 private static int TIMEOUT = 3000; // true表示验证连接 private static boolean TEST_ON_BORROW = true; //连接池 private static JedisPool jedisPool = null; public static void initJedisPool(String IP, int port, String password) { try { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(MAX_TOTAL); config.setMaxIdle(MAX_IDLE); config.setMinIdle(MIN_IDLE); config.setMaxWaitMillis(MAX_WAIT); config.setTestOnBorrow(TEST_ON_BORROW); jedisPool = new JedisPool(config, IP, port, TIMEOUT, password); } catch (Exception e) { e.printStackTrace(); } } public synchronized static Jedis getConnection() { try { if (jedisPool != null) { Jedis resource = jedisPool.getResource(); return resource; } else { return null; } } catch (Exception e) { e.printStackTrace(); return null; } } public static void returnResource(final Jedis jedis) { if (jedis != null) { jedis.close(); } } public static void main(String[] args) { initJedisPool("localhost",6379,null); Jedis redis = getConnection(); redis.select(1); //redis.set("key01", "a"); //redis.set("key02", "b"); //redis.set("key03", "c"); System.out.print(redis.dbSize()); returnResource(redis); } }
Category Archives: Java
Redis入门之增删改查(Java)
1、下载驱动
jedis驱动源码地址
jedis驱动下载地址
2、测试代码
package com.djhu.redis.test; import java.util.HashMap; import java.util.List; import java.util.Map; import redis.clients.jedis.Jedis; public class ConnectionTest { public static Jedis getConnection(String ip, int port) { Jedis jedis = new Jedis(ip, port); //jedis.auth("password"); return jedis; } public static void cleanAll(Jedis jedis) { jedis.flushDB(); } public static void stringTest(Jedis jedis) { jedis.set("key01", "a"); jedis.set("key02", "b"); jedis.set("key03", "c"); jedis.mset("key04", "d", "key05", "e", "key06", "f"); jedis.del("key04"); System.out.println("key01 is " + jedis.get("key01")); System.out.println("key04 is " + jedis.get("key04")); } public static void mapTest(Jedis jedis) { Map<String, String> map = new HashMap<String, String>(); map.put("username", "hansen"); map.put("usersex", "male"); jedis.hmset("mapkey01", map); map.put("username", "neohope"); map.put("usersex", "male"); jedis.hmset("mapkey02", map); map.put("username", "tuzi"); map.put("usersex", "female"); jedis.hmset("mapkey03", map); List<String> rsmap = jedis.hmget("mapkey03", "username", "usersex"); System.out.println("query for hmget(\"mapkey03\", \"username\", \"usersex\") is " + rsmap); jedis.hdel("mapkey02", "usersex"); System.out.println("query for hmget jedis.hmget(\"mapkey02\", \"username\") is "+jedis.hmget("mapkey02", "username")); System.out.println("query for jedis.hmget(\"mapkey02\", \"usersex\") is " + jedis.hmget("mapkey02", "usersex")); System.out.println("query for jedis.hlen(\"mapkey01\") is " + jedis.hlen("mapkey01")); System.out.println("query for jedis.exists(\"mapkey01\") is " + jedis.exists("mapkey01")); System.out.println("query for jedis.hkeys(\"mapkey01\") is " + jedis.hkeys("mapkey01")); System.out.println("query for jedis.hvals(\"mapkey01\") is " + jedis.hvals("mapkey01")); } public static void listTest(Jedis jedis) { jedis.lpush("keylist01", "a"); jedis.lpush("keylist01", "b"); jedis.lpush("keylist01", "c"); System.out.println("keylist01 is " + jedis.lrange("keylist01", 0, -1)); } public static void setTest(Jedis jedis) { jedis.sadd("keyset01", "01"); jedis.sadd("keyset01", "02"); jedis.sadd("keyset01", "03"); jedis.sadd("keyset01", "04"); jedis.sadd("keyset01", "05"); System.out.println("query for jedis.smembers(\"keyset01\") is " + jedis.smembers("keyset01")); System.out.println("query for jedis.sismember(\"keyset01\", \"06\") is " + jedis.sismember("keyset01", "06")); System.out.println("query for jedis.scard(\"keyset01\") is " + jedis.scard("keyset01")); } public static void sortTest(Jedis jedis) { jedis.rpush("keylist02", "16"); jedis.lpush("keylist02", "8"); jedis.lpush("keylist02", "4"); jedis.lpush("keylist02", "2"); jedis.lpush("keylist02", "1"); System.out.println("keylist02 is " + jedis.lrange("keylist02", 0, -1)); jedis.sort("keylist02"); System.out.println("after sort keylist02 is " + jedis.lrange("keylist02", 0, -1)); } public static void main(String[] args) { Jedis jedis = getConnection("localhost", 6379); cleanAll(jedis); stringTest(jedis); mapTest(jedis); listTest(jedis); sortTest(jedis); setTest(jedis); jedis.close(); } }
Hadoop增删改查(Java)
需要的jar包在hadoop里都可以找到,下面的例子中,至少需要这些jar包:
commons-cli-1.2.jar commons-collections-3.2.1.jar commons-configuration-1.6.jar commons-io-2.4.jar commons-lang-2.6.jar commons-logging-1.1.3.jar guava-11.0.2.jar hadoop-auth-2.7.1.jar hadoop-common-2.7.1.jar hadoop-hdfs-2.7.1.jar htrace-core-3.1.0-incubating.jar log4j-1.2.17.jar protobuf-java-2.5.0.jar servlet-api.jar slf4j-api-1.7.10.jar slf4j-log4j12-1.7.10.jar
代码如下:
package com.neohope.hadoop.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class HDFSTest { static Configuration hdfsConfig; static { hdfsConfig = new Configuration(); hdfsConfig.addResource(new Path("etc/hadoop/core-site.xml")); hdfsConfig.addResource(new Path("etc/hadoop/hdfs-site.xml")); } // 创建文件夹 public static void createDirectory(String dirPath) throws IOException { FileSystem fs = FileSystem.get(hdfsConfig); Path p = new Path(dirPath); try { fs.mkdirs(p); } finally { fs.close(); } } // 删除文件夹 public static void deleteDirectory(String dirPath) throws IOException { FileSystem fs = FileSystem.get(hdfsConfig); Path p = new Path(dirPath); try { fs.deleteOnExit(p); } finally { fs.close(); } } // 重命名文件夹 public static void renameDirectory(String oldDirPath, String newDirPath) throws IOException { renameFile(oldDirPath, newDirPath); } // 枚举文件 public static void listFiles(String dirPath) throws IOException { FileSystem hdfs = FileSystem.get(hdfsConfig); Path listf = new Path(dirPath); try { FileStatus statuslist[] = hdfs.listStatus(listf); for (FileStatus status : statuslist) { System.out.println(status.getPath().toString()); } } finally { hdfs.close(); } } // 新建文件 public static void createFile(String filePath) throws IOException { FileSystem fs = FileSystem.get(hdfsConfig); Path p = new Path(filePath); try { fs.createNewFile(p); } finally { fs.close(); } } // 删除文件 public static void deleteFile(String filePath) throws IOException { FileSystem fs = FileSystem.get(hdfsConfig); Path p = new Path(filePath); try { fs.deleteOnExit(p); } finally { fs.close(); } } // 重命名文件 public static void renameFile(String oldFilePath, String newFilePath) throws IOException { FileSystem fs = FileSystem.get(hdfsConfig); Path oldPath = new Path(oldFilePath); Path newPath = new Path(newFilePath); try { fs.rename(oldPath, newPath); } finally { fs.close(); } } // 上传文件 public static void putFile(String locaPath, String hdfsPath) throws IOException { FileSystem fs = FileSystem.get(hdfsConfig); Path src = new Path(locaPath); Path dst = new Path(hdfsPath); try { fs.copyFromLocalFile(src, dst); } finally { fs.close(); } } // 取回文件 public static void getFile(String hdfsPath, String locaPath) throws IOException { FileSystem fs = FileSystem.get(hdfsConfig); Path src = new Path(hdfsPath); Path dst = new Path(locaPath); try { fs.copyToLocalFile(false, src, dst, true); } finally { fs.close(); } } // 读取文件 public static void readFile(String hdfsPath) throws IOException { FileSystem hdfs = FileSystem.get(hdfsConfig); Path filePath = new Path(hdfsPath); InputStream in = null; BufferedReader buff = null; try { in = hdfs.open(filePath); buff = new BufferedReader(new InputStreamReader(in)); String str = null; while ((str = buff.readLine()) != null) { System.out.println(str); } } finally { buff.close(); in.close(); hdfs.close(); } } public static void main(String[] args) throws IOException { System.setProperty("HADOOP_USER_NAME", "hadoop"); // createDirectory("hdfs://hadoop-master:9000/usr"); // createDirectory("hdfs://hadoop-master:9000/usr/hansen"); // createDirectory("hdfs://hadoop-master:9000/usr/hansen/test"); // renameDirectory("hdfs://hadoop-master:9000/usr/hansen/test","hdfs://hadoop-master:9000/usr/hansen/test01"); // createFile("hdfs://hadoop-master:9000/usr/hansen/test01/hello.txt"); // renameFile("hdfs://hadoop-master:9000/usr/hansen/test01/hello.txt","hdfs://hadoop-master:9000/usr/hansen/test01/hello01.txt"); // putFile("hello.txt","hdfs://hadoop-master:9000/usr/hansen/test01/hello02.txt"); // getFile("hdfs://hadoop-master:9000/usr/hansen/test01/hello02.txt","hello02.txt"); // readFile("hdfs://hadoop-master:9000/usr/hansen/test01/hello02.txt"); listFiles("hdfs://hadoop-master:9000/usr/hansen/test01/"); } }
MongoDB查询使用Codec的简单示例(java)
1、数据准备
db.person.insert({"name":"neo","age":"26","sex":"male"}) db.person.insert({"name":"joe","age":"28","sex":"male"})
2、使用Codec
class Person { public ObjectId _id; public double Age; public String Name; public String Sex; public Person(ObjectId _id, String Name, double Age, String Sex) { this._id=_id; this.Name=Name; this.Age=Age; this.Sex=Sex; } } class PersonCodec implements Codec<Person> { private final CodecRegistry codecRegistry; public PersonCodec(final CodecRegistry codecRegistry) { this.codecRegistry = codecRegistry; } @Override public void encode(BsonWriter writer, Person t, EncoderContext ec) { writer.writeStartDocument(); writer.writeName("_id"); writer.writeObjectId(t._id); writer.writeName("name"); writer.writeString(t.Name); writer.writeName("age"); writer.writeDouble(t.Age); writer.writeName("sex"); writer.writeString(t.Sex); writer.writeEndDocument(); } @Override public Class<Person> getEncoderClass() { return Person.class; } @Override public Person decode(BsonReader reader, DecoderContext dc) { reader.readStartDocument(); reader.readName(); ObjectId _id = reader.readObjectId(); reader.readName(); String name = reader.readString(); reader.readName(); double age = reader.readDouble(); reader.readName(); String sex =reader.readString(); reader.readEndDocument(); return new Person(_id,name,age,sex); } } class PersonCodecProvider implements CodecProvider { @Override public <T> Codec<T> get(Class<T> type, CodecRegistry cr) { if (type == Person.class) { return (Codec<T>) new PersonCodec(cr); } return null; } } public class CodecTest { private static void testCodec() { String[] hosts = {"127.0.0.1"}; int port = 27017; String user = null; String password = null; String database = "test"; CodecRegistry codecRegistry = CodecRegistries.fromRegistries( CodecRegistries.fromProviders(new PersonCodecProvider()), MongoClient.getDefaultCodecRegistry()); MongoClient mongoClient = getConnection(hosts,port,user,password,database,codecRegistry); MongoDatabase db = mongoClient.getDatabase("test"); MongoCollection<Person> collection = db.getCollection("person",Person.class); FindIterable<Person> iterable = collection.find(); MongoCursor<Person> cursor = iterable.iterator(); while (cursor.hasNext()) { Person p = cursor.next(); System.out.println("personName: " + p.Name); } } private static MongoClient getConnection(String[] hosts, int port, String user, String password, String database, CodecRegistry codecRegistry) { MongoClientOptions mongoClientOptions = new MongoClientOptions.Builder() .connectionsPerHost(100) .threadsAllowedToBlockForConnectionMultiplier(5) .maxWaitTime(1000 * 60 * 2) .connectTimeout(1000 * 10) .socketTimeout(0) .socketKeepAlive(false) .readPreference(ReadPreference.primary()) .writeConcern(WriteConcern.ACKNOWLEDGED) .codecRegistry(codecRegistry) .build(); List<ServerAddress> mongoAddresses = new ArrayList<ServerAddress>(); for (String host : hosts) { mongoAddresses.add(new ServerAddress(host, port)); } List<MongoCredential> mongoCredentials = null; if (user != null && !user.isEmpty() && password != null && !password.isEmpty()) { mongoCredentials = new ArrayList<MongoCredential>(); mongoCredentials.add(MongoCredential.createMongoCRCredential(user, database, password.toCharArray())); } if(mongoCredentials==null) { return new MongoClient(mongoAddresses, mongoClientOptions); } else { return new MongoClient(mongoAddresses, mongoCredentials, mongoClientOptions); } } }
MongoDB的MapReduce简单示例(java)
1、数据准备
db.sell.insert({"price":8.0,"amount":500.0,"status":"a"}) db.sell.insert({"price":8.0,"amount":450.0,"status":"a"}) db.sell.insert({"price":8.0,"amount":400.0,"status":"a"}) db.sell.insert({"price":9.0,"amount":350.0,"status":"a"}) db.sell.insert({"price":9.0,"amount":300.0,"status":"a"}) db.sell.insert({"price":9.0,"amount":250.0,"status":"a"}) db.sell.insert({"price":9.0,"amount":200.0,"status":"a"}) db.sell.insert({"price":10.0,"amount":150.0,"status":"d"}) db.sell.insert({"price":10.0,"amount":100.0,"status":"d"}) db.sell.insert({"price":10.0,"amount":50.0,"status":"d"}) db.sell.insert({"price":10.0,"amount":0.0,"status":"d"})
2、MapReduce
private static void testMapReduce3x() { MongoClient mongoClient = new MongoClient("localhost", 27017); MongoDatabase db = mongoClient.getDatabase("test"); MongoCollection collection = db.getCollection("sell"); String map = "function(){emit(this.price,this.amount);}"; String reduce = "function(key, values){return Array.sum(values)}"; MapReduceIterable out = collection.mapReduce(map, reduce); MongoCursor cursor = out.iterator(); while (cursor.hasNext()) { System.out.println(cursor.next()); } } private static void testMapReduce2x() { MongoClient mongoClient = new MongoClient("localhost", 27017); MongoDatabase db = mongoClient.getDatabase("test"); BasicDBObject query=new BasicDBObject("status","a"); DBCollection dbcollection = mongoClient.getDB("test").getCollection("sell"); String map = "function(){emit(this.price,this.amount);}"; String reduce = "function(key, values){return Array.sum(values)}"; MapReduceCommand cmd = new MapReduceCommand(dbcollection, map, reduce, "outputCollection", MapReduceCommand.OutputType.INLINE, query); MapReduceOutput out2 = dbcollection.mapReduce(cmd); for (DBObject o : out2.results()) { System.out.println(o.toString()); } }
MongoDB入门之增删改查(Java)
测试代码,请重构
//枚举数据库 private static void listDB() { MongoClient mongoClient = new MongoClient("localhost", 27017); for (String dbName : mongoClient.listDatabaseNames()) { System.out.println("dbName: " + dbName); } } //枚举collection private static void listCollection() { MongoClient mongoClient = new MongoClient("localhost", 27017); MongoDatabase db = mongoClient.getDatabase("test"); for (String collectionName : db.listCollectionNames()) { System.out.println("collectionName: " + collectionName); } } //查询全部数据 private static void testQueryAll() { MongoClient mongoClient = new MongoClient("localhost", 27017); MongoDatabase db = mongoClient.getDatabase("test"); MongoCollection collection = db.getCollection("person"); BasicDBObject query = new BasicDBObject(); FindIterable iterable = collection.find(query); MongoCursor cursor = iterable.iterator(); while (cursor.hasNext()) { org.bson.Document person = (org.bson.Document)cursor.next(); System.out.println(person.get("name")); System.out.println(person.toString()); } cursor.close(); } //按条件查询数据 private static void testQuery() { MongoClient mongoClient = new MongoClient("localhost", 27017); MongoDatabase db = mongoClient.getDatabase("test"); MongoCollection collection = db.getCollection("person"); BasicDBObject query = new BasicDBObject("name","Joe"); FindIterable iterable = collection.find(query); MongoCursor cursor = iterable.iterator(); while (cursor.hasNext()) { org.bson.Document person = (org.bson.Document)cursor.next(); System.out.println(person.get("name")); System.out.println(person.toString()); } cursor.close(); } //插入 private static void testInsert() { MongoClient mongoClient = new MongoClient("localhost", 27017); MongoDatabase db = mongoClient.getDatabase("test"); MongoCollection collection = db.getCollection("person"); Document doc = new Document(); doc.put("name", "tuzi"); doc.put("age", 27); doc.put("sex", "Female"); collection.insertOne(doc); } //删除 private static void testDelete() { MongoClient mongoClient = new MongoClient("localhost", 27017); MongoDatabase db = mongoClient.getDatabase("test"); MongoCollection collection = db.getCollection("person"); BasicDBObject query = new BasicDBObject("name", "tuziki"); collection.deleteMany(query); } //更新 private static void testUpdate() { MongoClient mongoClient = new MongoClient("localhost", 27017); MongoDatabase db = mongoClient.getDatabase("test"); MongoCollection collection = db.getCollection("person"); BasicDBObject query = new BasicDBObject("name", "tuzi"); BasicDBObject newDocument = new BasicDBObject(); newDocument.put("name", "tuziki"); BasicDBObject updateObj = new BasicDBObject(); updateObj.put("$set", newDocument); collection.updateMany(query, updateObj); }
获取Eclipse执行文件根目录
Eclipse插件获取Eclipse的根目录
String eclipseRoot = Platform.getInstallLocation().getURL().toString(); eclipseRoot = eclipseRoot.replace("file:/", "");
Eclipse插件获取Workspace根目录
//方法1 String workspaceRoot= Platform.getInstanceLocation().getURL().toString(); workspaceRoot = workspaceRoot.replace("file:/", ""); //方法2 String path = Activator.getDefault().getStateLocation().makeAbsolute().toFile().getAbsolutePath();
Eclipse插件获取User根目录
String userHome = Platform.getUserLocation().getURL().toString(); userHome = userHome.replace("file:/", "");
Elclipse插件获取Bundle的OSGI路径
//方法1 String bundlePath = Activator.getDefault().getBundle().getLocation(); String pathBundle = bundlePath.replace("reference:file:/",""); //方法2 Bundle bundle = Platform.getBundle("bundle id"); URL urlentry = bundle.getEntry("bundle resource path"); String strEntry = FileLocator.toFileURL(urlentry).getPath(); //方法3 String pathClass = KeyHandler.class.getResource("resource path").getFile();
Build Jetty Lesson101
1. Download source code from Eclipse.
For example, I used this one:
From here: http://download.eclipse.org/jetty/ Get this one: jetty-8.1.15.v20140411
2. Read this:
http://docs.codehaus.org/display/JETTY/Building+from+Source
3. Prepare JDK and Maven:
SET JAVA_HOME=C:\Languages\Java\JDK\jdk_x86_1.6.0_34 SET MAVEN_HOME=C:\Languages\Java\JavaTools\apache-maven-3.0.4 SET PATH=%MAVEN_HOME%\bin;%JAVA_HOME%\bin;%PATH% CMD
4. Run “mvn install”
5. Use eclipse to import maven project
Build Tomcat Lesson101
1. Download one source tag from Apache
For example, I used this tag:
http://svn.apache.org/repos/asf/tomcat/tc6.0.x/tags/TOMCAT_6_0_41
2. Read this:
http://tomcat.apache.org/tomcat-6.0-doc/building.html
3. Prepare JDK and Ant
SET JAVA_HOME=C:\Languages\Java\JDK\jdk_x86_1.6.0_34 SET ANT_HOME=C:\Languages\Java\JavaTools\apache-ant-1.9.0 SET PATH=%ANT_HOME%\bin;%JAVA_HOME%\bin;%PATH% CMD
4. Copy build.properties.default to build.properties
5. Edit build.properties and set base.path
base.path=the path to store thirdpart libs
6. Run “Ant download”
7. Run “Ant”
8. Rename files
move eclipse.classpath .classpath move eclipse.project .project
9. Use eclipse to import this project
10. Set break point and debug
JAVA常用缩写
今天整理的一份文档,希望对大家有所帮助
AAA: Authentication, Authorization, Accounting
AWT: Abstract Window Toolkit (AWT VS. Swing VS. SWT)
AOP: ASPect Oriented Programming
BMP: Bean-Managed Persistent
CMP: Container-Managed Persistent
CORBA: Common Object Request Broker Architecture
DI: Dependency Injection
DTD: Document Type Definition
EJB: Enterprise Java Beans(Session Beans, Entity Beans, Message-driven Beans)
IDL: Interface Definition Language
IIOP: Internet Inter-ORB Protocol
IOC: Inversion Of Control
I18N: InternationalizationN
L10N: LocalizatioN
J2EE: Java2 Enterprise Edition(EJB, JTA, JDBC, JCA, JMX, JNDI, JMS, JavaMail, Servlet, JSP)
J2ME: Java2 Micro Edition
J2SE: Java2 Standard Edition
JAF: Java Action FrameWork
JCA: Java Cryptography Architecture
JCP: Java Community Process
JDBC: Java Data Base Connectivity
JDK: Java Development Kit
JDO: Java Data Object
JFC: Java Foundation Classes
JMS: Java Message Service
JNDI: Java Naming And Directory Interface
JNI: Java Native Interface
JPA: Java Persistence API
JRE: Java Runtime Environment
JSDK: Java Software Development Kit
JSF: Java Server Faces
JSP: Java Server Pages
JTA: Java Transaction API
JTS: Java Transaction Service
JVM: Java Virtual Machine(JRE VS. JNode VS. SableVM)
MVC: Model, View, Controller
OMG: Object Menagement Group
OCP: Open Closed Principle (Software entities should be open for extension, but closed for modification.)
OR Mapping: Object Relation Mapping
PI: Processing Instruction
PO: Persisent Object
POJO: Plain Ordinary Java Object
RADIUS: Remote Authentication Dial In User Service
RMI: Remote Method Invocation
RTTI: RunTime Type Identification
SOA: Service-Oriented Architecture
SPI: Service Provider Interface
WFC: Windows Foundation Classes for Java
WORA: Write Once, Run Anywhere