大數據基本的 增刪改差 Demo(資源來自網絡本人屬於收藏總結)
阿新 • • 發佈:2018-03-15
mov mage 基本 set http port exc epo param
今天第一天接觸大數據 做一些基本的增刪改差,來這總結一下 ,資源來自網絡本人屬於收藏總結
這一篇只有demo合運行結果 ,下一篇有一點對代碼的分析合自己的理解
先看一下數據庫 的數據
查詢數據
1 public static void main(String args[]) throws TRSException { 2 TRSConnection conn = new TRSConnection("http://127.0.0.1:5555", "admin", "trsadmin", new ConnectParams()); 3 SearchParams param = newSearchParams(); 4 param.setReadColumns("rowid;日期;標題;作者;正文"); 5 param.setColorColumns("正文"); 6 param.setCutSize(200); 7 TRSResultSet resultSet = conn.executeSelect("demo", "正文:街舞", 0, 10, param); 8 9 System.out.println(resultSet.getNumFound() + " records found");10 for (int i = 0; i < resultSet.size(); i++) { 11 resultSet.moveNext();//移動到檢索結果記錄集的下一條記錄。 12 TRSRecord re = resultSet.get(); 13 System.out.println(); 14 System.out.println("result_" + i); 15 System.out.println("rowid:" + re.getString("rowid"));16 System.out.println("日期:" + re.getString("日期")); 17 System.out.println("標題:" + re.getString("標題")); 18 System.out.println("作者:" + re.getString("作者")); 19 System.out.println("正文:" + re.getString("正文")); 20 } 21 conn.close(); 22 }
後臺輸出結果
增加數據
1 public class DemoAdd { 2 3 /** 4 * @param args 5 * @throws TRSException 6 */ 7 public static void main(String[] args) throws TRSException { 8 // TODO Auto-generated method stub 9 TRSConnection conn = new TRSConnection("http://127.0.0.1:5555", "admin", "trsadmin", new ConnectParams()); 10 List<TRSInputRecord> recordList = new ArrayList<TRSInputRecord>(); 11 TRSInputRecord record_1 = new TRSInputRecord(); 12 record_1.addColumn("rowid", "1"); 13 record_1.addColumn("日期", "2018-03-15"); 14 record_1.addColumn("版次", "1"); 15 record_1.addColumn("版名", "娛樂"); 16 record_1.addColumn("標題", "街舞少年尬舞,輸的要吃要吃掉對方的帽子"); 17 record_1.addColumn("作者", "張邦煊"); 18 record_1.addColumn("正文", "新疆烏魯木齊街舞少年 日前在南湖廣場高調尬舞,揚言輸掉要吃掉對方的帽子"); 19 recordList.add(record_1); 20 21 TRSInputRecord record_2 = new TRSInputRecord(); 22 record_2.addColumn("rowid", "2"); 23 record_2.addColumn("日期", "2018.3.15"); 24 record_2.addColumn("版次", "3 "); 25 record_2.addColumn("版名", "娛樂"); 26 record_2.addColumn("標題", "ks舞社再次蟬聯九屆WID冠軍"); 27 record_2.addColumn("作者", "張邦煊"); 28 record_2.addColumn("正文", "ks舞社隊長 嘎子說:“我們已經是連續九屆的冠軍了, 拿的手都軟了”"); 29 recordList.add(record_2); 30 31 OperationParams params = new OperationParams(); 32 params.setBoolProperty("insert.skip.error", true); 33 TRSReport report = new TRSReport(); 34 try { 35 conn.executeInsert("demo", recordList,params, report);//插入數據 demo表名 recordList數據 36 conn.commitDatabase("demo", new OperationParams());//提交 37 System.out.println("insert records:" + report.getInsertedNum());//插入成功的數目 如果 executeInsert方法不寫 後面的兩個參數 則此處置為 0 38 } catch (TRSException e) { 39 System.out.println(e.getErrorCode() + ":" + e.getErrorString()); 40 } finally { 41 conn.close(); 42 } 43 } 44 45 46 47 }
這個就不運行了
刪除數據
public class DemoDel { /** * @param args * @throws TRSException */ public static void main(String[] args) throws TRSException { // TODO Auto-generated method stub TRSConnection conn = new TRSConnection("http://127.0.0.1:5555", "admin", "trsadmin", new ConnectParams()); SearchParams param = new SearchParams(); param.setReadColumns("rowid;日期;標題;作者;正文"); param.setColorColumns("標題");//高亮顯示 param.setCutSize(200); TRSResultSet resultSet = conn.executeSelect("demo", "版次:1", 0, 10, param); System.out.println(resultSet.getNumFound()); // for (int i = 0; i < resultSet.size(); i++) { // resultSet.moveNext();//移動到檢索結果記錄集的下一條記錄。 // TRSRecord re = resultSet.get(); // System.out.println(); // System.out.println("result_" + i); // System.out.println("rowid:" + re.getString("rowid")); // System.out.println("日期:" + re.getString("日期")); // System.out.println("標題:" + re.getString("標題")); // System.out.println("作者:" + re.getString("作者")); // System.out.println("正文:" + re.getString("正文")); // } for (int i = 0; i < resultSet.size(); i++) { resultSet.moveNext(); TRSRecord re = resultSet.get(); conn.executeDelete("demo", re.getUid()); } conn.close(); } }
後臺結果
數據庫的結果
修改數據
1 public class DemoUpdate { 2 3 /** 4 * @param args 5 * @throws TRSException - 6 */ 7 public static void main(String[] args) throws TRSException { 8 // TODO Auto-generated method stub 9 TRSConnection conn = new TRSConnection("http://127.0.0.1:5555", "admin", "trsadmin", new ConnectParams()); 10 SearchParams sParams = new SearchParams(); 11 TRSResultSet resultSet = conn.executeSelect("demo", "版名:娛樂", 0, 1, sParams);//查詢 12 System.out.println(resultSet.getNumFound()); 13 resultSet.moveNext();//下一個 14 TRSRecord re = resultSet.get();//取當前一條記錄 15 TRSInputRecord updated = new TRSInputRecord(); 16 updated.setUid(re.getUid()); 17 updated.addColumn("正文", "update================="); 18 ArrayList<TRSInputRecord> updateList = new ArrayList<TRSInputRecord>(1); 19 updateList.add(updated); 20 OperationParams uParams = new OperationParams(); 21 uParams.setProperty("update.mode.replace", "false"); 22 conn.executeUpdate("demo", updateList,uParams,new TRSReport()); 23 conn.close(); 24 25 26 27 } 28 29 }
運行前的數據庫
運行後
後臺打印
大數據基本的 增刪改差 Demo(資源來自網絡本人屬於收藏總結)