Package hipi.imagebundle

Examples of hipi.imagebundle.HipiImageBundle


    throws IOException, InterruptedException
    {
      String temp_path = conf.get("downloader.outpath") + key.get() + ".hib.tmp";
      System.out.println("Temp path: " + temp_path);
     
      HipiImageBundle hib = new HipiImageBundle(new Path(temp_path), conf);
      hib.open(HipiImageBundle.FILE_MODE_WRITE, true);

      String word = value.toString();

      BufferedReader reader = new BufferedReader(new StringReader(word));
      String uri;
      int i = key.get();
      int iprev = i;
      while((uri = reader.readLine()) != null)     
      {
        if(i >= iprev+100) {
          hib.close();
          context.write(new BooleanWritable(true), new Text(hib.getPath().toString()));
          temp_path = conf.get("downloader.outpath") + i + ".hib.tmp";
          hib = new HipiImageBundle(new Path(temp_path), conf);
          hib.open(HipiImageBundle.FILE_MODE_WRITE, true);
          iprev = i;
        }
        long startT=0;
        long stopT=0;    
        startT = System.currentTimeMillis();           

        try {
          String type = "";
          URLConnection conn;
          // Attempt to download
          context.progress();

          try {
            URL link = new URL(uri);
            System.err.println("Downloading " + link.toString());
            conn = link.openConnection();
            conn.connect();
            type = conn.getContentType();
          } catch (Exception e)
          {
            System.err.println("Connection error to image : " + uri);
            continue;
          }

          if (type == null)
            continue;

          if (type.compareTo("image/gif") == 0)
            continue;

          if (type != null && type.compareTo("image/jpeg") == 0)
            hib.addImage(conn.getInputStream(), ImageType.JPEG_IMAGE);
         
        } catch(Exception e)
        {
          e.printStackTrace();
          System.err.println("Error... probably cluster downtime");
          try
          {
            Thread.sleep(1000);         
          } catch (InterruptedException e1)
          {
            e1.printStackTrace();
          }
        }

        i++;
       
        // Emit success
        stopT = System.currentTimeMillis();
        float el = (float)(stopT-startT)/1000.0f;
        System.err.println("> Took " + el + " seconds\n");       
      }


      try
      {
        reader.close();
        hib.close();
        context.write(new BooleanWritable(true), new Text(hib.getPath().toString()));
      } catch (Exception e)
      {
        e.printStackTrace();
      }
View Full Code Here


    public void reduce(BooleanWritable key, Iterable<Text> values, Context context)
    throws IOException, InterruptedException
    {
      if(key.get()){
        FileSystem fileSystem = FileSystem.get(conf);
        HipiImageBundle hib = new HipiImageBundle(new Path(conf.get("downloader.outfile")), conf);
        hib.open(HipiImageBundle.FILE_MODE_WRITE, true);
        for (Text temp_string : values) {
          Path temp_path = new Path(temp_string.toString());
          HipiImageBundle input_bundle = new HipiImageBundle(temp_path, conf);
          hib.append(input_bundle);
         
          Path index_path = input_bundle.getPath();
          Path data_path = new Path(index_path.toString() + ".dat");
          System.out.println("Deleting: " + data_path.toString());
          fileSystem.delete(index_path, false);
          fileSystem.delete(data_path, false);
         
          context.write(new BooleanWritable(true), new Text(input_bundle.getPath().toString()));
          context.progress();
        }
        hib.close();
      }
    }
View Full Code Here

    int numMapTasks = conf.getInt("hipi.map.tasks", 0);
    List<InputSplit> splits = new ArrayList<InputSplit>();
    for (FileStatus file : listStatus(job)) {
      Path path = file.getPath();
      FileSystem fs = path.getFileSystem(conf);
      HipiImageBundle hib = new HipiImageBundle(path, conf);
      hib.open(AbstractImageBundle.FILE_MODE_READ);
      // offset should be guaranteed to be in order
      List<Long> offsets = hib.getOffsets();
      BlockLocation[] blkLocations = fs.getFileBlockLocations(hib.getDataFile(), 0, offsets.get(offsets.size() - 1));
      if (numMapTasks == 0) {
        int i = 0, b = 0;
        long lastOffset = 0, currentOffset = 0;
        for (; (b < blkLocations.length) && (i < offsets.size()); b++) {
          long next = blkLocations[b].getOffset() + blkLocations[b].getLength();
          while (currentOffset < next && i < offsets.size()) {
            currentOffset = offsets.get(i);
            i++;
          }
          String[] hosts = null;
          if (currentOffset > next) {
            Set<String> hostSet = new HashSet<String>();
            int endIndex = getBlockIndex(blkLocations, currentOffset - 1);
            for (int j = b; j < endIndex; j++) {
              String[] blkHosts = blkLocations[j].getHosts();
              for (int k = 0; k < blkHosts.length; k++)
                hostSet.add(blkHosts[k]);
            }
            hosts = (String[]) hostSet.toArray(new String[hostSet.size()]);
          } else { // currentOffset == next
            hosts = blkLocations[b].getHosts();
          }
          splits.add(new FileSplit(hib.getDataFile().getPath(), lastOffset, currentOffset - lastOffset, hosts));
          lastOffset = currentOffset;
        }
        System.out.println(b + " tasks spawned");
      } else {
        int imageRemaining = offsets.size();
        int i = 0, taskRemaining = numMapTasks;
        long lastOffset = 0, currentOffset;
        while (imageRemaining > 0) {
          int numImages = imageRemaining / taskRemaining;
          if (imageRemaining % taskRemaining > 0)
            numImages++;
          int next = Math.min(offsets.size() - i, numImages) - 1;
          int startIndex = getBlockIndex(blkLocations, lastOffset);
          currentOffset = offsets.get(i + next);
          int endIndex = getBlockIndex(blkLocations, currentOffset - 1);
          ArrayList<String> hosts = new ArrayList<String>();
          // check getBlockIndex, and getBlockSize
          for (int j = startIndex; j <= endIndex; j++) {
            String[] blkHosts = blkLocations[j].getHosts();
            for (int k = 0; k < blkHosts.length; k++)
              hosts.add(blkHosts[k]);
          }
          splits.add(new FileSplit(hib.getDataFile().getPath(), lastOffset, currentOffset - lastOffset, hosts.toArray(new String[hosts.size()])));
          lastOffset = currentOffset;
          i += next + 1;
          taskRemaining--;
          imageRemaining -= numImages;
          System.out.println("imageRemaining: " + imageRemaining + "\ttaskRemaining: " + taskRemaining + "\tlastOffset: " + lastOffset + "\ti: " + i);
        }
      }
      hib.close();
    }
    return splits;
  }
