zoukankan      html  css  js  c++  java
  • 大数据学习练习3--第一天

    今天对这次练习的第一部分进行了完成,主要目标为完成对地域名和地域码的完善。

    开始的时候想着区百度地图爬取,顾及到可能会被限制访问,毕竟数据较多,所以就放弃了这个方法。

    第二个想到了调用百度地图API,并通过这个完成了第一部分。

    首先

    第一步,我们申请百度地图API所用ak,

    https://jingyan.baidu.com/article/e73e26c0b5b75124adb6a786.html(这个是申请的步骤教程),我们在网上很容易就可以搜到。

    第二步,我们就可以调用百度地图API来进行地域名和地域码的获取了,方法有很多种,作者是通过搜索的地点来确定经纬,然后根据经纬来确定地域名和地域码,这种方法接口返回的数据很多种,更能满足我们的需要,当然也有很多别的用法,这里附上百度地图API官方的一个手册,里面讲解很详细很全面(http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-placeapi),接下来直接附上作者的实现代码。

    Data.java

    package bao;
    
    public class Data {
    String acode="";
    String diyv="";
    }

    DBUtil.java

    package bao;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    /**
     * 
     * @author zm
     *
     */
    public class DBUtil {
        public static String db_url = "jdbc:mysql://localhost:3306/数据库名?&useSSL=false&serverTimezone=UTC";
        public static String db_user = "用户名";
        public static String db_password = "数据库密码";
    
        public static Connection getConn() {
            Connection conn = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection(db_url, db_user, db_password);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return conn;
        }
        
        public static void close(Statement state, Connection conn) {
            if(state!=null) {
                try {
                    state.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        
        public static void close(ResultSet rs, Statement state, Connection conn) {
            if(rs!=null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(state!=null) {
                try {
                    state.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    getregion.java

    package bao;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.List;
    
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;
    
    public class getregion{
         
         public Data getLocation(String lat,String lng){
             Data data=new Data();
               String location1="";
               String location2="";
               String location3="";
               String location4="";
              String url="http://api.map.baidu.com/reverse_geocoding/v3/?ak=您申请的ak&output=xml&coordtype=wgs84ll&location="+lat+","+lng;
              //System.out.println(url);
              Document doc = null;
               HttpURLConnection conn = null;
               InputStream ins = null;
               SAXReader reader = null;
               try{
                //HttpTimeoutHandler hth = new HttpTimeoutHandler(600000);
                URL conURL = new URL(null,url);
                conn = (HttpURLConnection)conURL.openConnection();
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setUseCaches(false);
                ins = conn.getInputStream();
                reader =new SAXReader();
                doc= reader.read(ins);
                Element root=doc.getRootElement();
                String docXmlText=doc.asXML();
                Element e=root.element("result");
                Element location=e.element("addressComponent");
                location1=location.asXML();
                Element adcode=location.element("adcode");
                location2=adcode.asXML();
                Element province=location.element("province");
                location3=province.asXML();
                Element district=location.element("district");
                location4=district.asXML();
                location2=location2.replace("<adcode>","").replace("</adcode>", "");
                location3=location3.replace("<province>","").replace("</province>", "");
                location4=location4.replace("<district>","").replace("</district>", "");
                data.acode=location2;
                data.diyv=location3+location4;
                System.out.println(location2);
                System.out.println(location3);
                System.out.println(location4);
                ins.close();
                conn.disconnect();
               }catch (MalformedURLException e) {
                e.printStackTrace();
               } catch (IOException e) {
                e.printStackTrace();   
               } catch (DocumentException e) {
                e.printStackTrace();
               }catch(Exception e){
                e.printStackTrace();
               }finally {
                try {
                 if (ins != null) {
                  ins.close();
                  ins = null;
                 }
                } catch (IOException e1) {
                 e1.printStackTrace();
                }
                try {
                 if (conn != null) {
                  conn.disconnect();
                  conn = null;
                 }
                } catch (Exception e2) {
                 e2.printStackTrace();
                }
               }
               return data;
            }
        public Data getlocation1(String loc){
            String location3="";
            Data data = new Data();
              String url="http://api.map.baidu.com/geocoding/v3/?address="+loc+"&output=xml&ak=您申请的ak&callback=showLocation";
              Document doc = null;
               HttpURLConnection conn = null;
               InputStream ins = null;
               SAXReader reader = null;
               try{
                //HttpTimeoutHandler hth = new HttpTimeoutHandler(600000);
                URL conURL = new URL(null,url);
                conn = (HttpURLConnection)conURL.openConnection();
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setUseCaches(false);
                ins = conn.getInputStream();
                reader =new SAXReader();
                doc= reader.read(ins);
                Element root=doc.getRootElement();
               // String docXmlText=doc.asXML();
                //System.out.println(docXmlText);
                Element e=root.element("result");
                Element e1=root.element("status");
                String check=e1.asXML().replace("</status>", "").replace("<status>","");
                System.out.println(check);
                if(check.equals("0")) {
                Element location=e.element("location");
                Element lng=location.element("lng");
                Element lat=location.element("lat");
                String lng1=lng.asXML();
                String lat1=lat.asXML();
                //System.out.println("lng"+lng1);
               // System.out.println("lat"+lat1);
               // System.out.println("location"+location.asXML());
                //System.out.println("xiayukun"+e.asXML());
                lng1=lng1.substring(lng1.indexOf("<lng>")+5,lng1.indexOf("</lng>"));
                //System.out.println(lng1);
                lat1=lat1.substring(lat1.indexOf("<lat>")+5,lat1.indexOf("</lat>"));
                //System.out.println(lat1);
                //location2=getLocation(lat1,lng1);
                data=getLocation(lat1,lng1);}
                ins.close();
                conn.disconnect();
               }catch (MalformedURLException e) {
                e.printStackTrace();
               } catch (IOException e) {
                e.printStackTrace();   
               } catch (DocumentException e) {
                e.printStackTrace();
               }catch(Exception e){
                e.printStackTrace();
               }finally {
                try {
                 if (ins != null) {
                  ins.close();
                  ins = null;
                 }
                } catch (IOException e1) {
                 e1.printStackTrace();
                }
                try {
                 if (conn != null) {
                  conn.disconnect();
                  conn = null;
                 }
                } catch (Exception e2) {
                 e2.printStackTrace();
                }
               }
               return data;
               //return location2;
            }
        
    }

    YiDao.java

    package bao;
    
    
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    public class YiDao
    {
        //int num_i=0;
        public void select_city()
        {
            getregion getregion=new getregion();
            Data data=new Data();
            Connection conn = DBUtil.getConn();
            Statement state = null;
            String str=new String();
            //String newStr=str;
            int i=0;
            try {
                state = conn.createStatement();
                ResultSet rs = state.executeQuery("select * from test2");
                while(rs.next()) {
                    //如果有结果,是认为是通过验证了
                    str=rs.getString("wanchengdanwei");
                    //newStr=str;
                    //int i = str.indexOf(" ");
                    //if(i!=-1)
                   // {
                       // newStr = str.substring(0,i);
                   // }
                   // city=getregion.SELECT(newStr);
                    data=getregion.getlocation1(str);
                    update_city(data.acode,data.diyv,str);
                    i++;
                    System.out.println("执行了"+i);
                }
            } catch (Exception e)
            {
                e.printStackTrace();
            }finally {
                DBUtil.close(state, conn);
            }
        }
        public void update_city(String acode,String diyv,String wanchengdanwei) {
            String sql = "update test2 set diyvma ='"+acode+"',biaozhundiyv = '"+diyv+"' where  wanchengdanwei ='"+wanchengdanwei+"'";
            Connection conn = DBUtil.getConn();
            Statement state = null;
            try {
                state = conn.createStatement();
                state.executeUpdate(sql);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                DBUtil.close(state, conn);
            }
        }
        public static void main(String[] args) {
            YiDao yindao=new YiDao();
            yindao.select_city();
        }
    }

    部分结果图

     到这里就结束了,当然代码也并非全部如作者编写,有借鉴网上的模板,毕竟第一次接触调用百度地图API,有想过像爬取网页一样实现,但还是感觉不正规,虽然也可以实现,然后由于数据问题,有些地点搜索不到或过长等,我们要跳过这些信息,这也没有办法,明天我会继续完成第二部分,就这么多了,再见。

  • 相关阅读:
    设计模式来替代if-else
    Cloneable接口的作用与深度克隆与浅度克隆
    IP地址分类
    MIME-TYPE
    Linux curl
    Cookie 跨域???
    冒烟测试
    @Valid、@Validated 、正则验证工具
    Node.js—第一个动态页面
    Node.js—小试牛刀-创建目录
  • 原文地址:https://www.cnblogs.com/my---world/p/12483066.html
Copyright © 2011-2022 走看看