Friday, May 6, 2011

Write php array in HBase using thrift

I have a Thrift php client and I want to write in a HBase table and I'm doing the following:

  $mutations = array(
    new Mutation( array(
      'column' => 'entry:num',
      'value' => array('a','b','c')
    ) ),
  );
  $client->mutateRow( $t, $row, $mutations );

The problem is that when inserting in HBase the value, which is an array, gets converted to 'Array' instead of storing the elements of the array. How can I store the list as an array (or byte array)

From stackoverflow
  • I must admit that I do not have a clue what you're trying to do (perhaps due to a lack of knowledge regarding Thrift and HBase), but if I understood your question correctly, you're trying to write some PHP data structure (array in this case) to a storage media. To achieve this you have to serialize your data somehow. That could be using a custom XML serialization, a custom binary serialization or, perhaps the most simple solution, the PHP internal serialization mechanism provided by serialize() and the corresponding unserialize().

    If you strive for inter-language-interoperability you should use a custom serialization or you have to write a unserialization function that unserializes the PHP serialization format in your target language.

    Just a quick example - I don't know where you'd have to put this code, as I don't know exactly what you're doing:

    $mutations = array(
        new Mutation(array(
          'column' => 'entry:num',
          'value'  => array('a','b','c')
        )),
      );
    $data = serialize($mutations); // $data now is a string
    // write $data to storage
    // read $readData from storage
    $readMutations = unserialize($readData);
    // $readMutations == $mutations 
    // (but the Mutation instances are not the same instances any more)
    

    Please seee

How can I read SMS messages from the inbox programmatically in Android?

I want to retrieve the SMS messages from the inbox and display them?

