zoukankan      html  css  js  c++  java
  • 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();
        }
    }
  • 相关阅读:
    程序员代码面试指南:IT名企算法与数据结构题目最优解
    经典排序算法
    Log4j输出格式控制--log4j的PatternLayout参数含义
    常用数据库4 mongodb
    常用数据库2 sqlite及SQL注入
    面试常问-数据库索引实现原理
    自定义web框架
    HTML|CSS之布局相关总结
    C++模板类练习题
    C++中的运算符重载练习题
  • 原文地址:https://www.cnblogs.com/zhanggl/p/5175897.html
Copyright © 2011-2022 走看看