View Full Code Here

public class CreateHipiImageBundle {
  public static void main(String[] args) throws IOException {
    File folder = new File(args[0]);
    File[] files = folder.listFiles();
    Configuration conf = new Configuration();
    HipiImageBundle hib = new HipiImageBundle(new Path(args[1]), conf);
    hib.open(AbstractImageBundle.FILE_MODE_WRITE, true);
    for (File file : files) {
      FileInputStream fis = new FileInputStream(file);
      String fileName = file.getName().toLowerCase();
      String suffix = fileName.substring(fileName.lastIndexOf('.'));
      if (suffix.compareTo(".jpg") == 0 || suffix.compareTo(".jpeg") == 0) {
        hib.addImage(fis, ImageType.JPEG_IMAGE);
      } else if (suffix.compareTo(".png") == 0) {
        hib.addImage(fis, ImageType.PNG_IMAGE);
      }
    }
    hib.close();
  }
View Full Code Here

public class HipiImageBundleTestCase extends AbstractImageBundleTestCase {

  @Override
  public AbstractImageBundle createImageBundleAndOpen(int mode) throws IOException {
    Configuration conf = new Configuration();
    HipiImageBundle hib = new HipiImageBundle(new Path("/tmp/bundle.hib"), conf);
    hib.open(mode, true);
    return hib;
  }
View Full Code Here

    return hib;
  }

  @Test
  public void testOffsets() throws IOException {
    HipiImageBundle hib = (HipiImageBundle) createImageBundleAndOpen(AbstractImageBundle.FILE_MODE_READ);
    List<Long> offsets = hib.getOffsets();
    assertEquals("the first offset should be the size of first image plus 8", (long) 128037 + 8, (long) offsets.get(0));
    assertEquals("the second offset should be the size of data file", (long) 171236, (long) offsets.get(1));
  }
View Full Code Here

