天天看點

kudu select data api

課程連結: http://edu.51cto.com/course/15174.html

kudu對表資料進行讀取操作

重要調優參數:

defaultAdminOperationTimeoutMs:使用者對kudu的createTable,insertTable的操作連接配接逾時數設定

String tableName = "wyh_main";
        KuduClient client = new KuduClient.KuduClientBuilder("hadoop4,hadoop5,hadoop6").defaultAdminOperationTimeoutMs(600000).build();
        
        // 擷取需要查詢資料的列
        List<String> projectColumns = new ArrayList<String>();
        projectColumns.add("id");
        projectColumns.add("user_id");
        projectColumns.add("start_time");
        projectColumns.add("name");

        // 簡單的讀取
		// KuduScanner scanner = client.newScannerBuilder(table).setProjectedColumnNames(projectColumns).build();
		
        // 根據主鍵設定讀取的上限和下限
        Schema schema = table.getSchema();
        PartialRow lower = schema.newPartialRow();
        lower.addLong("id", 10);
        lower.addLong("user_id", 10);
        lower.addLong("start_time", 50);
        PartialRow upper = schema.newPartialRow();
        upper.addLong("id", 50);
        upper.addLong("user_id", 50);
        upper.addLong("start_time", 50);
        KuduScanner scanner = client.newScannerBuilder(table)
                .setProjectedColumnNames(projectColumns)
                .lowerBound(lower)
                .exclusiveUpperBound(upper)
                .build();
        while (scanner.hasMoreRows()) {
            RowResultIterator results = scanner.nextRows();
            
            // 18個tablet,每次從tablet中擷取的資料的行數
            int numRows = results.getNumRows();
            System.out.println("numRows is : " + numRows);
            while (results.hasNext()) {
                RowResult result = results.next();
                long id = result.getLong(0);
                long user_id = result.getLong(1);
                long start_time = result.getLong(2);
                String name = result.getString(3);
                System.out.println("id is : " + id + "===user_id is : " + user_id + "===start_time" + start_time + "===name is : " + name);
            }
            System.out.println("--------------------------------------");
        }