JAVA--語周波数統計wordcountの実現
7647 ワード
頻度統計需要:
1.非英字を区切りとして、1つのファイル内のすべての英字フレーズを統計する必要があります(ここではスペースを例に挙げます).
2.統計結果をコンソールで出力し、mysqlデータベースとredisデータベースに保存する必要があります.
3.大文字と小文字を区別しないように、Mapキー値ペアで格納することが要求されています(コンテンツをすべて大文字にするか、すべて小文字にすることで大文字と小文字を区別しないことができます)
4.辞書形式で並べ替え
頻度統計:
JDBCリンクツールクラス:
Jedisリンクツールクラス:
redisDBプロファイル(propertiesファイル):
テストクラス:
ps:データベースに格納して操作する必要がなければ、対応するデータベースツールと方法を削除すればいい、emmm
THE END...
1.非英字を区切りとして、1つのファイル内のすべての英字フレーズを統計する必要があります(ここではスペースを例に挙げます).
2.統計結果をコンソールで出力し、mysqlデータベースとredisデータベースに保存する必要があります.
3.大文字と小文字を区別しないように、Mapキー値ペアで格納することが要求されています(コンテンツをすべて大文字にするか、すべて小文字にすることで大文字と小文字を区別しないことができます)
4.辞書形式で並べ替え
頻度統計:
package wordcount;
import redis.clients.jedis.Jedis;
import util.JDBCUnit;
import util.JedisPoolUtil;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
public class WordCount {
public void displayWordCount(String fileName) {
//
try {
//
FileReader fileReader = new FileReader(fileName);
//
BufferedReader reader = new BufferedReader(fileReader);
// TreeMap,
TreeMap tm = new TreeMap();
String readLine = null;
while((readLine = reader.readLine()) != null){
//
readLine = readLine.toLowerCase();
//
String[] str = readLine.split("[\\s]+");
// ,“+” 。
for (int i = 0; i < str.length; i++) {//
String word = str[i].trim();
if (tm.containsKey(word)) {
tm.put(word, tm.get(word) + 1);
} else {
tm.put(word, 1);
}
}
}
//
System.out.println(" :");
Iterator> it = tm.entrySet().iterator();
//
while(it.hasNext()) {
Map.Entry entry = it.next();
// redis
Jedis jedis = JedisPoolUtil.getJedis();
jedis.hset("wordcount",entry.getKey(), String.valueOf(entry.getValue()));
// mysql
Connection conn = JDBCUnit.getConnection();
String sql = "INSERT INTO wordcount(words,counts) VALUES(?,?)";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1,entry.getKey());
pst.setString(2, String.valueOf(entry.getValue()));
pst.executeUpdate();
//
System.out.println(entry.getKey() + "\t" + entry.getValue());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
JDBCリンクツールクラス:
package util;
import java.sql.*;
public class JDBCUnit {
private static final String NAME = "root";
private static final String PASSWORD = "zxc";
private static final String URL = "jdbc:mysql://localhost:3306/maven-wordcount?useUnicode=true&characterEncoding=UTF-8";
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* @return
*/
public static Connection getConnection() {
Connection connection = null;
try {
connection = DriverManager.getConnection(URL, NAME, PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
/**
*
* @param connection
* @param statement SQL
*/
public static void release(Connection connection, Statement statement) {
try {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
*
* @param connection
* @param statement SQL
* @param resultSet
*/
public static void release(Connection connection, Statement statement, ResultSet resultSet) {
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Jedisリンクツールクラス:
package util;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.io.InputStream;
import java.util.Properties;
public class JedisPoolUtil {
// private static String host = "192.168.59.150";
// private static int port = 6379;
private static JedisPool jedisPool = null;
static {
// : getClassLoader() getResourceAsStream() ,
// ,JedisPoolUtil.class ,
//
InputStream inputStream = JedisPoolUtil.class.getClassLoader().getResourceAsStream("redisDB.properties");
//Properties .properties
Properties properties = new Properties();
try {
properties.load(inputStream);
} catch (Exception e) {
System.out.println(" !");
}
String host = properties.getProperty("redis.host");
//Integer.parseInt()
Integer Max_Total = Integer.parseInt(properties.getProperty("redis.MaxTotal"));
Integer Max_Idle = Integer.parseInt(properties.getProperty("redis.MaxIdle"));
Integer Min_Idle = Integer.parseInt(properties.getProperty("redis.MinIdle"));
Integer port = Integer.parseInt(properties.getProperty("redis.port"));
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
//
jedisPoolConfig.setMaxTotal(Max_Total);
//
jedisPoolConfig.setMaxIdle(Max_Idle);
//
jedisPoolConfig.setMinIdle(Min_Idle);
//
jedisPool = new JedisPool(jedisPoolConfig, host, port);
// System.out.println(host+"
"+port+"
"+Max_Total+"
"+Max_Idle+"
"+Min_Idle);
}
public static Jedis getJedis() {
//
return jedisPool.getResource();
}
public static void release(Jedis jedis) {
if (jedis != null) {
//
jedis.close();
}
}
}
redisDBプロファイル(propertiesファイル):
redis.port=6379
redis.host=192.168.59.150
redis.MaxTotal=10
redis.MaxIdle=8
redis.MinIdle=8
テストクラス:
package wordcount;
public class Main {
public static void main(String[] args) {
String line = "C:\\Users\\Super\\Desktop\\123.txt";
//
String fileName = line.trim();
WordCount wc = new WordCount();
wc.displayWordCount(fileName);
}
}
ps:データベースに格納して操作する必要がなければ、対応するデータベースツールと方法を削除すればいい、emmm
THE END...