eBay API, get globalID from numerical Site ID

2019-08-29 04:32发布

问题:

As the title says, I have the numerical Site ID, I need a way to get the corresponding GlobalId.

I know there are tables with matches

https://developer.ebay.com/DevZone/merchandising/docs/CallRef/Enums/GlobalIdList.html

https://developer.ebay.com/DevZone/merchandising/docs/Concepts/SiteIDToGlobalID.html

but I need a programmatic way to do it.

回答1:

Your three options:

Hash Map implementation

 import java.util.HashMap;
 public class Mappings{

 public static void main(String []args){

    HashMap myMap = new HashMap<Integer, String>();
    myMap.put(0, "EBAY-US");
    myMap.put(2, "EBAY-ENCA");
    myMap.put(3, "EBAY-GB");
    myMap.put(15, "EBAY-AU");
    //...

    System.out.println("Given ID 15, it's corresponding Global ID is: " + myMap.get(15));

   }
}

SQL Table implementation

 //STEP 1. Import required packages
 import java.sql.*;

 public class JDBCExample {
    // JDBC driver name and database URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
    static final String DB_URL = "jdbc:mysql://localhost/STUDENTS";

    //  Database credentials
    static final String USER = "username";
    static final String PASS = "password";

    public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;
    try{
       //STEP 2: Register JDBC driver
       Class.forName("com.mysql.jdbc.Driver");

       //STEP 3: Open a connection
       System.out.println("Connecting to a selected database...");
       conn = DriverManager.getConnection(DB_URL, USER, PASS);
       System.out.println("Connected database successfully...");

       //STEP 4: Execute a query
       System.out.println("Inserting records into the table...");
       stmt = conn.createStatement();

       String sql = "INSERT INTO Mappings " +
               "VALUES (0, 'EBAY-US')";
       stmt.executeUpdate(sql);

       sql = "INSERT INTO Mappings " +
               "VALUES (2, 'EBAY-ENCA')";
       stmt.executeUpdate(sql);

       sql = "INSERT INTO Mappings " +
               "VALUES (3, 'EBAY-GB')";
       stmt.executeUpdate(sql);

       sql = "INSERT INTO Mappings " +
               "VALUES (15, 'EBAY-AU')";
       stmt.executeUpdate(sql);

       System.out.println("Inserted records into the table...");



       sql = "SELECT siteID, globalID FROM Mappings";

       ResultSet rs = stmt.executeQuery(sql);

     //STEP 5: Extract data from result set
       while(rs.next()){
          //Retrieve by column name
            int siteId  = rs.getInt("siteID");
            int globalId = rs.getInt("globalID");

            //Display values
            System.out.print("siteId: " + siteId);
            System.out.print(", globalId: " + globalId);

        }

    rs.close();
    }catch(SQLException se){
       //Handle errors for JDBC
       se.printStackTrace();
    }catch(Exception e){
       //Handle errors for Class.forName
       e.printStackTrace();
    }finally{
       //finally block used to close resources
       try{
          if(stmt!=null)
             conn.close();
       }catch(SQLException se){
       }// do nothing
       try{
          if(conn!=null)
             conn.close();
       }catch(SQLException se){
          se.printStackTrace();
       }//end finally try
    }//end try
    System.out.println("Goodbye!");
 }//end main
 }//end JDBCExample

Kimono implementation

If you don't want to have local storage for the Site ID <=> Global ID mappings, as mentioned in the techcrunch article, "Kimono Is A Smarter Web Scraper That Lets You “API-ify” The Web, No Code Required,":

 With Kimono, the end goal is to simplify data extraction so that anyone can manage it. 
 Then Kimono’s learning algorithm will build a data model involving the items you’ve
 selected.

As Kimono's website goes onto mention:

 Turn websites into structured APIs from your browser in seconds.