From stackoverflow
  • It is a trivial process. You can see a good example in the source code SMSPopup

    Examine the following methods:

    public static SmsMmsMessage getSmsDetails(Context context,
                            long ignoreThreadId, boolean unreadOnly)
    public static long findMessageId(Context context, long threadId, long _timestamp, int messageType
    public static void setMessageRead(Context context, long messageId, int messageType)
    public static void deleteMessage(Context context, long messageId, long threadId, int messageType)
    

    this is the method for reading:

            public static SmsMmsMessage getSmsDetails(Context context,
                            long ignoreThreadId, boolean unreadOnly) {
    
                    String SMS_READ_COLUMN = "read";
                    String WHERE_CONDITION = unreadOnly ? SMS_READ_COLUMN + " = 0" : null;
                    String SORT_ORDER = "date DESC";
                    int count = 0;
    
                    //Log.v(WHERE_CONDITION);
    
                    if (ignoreThreadId > 0) {
    //                      Log.v("Ignoring sms threadId = " + ignoreThreadId);
                            WHERE_CONDITION += " AND thread_id != " + ignoreThreadId;
                    }
    
                    Cursor cursor = context.getContentResolver().query(
                                    SMS_INBOX_CONTENT_URI,
                          new String[] { "_id", "thread_id", "address", "person", "date", "body" },
                                    WHERE_CONDITION,
                                    null,
                                    SORT_ORDER);
    
                    if (cursor != null) {
                            try {
                                    count = cursor.getCount();
                                    if (count > 0) {
                                            cursor.moveToFirst();
    
    //                                      String[] columns = cursor.getColumnNames();
    //                                      for (int i=0; i<columns.length; i++) {
    //                                              Log.v("columns " + i + ": " + columns[i] + ": "
    //                                                              + cursor.getString(i));
    //                                      }
    
                                            long messageId = cursor.getLong(0);
                                            long threadId = cursor.getLong(1);
                                            String address = cursor.getString(2);
                                            long contactId = cursor.getLong(3);
                                            String contactId_string = String.valueOf(contactId);
                                            long timestamp = cursor.getLong(4);
    
                                            String body = cursor.getString(5);
    
                                            if (!unreadOnly) {
                                                    count = 0;
                                            }
    
                                            SmsMmsMessage smsMessage = new SmsMmsMessage(
                                                            context, address, contactId_string, body, timestamp,
                                                            threadId, count, messageId, SmsMmsMessage.MESSAGE_TYPE_SMS);
    
                                            return smsMessage;
    
                                    }
                            } finally {
                                    cursor.close();
                            }
                    }               
                    return null;
            }
    

How to use Union method or left outer join?

i can not join #Temp with scr_SecuristLog. How can i do it?

CREATE TABLE #Temp (VisitingCount int, [Time] int )
 DECLARE @DateNow DATETIME,@i int,@Time int
    set @DateNow='00:00'  
    set @i=1;  
    while(@i<48)  
        begin  
set @DateNow = DATEADD(minute, 30, @DateNow)
set @Time = (datepart(hour,@DateNow)*60+datepart(minute,@DateNow))/30 
insert into #Temp(VisitingCount,[Time]) values(0,@Time )
set @i=@i+1
                end
select VisitingCount, [Time]
from #Temp as t
left outer join (
    select count(page) as VisitingCount, 
    (datepart(hour,Date)*60+datepart(minute,Date))/30 as [Time]
    from scr_SecuristLog
    where Date between '2009-05-04' and '2009-05-05'
) as s
    on t.VisitingCount = s.VisitingCount
        and t.Time = s.Time

This Codes give error:



Msg 8120, Level 16, State 1, Line 1 Column 'scr_SecuristLog.Date' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 8120, Level 16, State 1, Line 1 Column 'scr_SecuristLog.Date' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 209, Level 16, State 1, Line 1 Ambiguous column name 'VisitingCount'.
Msg 209, Level 16, State 1, Line 1 Ambiguous column name 'Time'.

From stackoverflow
  • As you don't mention a specific error, I am guessing your error comes from the fact you have not prefixed your select values.

    select t.VisitingCount, t.[Time]
    


    Edit

    Your second error should be resolved with this group by.

    select count(page) as VisitingCount, 
    (datepart(hour,Date)*60+datepart(minute,Date))/30 as [Time]
    from scr_SecuristLog
    where Date between '2009-05-04' and '2009-05-05'
    GROUP BY (datepart(hour,Date)*60+datepart(minute,Date))/30
    
  • I think you need to add a GROUP BY in your derived table scr_SecuristLog, you need to group it by time because your using an aggregate function count.

  • Ok try this

    CREATE TABLE #Temp (VisitingCount int, [Time] int )
    DECLARE @DateNow DATETIME,@i int,@Time int
    set @DateNow='00:00'  
    set @i=1;  
    while(@i<48)  
        begin  
            set @DateNow = DATEADD(minute, 30, @DateNow)
            set @Time = (datepart(hour,@DateNow)*60+datepart(minute,@DateNow))/30 
            insert into #Temp(VisitingCount,[Time]) values(0,@Time )
            set @i=@i+1
        end
        select t.VisitingCount, t.[Time]
        from #Temp as t
        left outer join (
             select count(page) as VisitingCount, 
           (datepart(hour,Date)*60+datepart(minute,Date))/30 as [Time]
           from scr_SecuristLog
           where Date between '2009-05-04' and '2009-05-05'
           GROUP BY scr_SecuristLog.Date
       ) as s
        on t.VisitingCount = s.VisitingCount
        and t.Time = s.Time
    
  • Your inner select (which you are joining on) does not aggregate properly. You have a count and a column. This means you need to group by the column for SQL to understand it properly.

    select t.VisitingCount, t.[Time]
    from #Temp as t
    left outer join (
        select count(page) as VisitingCount, 
        (datepart(hour,Date)*60+datepart(minute,Date))/30 as [Time]
        from scr_SecuristLog
        where Date between '2009-05-04' and '2009-05-05'
        GROUP BY [Date]
    ) as s
        on t.VisitingCount = s.VisitingCount
            and t.Time = s.Time
    

    What you might actually need is to group by the calculated column instead, in which case your group by should be:

    GROUP BY (datepart(hour,Date)*60+datepart(minute,Date))/30
    
  • 
    CREATE TABLE #Temp (
      VisitingCount INT,
      [Time] INT)
    DECLARE @DateNow DATETIME,
      @i INT,
      @Time INT
    SET @DateNow = '00:00'
    SET @i = 1 ;
    WHILE(@i < 48)
    BEGIN
    SET @DateNow = DATEADD(minute, 30, @DateNow) SET @Time = (DATEPART(hour, @DateNow) * 60 + DATEPART(minute, @DateNow)) / 30 INSERT INTO #Temp (VisitingCount, [Time]) VALUES (0, @Time) SET @i = @i + 1 END SELECT VisitingCount, [Time] FROM #Temp AS t UNION SELECT COUNT(page) AS VisitingCount, (DATEPART(hour, Date) * 60 + DATEPART(minute, Date)) / 30 AS [Time] FROM scr_SecuristLog WHERE Date BETWEEN '2009-05-04' AND '2009-05-05' GROUP BY Date

    DROP TABLE #Temp

How to configure WCF end-to-end logging?

