JAVA 实现数据导入Phoenix
需要导入的jar 包有:
实现代码:
package cn.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.util.Random; public class PhoenixTest { public static void insertDatas() { Statement stmt; Connection con; String ItemID = getItemID( 10 ); String ItemName = getItemName(10); float Price = (float) 0.1; try { con = DriverManager.getConnection("jdbc:phoenix:node11:2181" ); stmt = con.createStatement(); System.out.println("content success...."); // stmt.executeUpdate("create table test (mykey integer not null primary key, mycolumn varchar)"); stmt.executeUpdate( "create table items ( ItemID varchar not null primary key,ItemName VARCHAR NULL,Price FLOAT NULL "); for ( int i = 0; i < 1000; i++ ) //需要导入多少数据自己修改,如果数据量比较大就需要500或者1000条提交一次;或者定时提交,下次再修改代码 { stmt.executeUpdate( "upsert into items values ('" + ItemID + "','" + ItemName + "','" + Price++ + "')" ); } con.commit(); con.close(); } catch ( SQLException e ) { e.printStackTrace(); } } /** * 生成随机数当作getItemID * * @return */ public static String getItemID( int n ) { String val = ""; Random random = new Random(); for ( int i = 0; i < n; i++ ) { String str = random.nextInt( 2 ) % 2 == 0 ? "num" : "char"; if ( "char".equalsIgnoreCase( str ) ) { // 产生字母 int nextInt = random.nextInt( 2 ) % 2 == 0 ? 65 : 97; // System.out.println(nextInt + "!!!!"); 1,0,1,1,1,0,0 val += (char) ( nextInt + random.nextInt( 26 ) ); } else if ( "num".equalsIgnoreCase( str ) ) { // 产生数字 val += String.valueOf( random.nextInt( 10 ) ); } } return val; } /** * 生产ItemName随机函数 * @param length * @return */ public static String getItemName( int length ){ String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; Random random = new Random(); StringBuffer sb = new StringBuffer(); for ( int i = 0; i < length; i++ ) { int number = random.nextInt( base.length() ); sb.append( base.charAt( number ) ); } return sb.toString(); }
/**
* time run
*/
public static void runTimerByTime(){
Runnable runnable = new Runnable() {
public void run() {
System.out.println("...........hellon.....");
}
};
ScheduledExecutorService service = Executors
.newSingleThreadScheduledExecutor();
service.scheduleAtFixedRate(runnable, 0, 3, TimeUnit.MILLISECONDS);
}
public static void main( String[] args ){ insertDatas(); } }