  @Test
  public void testAppend() throws IOException {
    //create image bundles
    Configuration conf = new Configuration();
    HipiImageBundle aib1 = new HipiImageBundle(new Path("/tmp/bundle1.hib"), conf);
    aib1.open(AbstractImageBundle.FILE_MODE_WRITE, true);   
    aib1.addImage(new FileInputStream("data/test/ImageBundleTestCase/read/0.jpg"), ImageType.JPEG_IMAGE);
    aib1.addImage(new FileInputStream("data/test/ImageBundleTestCase/read/1.jpg"), ImageType.JPEG_IMAGE);
    aib1.close();

    HipiImageBundle aib2 = new HipiImageBundle(new Path("/tmp/bundle2.hib"), conf);
    aib2.open(AbstractImageBundle.FILE_MODE_WRITE, true);
    aib2.addImage(new FileInputStream("data/test/ImageBundleTestCase/read/2.jpg"), ImageType.JPEG_IMAGE);
    aib2.addImage(new FileInputStream("data/test/ImageBundleTestCase/read/3.jpg"), ImageType.JPEG_IMAGE);
    aib2.close();

    HipiImageBundle aib1_in = new HipiImageBundle(new Path("/tmp/bundle1.hib"), conf);
    HipiImageBundle aib2_in = new HipiImageBundle(new Path("/tmp/bundle2.hib"), conf);

    HipiImageBundle merged_hib = new HipiImageBundle(new Path("/tmp/merged_bundle.hib"), conf);
    merged_hib.open(HipiImageBundle.FILE_MODE_WRITE, true);
    merged_hib.append(aib1_in);
    merged_hib.append(aib2_in);
    merged_hib.close();
  }
View Full Code Here

    int numMapTasks = conf.getInt("hipi.map.tasks", 0);
    List<InputSplit> splits = new ArrayList<InputSplit>();
    for (FileStatus file : listStatus(job)) {
      Path path = file.getPath();
      FileSystem fs = path.getFileSystem(conf);
      HipiImageBundle hib = new HipiImageBundle(path, conf);
      hib.open(AbstractImageBundle.FILE_MODE_READ);
      // offset should be guaranteed to be in order
      List<Long> offsets = hib.getOffsets();
      BlockLocation[] blkLocations = fs.getFileBlockLocations(hib.getDataFile(), 0, offsets.get(offsets.size() - 1));
      if (numMapTasks == 0) {
        int i = 0, b = 0;
        long lastOffset = 0, currentOffset = 0;
        for (; (b < blkLocations.length) && (i < offsets.size()); b++) {
          long next = blkLocations[b].getOffset() + blkLocations[b].getLength();
          while (currentOffset < next && i < offsets.size()) {
            currentOffset = offsets.get(i);
            i++;
          }
          String[] hosts = null;
          if (currentOffset > next) {
            Set<String> hostSet = new HashSet<String>();
            int endIndex = getBlockIndex(blkLocations, currentOffset - 1);
            for (int j = b; j < endIndex; j++) {
              String[] blkHosts = blkLocations[j].getHosts();
              for (int k = 0; k < blkHosts.length; k++)
                hostSet.add(blkHosts[k]);
            }
            hosts = (String[]) hostSet.toArray(new String[hostSet.size()]);
          } else { // currentOffset == next
            hosts = blkLocations[b].getHosts();
          }
          splits.add(new FileSplit(hib.getDataFile().getPath(), lastOffset, currentOffset - lastOffset, hosts));
          lastOffset = currentOffset;
        }
        System.out.println(b + " tasks spawned");
      } else {
        int imageRemaining = offsets.size();
        int i = 0, taskRemaining = numMapTasks;
        long lastOffset = 0, currentOffset;
        while (imageRemaining > 0) {
          int numImages = imageRemaining / taskRemaining;
          if (imageRemaining % taskRemaining > 0)
            numImages++;
          int next = Math.min(offsets.size() - i, numImages) - 1;
          int startIndex = getBlockIndex(blkLocations, lastOffset);
          currentOffset = offsets.get(i + next);
          int endIndex = getBlockIndex(blkLocations, currentOffset - 1);
          ArrayList<String> hosts = new ArrayList<String>();
          // check getBlockIndex, and getBlockSize
          for (int j = startIndex; j <= endIndex; j++) {
            String[] blkHosts = blkLocations[j].getHosts();
            for (int k = 0; k < blkHosts.length; k++)
              hosts.add(blkHosts[k]);
          }
          splits.add(new FileSplit(hib.getDataFile().getPath(), lastOffset, currentOffset - lastOffset, hosts.toArray(new String[hosts.size()])));
          lastOffset = currentOffset;
          i += next + 1;
          taskRemaining--;
          imageRemaining -= numImages;
          System.out.println("imageRemaining: " + imageRemaining + "\ttaskRemaining: " + taskRemaining + "\tlastOffset: " + lastOffset + "\ti: " + i);
        }
      }
      hib.close();
    }
    return splits;
  }
View Full Code Here

TOP

Related Classes of hipi.imagebundle.HipiImageBundle

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.