Hi, I have a problem that I'd like to solve, but I don't know how to configure end-to-end logging in WCF (I'm not sure I can solve my problem even with end-to-end logging enabled). I have three applications (A, B, C) that use WCF service through NetMsmqBinding to communicate with each other. A is the host, and B and C are clients of the WCF service. The service uses message security with certificates. The communication between B and A works perfectly OK, though, the communication between C and A does not. So I'd like to use logging and figure out why the messages are deleted from the queue (in the case C -> A) and are not processed by the host... How do we configure logging for this scenario in order to diagnose the problem... (app B and app C are on the same machine, app A is on a different machine). (B -> A works both with and without certificates) (C -> A works ONLY without certificates). I need to configure C -> A to work with certificates, but I don't know what's wrong in order to fix it. Please help!

Thanks!

From stackoverflow
  • Here is how to do WCF end-to-end tracing:

    http://msdn.microsoft.com/en-us/library/aa738749.aspx

    In your case I would have used Enterprise Library Logging. Logged entry and exit of each method with which parameters were sent, as well as using try catch around critical parts of the code, with logging of the exception.

    Hope this helps.

Design patterns for web services?

Hi,

I am developing web service in C#, .NET 2.0. What are the proven design patterns for a web service? If possible please provide me the link about these design patterns?

From stackoverflow

C# mono p/invoke failure

Recently I tried to use p/invoke in mono (.NET for Linux platform) to write a simple OpenGL application to find out how it works on C# (I've already sucessfully done it on windows). I heard about the tao framework, but I don't want everything for a simple "hello world" like program.

I just stucked at the start. I p/invoked some GL functions to see if they work. I immediately called glClearColor and glClear to see whether it sets glGetError or not (due to opengl haven't been initialized at all at that point).

Instead of calling the function it just crashes and dumps the following stack trace and other debuginfo. No exception is thrown.

Stacktrace:

  at (wrapper managed-to-native) Calmarius.OGL.OpenGLLibrary.glClearColor (single,single,single,single) <0x00004>
  at (wrapper managed-to-native) Calmarius.OGL.OpenGLLibrary.glClearColor (single,single,single,single) <0xffffffff>
  at Calmarius.RTS.GameForm.OnPaint (System.Windows.Forms.PaintEventArgs) [0x00000] in /home/calmarius/Development/csharp/RTS/RTS/Form1.cs:60
  at System.Windows.Forms.Control.WmPaint (System.Windows.Forms.Message&) <0x000b0>
  at System.Windows.Forms.Control.WndProc (System.Windows.Forms.Message&) <0x001e2>
  at System.Windows.Forms.ScrollableControl.WndProc (System.Windows.Forms.Message&) <0x0000d>
  at System.Windows.Forms.ContainerControl.WndProc (System.Windows.Forms.Message&) <0x00054>
  at System.Windows.Forms.Form.WndProc (System.Windows.Forms.Message&) <0x001da>
  at ControlWindowTarget.OnMessage (System.Windows.Forms.Message&) <0x00014>
  at ControlNativeWindow.WndProc (System.Windows.Forms.Message&) <0x00022>
  at System.Windows.Forms.NativeWindow.WndProc (intptr,System.Windows.Forms.Msg,intptr,intptr) <0x001b7>
  at System.Windows.Forms.XplatUIX11.DispatchMessage (System.Windows.Forms.MSG&) <0x00016>
  at System.Windows.Forms.XplatUI.DispatchMessage (System.Windows.Forms.MSG&) <0x00015>
  at System.Windows.Forms.Application.RunLoop (bool,System.Windows.Forms.ApplicationContext) <0x00997>
  at System.Windows.Forms.Application.Run (System.Windows.Forms.ApplicationContext) <0x0006a>
  at System.Windows.Forms.Application.Run (System.Windows.Forms.Form) <0x00025>
  at Calmarius.RTS.Program.Main () [0x0000b] in /home/calmarius/Development/csharp/RTS/RTS/Program.cs:19
  at (wrapper runtime-invoke) System.Object.runtime_invoke_void (object,intptr,intptr,intptr) <0xffffffff>

The signature for glClearColor is:

//gllibname="opengl32.dll" --> mapped to libGL.so
[DllImport(gllibname)]

public static extern void glClearColor(float red, float green, float blue, float alpha);

C specification is:

void glClearColor( GLclampf red,GLclampf green,GLclampf blue,GLclampf alpha );

GLclampf is float as I saw its declaration in the header.

From stackoverflow
  • You could run the program with gdb and see exactly where the SEGV happens (see the mono wiki for instructions).

    A likely cause is that some other incorrect p/invoke declaration and call in the code corrupted memory so later you get the crash.

I want a way to know if a particular web page is completely loaded using javascript

Hi,

I want a way to know if a particular web page is completely loaded using javascript

Ex) A web page can have dynamic contents getting displayed through Ajax, or contents comming through javascript, all the images and css.I need to find out when all these contents are loaded properly and the webpage is stablilized.

I got some pointers like ondocumentready its an alternate soultion for window.onload, onreadystatechange but these solutions are unreliable sometimes it works and sometimes it doesn’t

Also the solution should be browser independent, i.e it should work for all browsers

Any pointers on this will be highly appreciated

Thanks in advance

From stackoverflow
  • Use jQuery and its easy:

    <script type="text/javascript">
        $(document).ready(function() {
            alert('doc is ready');
            });
        });
    </script>
    
    • browser independent
    • highly reliable
    • awesome
  • You can also use the following. It's a shorthand version of the code posted by @cottsak

    $(function() 
    { 
         // your code 
    });
    

    if you’re using jQuery.noConflict() (which is used to give control of the $ variable back to whichever library first implemented it), you can do this:

    By adding jQuery.noConflict(), jquery won't conflict with $ variables of other libraries

    jQuery(function($) 
    { 
           // your code
    });
    
  • Using pure javascript you can put javascript in the onload event of the body tag like so:

    <body onload="function">
    

    This will trigger when the page has finished loading.