Data Restore @ GAE

日本語

@GAE, cannot write even temporary file, sqlite on memory can not restore to except File, so this is how to restore at Google App Engine. (or use h2db.write(Writer writer, ResultSet rs))

JSON
Blob Base64 String
Text String

for exporting, and to restore, its reverse.

necessary jars

  1. commons-codec-1.5.jar
  2. commons-io-1.0.jar
  3. json_simple-1.1.jar
  4. cos.jar

export

running codes

Tidder export ExportTidders.java
File export ExportAttachments.java
process
  1. create JSONArray
  2. PMF.select(Foo.class)
  3. put every single Foo to JSONArray
    1. convert Blob > Byte > commons-codec Blob64.toBase64String() if you need
  4. JSONArray.write(HttpResponse.PrintWriter)
code
    @Override
    public void doGet(final HttpServletRequest req, final HttpServletResponse res)
    {
        final String page = req.getParameter("p");
        final int p = (page == null) ? 0 : (!page.matches("[0-9]+")) ? 0 : Integer.parseInt(page);
        final PersistenceManager pm = PMF.get().getPersistenceManager();
        final javax.jdo.Query que = pm.newQuery("SELECT FROM " + com.tidspr.tsr.entity.File.class.getName());
        que.setRange(0 + (1000 * p), 999 + (1000 * p)); // export?p=1 -> record 1000~1999
        System.out.println(que.toString());
        final List<com.tidspr.tsr.entity.File> list = (List<com.tidspr.tsr.entity.File>) que.execute();
        try
        {
            res.setContentType("application/json");
            res.setCharacterEncoding("UTF-8");
            final PrintWriter out = res.getWriter();
            final JSONArray rs = new JSONArray();
            for (final com.tidspr.tsr.entity.File f : list)
            {
                final JSONObject obj = new JSONObject();
                obj.put("name", f.name());
                obj.put("key", f.key().getName());
                obj.put("data", new String(new Base64().encodeBase64(f.blob().getBytes())));//Blob
                obj.put("type", f.type());//String
                obj.put("wiki", f.wiki());
                obj.put("raw", t.raw().getValue());//Text
                rs.add(obj);
            }
            rs.writeJSONString(out);
        }
        catch (final IOException e)
        {
            e.printStackTrace();
        }
    }

import

running codes

Tidder import ImportTidders.java
File import ImportAttachments.java
process
  1. getPost
  2. MultipartParser cos.jar
  3. String <- commons-io IOUtils.toString(Post-FilePart.getInputStream())
  4. List<Foo.Class>
  5. JSONArray <- JSONValue.parse(String)
  6. for each JSON in JSONArray
    1. create Foo.class <- JSON
      1. convert Blob < Byte < commons-codec Blob64.decodeBase64String(Base64String) if you need
    2. List.add(Foo.class)
  7. PMF.makePersistentAll(List);
  8. PMF.flush();
code
    @Override
    public void doPost(final HttpServletRequest req, final HttpServletResponse res)
    {
        try
        {
            final MultipartParser p = new MultipartParser(req, 50000000, false, false, "UTF-8");// < 50Mb
            Part pa;
            while ((pa = p.readNextPart()) != null)
            {
                if (pa.isFile())
                {
                    final FilePart fp = ((FilePart) pa);
                    final List<com.tidspr.tsr.entity.File> ls = new LinkedList<com.tidspr.tsr.entity.File>();
                    final Object obj = JSONValue.parse(IOUtils.toString(fp.getInputStream(),"UTF-8"));
                    final JSONArray array = (JSONArray) obj;
                    final Iterator<Object> i = array.iterator();
                    while (i.hasNext())
                    {
                        final JSONObject jo = (JSONObject) i.next();
                        final File f = new com.tidspr.tsr.entity.File((String) jo.get("name"), new Blob(new Base64().decodeBase64((String) jo.get("data"))), (String) jo.get("type"), (String) jo.get("wiki"));//new Text((String) jo.get("raw").getValue()) for google.Text
                        final Key k = KeyFactory.createKey(com.tidspr.tsr.entity.File.class.getSimpleName(), (String) jo.get("key"));
                        f.key(k);
                        ls.add(f);
                    }
                    final PersistenceManager pm = PMF.get().getPersistenceManager();
                    pm.makePersistentAll(ls);
                    pm.flush();
                    pm.close();
                }
            }

        }
        catch (final IOException e1)
        {
            e1.printStackTrace();
        }
    }

try

/ts.export.json Tidder Export
/fs.export.json File Export
/ts.import.json Tidder Restore (Owner Authorization Needed)
/fs.import.json File Restore (Owner Authorization Needed)
see also Export & Restore
    last modified: 19 July 2011 [ View wiki source Close ]
     
SEARCH DIFFERENCE ATTACHMENT RECENT POST